fix: remove need for inline JS for queue_microtask (#1129)

This commit is contained in:
tempbottle 2023-06-02 20:16:29 +08:00 committed by GitHub
parent 53e09279a2
commit 84e8922aa6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,8 +1,4 @@
// `queue_microtask` needs to be in its own module, which is the only thing
// in this entire framework that requires "unsafe" code (because Rust seems to
// that a `wasm_bindgen` imported function like this is unsafe)
// this is stupid, and one day hopefully web_sys will add queue_microtask itself
#![forbid(unsafe_code)]
use cfg_if::cfg_if;
cfg_if! {
@ -14,11 +10,13 @@ cfg_if! {
}
#[cfg(any(feature = "csr", feature = "hydrate"))]
#[wasm_bindgen::prelude::wasm_bindgen(
inline_js = "export function microtask(f) { queueMicrotask(f); }"
)]
extern "C" {
fn microtask(task: wasm_bindgen::JsValue);
fn microtask(task: wasm_bindgen::JsValue) {
use js_sys::{Reflect, Function};
use wasm_bindgen::prelude::*;
let window = web_sys::window().expect("window not available");
let queue_microtask = Reflect::get(&window, &JsValue::from_str("queueMicrotask")).expect("queueMicrotask not available");
let queue_microtask = queue_microtask.unchecked_into::<Function>();
let _ = queue_microtask.call0(&task);
}
} else {
/// Exposes the [`queueMicrotask`](https://developer.mozilla.org/en-US/docs/Web/API/queueMicrotask) method