-
Notifications
You must be signed in to change notification settings - Fork 147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[rfc] Implement an ObjectUrl wrapper #231
Conversation
crates/file/src/object_url.rs
Outdated
pub fn new(target: &dyn ObjectUrlTarget) -> Self { | ||
Self { | ||
inner: Rc::new(ObjectUrlAllocation::allocate_new_object_url(target)), | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just have From
impls? From<Blob>
, From<File>
, etc should be fine. I think it's better to use Rust's traits instead of creating our own, unless we have to.
A macro may be used to make implementing traits easier. Rust's orphan rules will ensure that no more traits can be implemented, effectively sealing it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the most part, it's replicating BlobContents
. The "overload" here is for the Url::create_object_url
method. A potential impl for MediaSource
has been omitted because it's deprecated. Should it be added, too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the most part, it's replicating
BlobContents
. The "overload" here is for theUrl::create_object_url
method.
I didn't know BlobContents
also did it. I'd argue that should be changed as well but that's for another PR. From trait can also provide the overload and (imo) is better since it's part of std lib so users are more familiar with it and understand how that overload work. That may also help avoid looking at documentation to see how to construct it since the user can just use Intellisense with the From trait.
A potential impl for
MediaSource
has been omitted because it's deprecated. Should it be added, too?
Unless someone complains and has a valid use-case, no, let's not add support deprecated APIs.
Released in gloo-file v0.2.3 🎉 |
Rationale
The
createObjectUrl
API is offered by the browser to create URLs from Blobs that are too large to represent as data urls. The resulting url is along the lines ofblob:[origin]://[uuid]
. There's a counter partrevokeObjectURL
that releases the URL again. Rust lends itself to an abstraction that automatically revokes the url when it is not need any longer.The API
A
struct ObjectUrl
abstracts the allocation of a specific ObjectUrl. It uses reference counting to revoke the Url after the last user drops its use. It can be created from aBlob
,File
or directly from aweb_sys::Blob
. ADeref
implemention allows access to the stringly-typed url under-the-hood.