Web: avoid using js_sys::Reflect (#3804)

This commit is contained in:
daxpedda
2024-07-20 20:33:21 +02:00
committed by GitHub
parent 652ff7576c
commit 88bbdb33da

View File

@@ -1,7 +1,7 @@
use std::cell::OnceCell; use std::cell::OnceCell;
use std::time::Duration; use std::time::Duration;
use js_sys::{Array, Function, Object, Promise, Reflect}; use js_sys::{Array, Function, Object, Promise};
use wasm_bindgen::closure::Closure; use wasm_bindgen::closure::Closure;
use wasm_bindgen::prelude::wasm_bindgen; use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::{JsCast, JsValue}; use wasm_bindgen::{JsCast, JsValue};
@@ -78,9 +78,9 @@ impl Schedule {
let scheduler = window.scheduler(); let scheduler = window.scheduler();
let closure = Closure::new(f); let closure = Closure::new(f);
let mut options = SchedulerPostTaskOptions::new(); let options: SchedulerPostTaskOptions = Object::new().unchecked_into();
let controller = AbortController::new().expect("Failed to create `AbortController`"); let controller = AbortController::new().expect("Failed to create `AbortController`");
options.signal(&controller.signal()); options.set_signal(&controller.signal());
if let Some(duration) = duration { if let Some(duration) = duration {
// `Duration::as_millis()` always rounds down (because of truncation), we want to round // `Duration::as_millis()` always rounds down (because of truncation), we want to round
@@ -91,7 +91,7 @@ impl Schedule {
.and_then(|secs| secs.checked_add(duration.subsec_micros().div_ceil(1000).into())) .and_then(|secs| secs.checked_add(duration.subsec_micros().div_ceil(1000).into()))
.unwrap_or(u64::MAX); .unwrap_or(u64::MAX);
options.delay(duration as f64); options.set_delay(duration as f64);
} }
thread_local! { thread_local! {
@@ -310,22 +310,10 @@ extern "C" {
) -> Promise; ) -> Promise;
type SchedulerPostTaskOptions; type SchedulerPostTaskOptions;
}
impl SchedulerPostTaskOptions { #[wasm_bindgen(method, setter, js_name = delay)]
fn new() -> Self { fn set_delay(this: &SchedulerPostTaskOptions, value: f64);
Object::new().unchecked_into()
}
fn delay(&mut self, val: f64) -> &mut Self { #[wasm_bindgen(method, setter, js_name = signal)]
let r = Reflect::set(self, &JsValue::from("delay"), &val.into()); fn set_signal(this: &SchedulerPostTaskOptions, value: &AbortSignal);
debug_assert!(r.is_ok(), "Failed to set `delay` property");
self
}
fn signal(&mut self, val: &AbortSignal) -> &mut Self {
let r = Reflect::set(self, &JsValue::from("signal"), &val.into());
debug_assert!(r.is_ok(), "Failed to set `signal` property");
self
}
} }