-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
a kind of GlobalProxy utility and expose global self
for universal window/worker usage
#1046
Comments
I agree, all generated JS should use access |
Thanks for the report! I think this is definitely a case where we don't handle this super-well today. One option is to use It seems reasonable though to at the very least have documentation about this pattern if not a crate which supports it! |
but how to do the check: |
We should probably fixup the |
@alexcrichton I'm not sure I like that idea. It seems like a footgun to conflate "this isn't the right type" with "this type doesn't exist". I can see situations where people would want to distinguish those situations (e.g. polyfills), and it can be confusing if the type check silently returns In addition, type detection often involves much more than just So my feeling is that we should have support for a more general type detection system. Such as an attribute that allows users to provide custom behavior for As for the issue about Window/WorkerGlobalScope, it's unfortunate that they don't share a common ancestor, instead they use mixins. I wonder if web-sys should auto-generate a trait for each mixin, and then impl the trait for each type that uses that mixin. So in this case it would generate a pub trait WindowOrWorkerGlobalScope {
fn atob(&self, input: &str) -> Result<String, JsValue>;
...
}
impl WindowOrWorkerGlobalScope for Window {
#[inline]
fn atob(&self, input: &str) -> Result<String, JsValue> {
// Uses Window::atob
Self::atob(input)
}
...
}
impl WindowOrWorkerGlobalScope for WorkerGlobalScope {
#[inline]
fn atob(&self, input: &str) -> Result<String, JsValue> {
// Uses WorkerGlobalScope::atob
Self::atob(input)
}
...
} I just now tested it, and it seems to work perfectly: using traits works well even when using So we can use traits for mixins, and |
To be clear: my idea is not a rehash of the trait RFC. I have no intention of resurrecting that. My idea is that we keep everything the same as it is now (inherent methods + Specifically, we add traits only for WebIDL mixins, not for class inheritance. These traits would just delegate to the inherent methods (which already exist today). This allows for Rust code to work with any type (or sub-type, thanks to Most users don't need to use the traits: they continue to use the inherent methods + But in specific situations like this (where you want the same code on both workers and the window), it is useful to use the trait. |
@Pauan your examples only include passing around such Global trait. Not exactly a solution. In practice I would need a function that produces it (that's why I've used a |
@alexcrichton if
would be enough for starters. @Pauan IMHO by default you should not care to distinguish those situations while using |
I think that traits like you mention @Pauan are definitely possible, but I think that it may be a bit too early for such an implementation/addition to web-sys. I'd prefer if we got a bit more experience using the existing APIs (fixing bugs where necessary) before adding more abstractions. It seems like we should definitely fix the bug where |
You would write a function that returns I haven't thought super hard about this, though, so there might be some limitations I'm not understanding. @alexcrichton I don't think it's quite right to call it a bug: as a user I would expect it to error, since that is standard behavior in both JavaScript and Rust. I also don't think that it's good to just return Thinking about it more, perhaps the best idea is to add a new |
@Pauan regarding instanceof check "error": currently this is the kind of "error" you can't do anything about it from rust code (it's not even like a panic, it's more like an "abort" in the native code), so yes this is obviously a bug and should be addressed as such. Here's an example: https://github.com/royaltm/rust-wasm-bindgen-window-or-worker-playground/blob/master/tests/wasm.rs#L19 As for returning The role of The following would not compile (if uncommented): https://github.com/royaltm/rust-wasm-bindgen-window-or-worker-playground/blob/master/src/lib.rs#L32
so only boxed dynamic return type works in this case ... or an |
Hi I'm attempting to load my code in a webworker and running into this. Is there a timeline for release? |
I'm going to close this since we're unlikely to add layers of abstraction in web-sys or js-sys itself, and otherwise this has been quiet for quite some time. If there's follow-up items to take care of, please feel free to open a separate issue! |
This looks pretty hacky until something is done regarding rustwasm/wasm-bindgen#1046
This looks pretty hacky until something is done regarding rustwasm/wasm-bindgen#1046
Add support different WASM environments (such as workers, NodeJS) that don't have a `window` global by binding to the global `setInterval` and `clearInterval` functions directly. This uses the same approach as used by gloo-timers implemented in [rustwasm/gloo#185](rustwasm/gloo#185) and [rustwasm/gloo#283](rustwasm/gloo#283) given the `web-sys` lack of interes to support this (see discussion in [this issue](rustwasm/wasm-bindgen#1046)). Co-authored-by: sisou <hello@soerenschwert.de>
Add support different WASM environments (such as workers, NodeJS) that don't have a `window` global. This is done by binding to the global `setInterval` and `clearInterval` functions directly. This uses the same approach used by gloo-timers implemented in [rustwasm/gloo#185](rustwasm/gloo#185) and [rustwasm/gloo#283](rustwasm/gloo#283) given the `web-sys` lack of interes to support this (see discussion in [this issue](rustwasm/wasm-bindgen#1046)). Co-authored-by: sisou <hello@soerenschwert.de>
Add support different WASM environments (such as workers, NodeJS) that don't have a `window` global. This is done by introducing a `WebContext` `enum` that abstracts and detects the `Window` vs the `WorkerGlobalScope` API. This is done due to the `web-sys` lack of interes to support this (see discussion in [this issue](rustwasm/wasm-bindgen#1046)).
Add support different WASM environments (such as workers, NodeJS) that don't have a `window` global. This is done by introducing a `WebContext` `enum` that abstracts and detects the `Window` vs the `WorkerGlobalScope` API. This is done due to the `web-sys` lack of interes to support this (see discussion in [this issue](rustwasm/wasm-bindgen#1046)).
Add support different WASM environments (such as workers, NodeJS) that don't have a `window` global. This is done by introducing a `WebContext` `enum` that abstracts and detects the `Window` vs the `WorkerGlobalScope` API. This is done due to the `web-sys` lack of interes to support this (see discussion in [this issue](rustwasm/wasm-bindgen#1046)).
Add support different WASM environments (such as workers, NodeJS) that don't have a `window` global. This is done by introducing a `WebContext` `enum` that abstracts and detects the `Window` vs the `WorkerGlobalScope` API. This is done due to the `web-sys` lack of interest to support this (see discussion in [this issue](rustwasm/wasm-bindgen#1046)).
Add support different WASM environments (such as workers, NodeJS) that don't have a `window` global. This is done by introducing a `WebContext` `enum` that abstracts and detects the `Window` vs the `WorkerGlobalScope` API. This is done due to the `web-sys` lack of interest to support this (see discussion in [this issue](rustwasm/wasm-bindgen#1046)).
Add support different WASM environments (such as workers, NodeJS) that don't have a `window` global. This is done by introducing a `WebContext` `enum` that abstracts and detects the `Window` vs the `WorkerGlobalScope` API. This is done due to the `web-sys` lack of interest to support this (see discussion in [this issue](rustwasm/wasm-bindgen#1046)).
We introduce a `WebContext` `enum` that abstracts and detects the `Window` vs the `WorkerGlobalScope` API. Related: rustwasm/wasm-bindgen#1046. Pull-Request: #4889.
Currently trying to use web_sys::window from the worker context ends up in a uncatchable error as
Window
name does not exists in a worker context and fails hard in wasm_bindgen generated js code:There is however a WorkerGlobalScope and similar structs, but they share a lot of "global" functions with the
Window
, likefetch
orcreateImageBitmapWithImageData
.The workaround solution would be to create a
GlobalProxy
enum with all those common utility methods and then implement it for each of the *GlobalScope structs.Also exposing the following self function would be very helpfull in this context, with a signature like this:
Currently my workaround solution looks like this:
so i could:
which works regardless if run in a window or a worker context.
However this is very verbose and would be great if such an auto-generated solution would be provided in web-sys. This would be very tedious for an external crate to be in-sync with web-sys.
There is a mixin webidl: https://github.com/rustwasm/wasm-bindgen/blob/master/crates/web-sys/webidls/enabled/WindowOrWorkerGlobalScope.webidl which could be used to auto-generate common global methods of GlobalProxy.
The text was updated successfully, but these errors were encountered: