wasm_bindgen_futures/
queue.rsuse alloc::collections::VecDeque;
use alloc::rc::Rc;
use core::cell::{Cell, RefCell};
use js_sys::Promise;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen]
fn queueMicrotask(closure: &Closure<dyn FnMut(JsValue)>);
type Global;
#[wasm_bindgen(method, getter, js_name = queueMicrotask)]
fn hasQueueMicrotask(this: &Global) -> JsValue;
}
struct QueueState {
tasks: RefCell<VecDeque<Rc<crate::task::Task>>>,
is_scheduled: Cell<bool>,
}
impl QueueState {
fn run_all(&self) {
let _was_scheduled = self.is_scheduled.replace(false);
debug_assert!(_was_scheduled);
let mut task_count_left = self.tasks.borrow().len();
while task_count_left > 0 {
task_count_left -= 1;
let task = match self.tasks.borrow_mut().pop_front() {
Some(task) => task,
None => break,
};
task.run();
}
}
}
pub(crate) struct Queue {
state: Rc<QueueState>,
promise: Promise,
closure: Closure<dyn FnMut(JsValue)>,
has_queue_microtask: bool,
}
impl Queue {
pub(crate) fn schedule_task(&self, task: Rc<crate::task::Task>) {
self.state.tasks.borrow_mut().push_back(task);
if !self.state.is_scheduled.replace(true) {
if self.has_queue_microtask {
queueMicrotask(&self.closure);
} else {
let _ = self.promise.then(&self.closure);
}
}
}
#[cfg(not(target_feature = "atomics"))]
pub(crate) fn push_task(&self, task: Rc<crate::task::Task>) {
self.schedule_task(task)
}
}
impl Queue {
fn new() -> Self {
let state = Rc::new(QueueState {
is_scheduled: Cell::new(false),
tasks: RefCell::new(VecDeque::new()),
});
let has_queue_microtask = js_sys::global()
.unchecked_into::<Global>()
.hasQueueMicrotask()
.is_function();
Self {
promise: Promise::resolve(&JsValue::undefined()),
closure: {
let state = Rc::clone(&state);
Closure::new(move |_| state.run_all())
},
state,
has_queue_microtask,
}
}
pub(crate) fn with<R>(f: impl FnOnce(&Self) -> R) -> R {
use once_cell::unsync::Lazy;
struct Wrapper<T>(Lazy<T>);
#[cfg(not(target_feature = "atomics"))]
unsafe impl<T> Sync for Wrapper<T> {}
#[cfg(not(target_feature = "atomics"))]
unsafe impl<T> Send for Wrapper<T> {}
#[cfg_attr(target_feature = "atomics", thread_local)]
static QUEUE: Wrapper<Queue> = Wrapper(Lazy::new(Queue::new));
f(&QUEUE.0)
}
}