-
Notifications
You must be signed in to change notification settings - Fork 999
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(websocket-websys): add support for different WASM environments
We introduce a `WebContext` `enum` that abstracts and detects the `Window` vs the `WorkerGlobalScope` API. Related: rustwasm/wasm-bindgen#1046. Pull-Request: #4889.
- Loading branch information
Showing
6 changed files
with
77 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use wasm_bindgen::{prelude::*, JsCast}; | ||
use web_sys::window; | ||
|
||
/// Web context that abstract the window vs web worker API | ||
#[derive(Debug)] | ||
pub(crate) enum WebContext { | ||
Window(web_sys::Window), | ||
Worker(web_sys::WorkerGlobalScope), | ||
} | ||
|
||
impl WebContext { | ||
pub(crate) fn new() -> Option<Self> { | ||
match window() { | ||
Some(window) => Some(Self::Window(window)), | ||
None => { | ||
#[wasm_bindgen] | ||
extern "C" { | ||
type Global; | ||
|
||
#[wasm_bindgen(method, getter, js_name = WorkerGlobalScope)] | ||
fn worker(this: &Global) -> JsValue; | ||
} | ||
let global: Global = js_sys::global().unchecked_into(); | ||
if !global.worker().is_undefined() { | ||
Some(Self::Worker(global.unchecked_into())) | ||
} else { | ||
None | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// The `setInterval()` method. | ||
pub(crate) fn set_interval_with_callback_and_timeout_and_arguments( | ||
&self, | ||
handler: &::js_sys::Function, | ||
timeout: i32, | ||
arguments: &::js_sys::Array, | ||
) -> Result<i32, JsValue> { | ||
match self { | ||
WebContext::Window(w) => { | ||
w.set_interval_with_callback_and_timeout_and_arguments(handler, timeout, arguments) | ||
} | ||
WebContext::Worker(w) => { | ||
w.set_interval_with_callback_and_timeout_and_arguments(handler, timeout, arguments) | ||
} | ||
} | ||
} | ||
|
||
/// The `clearInterval()` method. | ||
pub(crate) fn clear_interval_with_handle(&self, handle: i32) { | ||
match self { | ||
WebContext::Window(w) => w.clear_interval_with_handle(handle), | ||
WebContext::Worker(w) => w.clear_interval_with_handle(handle), | ||
} | ||
} | ||
} |