pub struct MicrotaskQueue { /* private fields */ }Expand description
The microtask queue — drained completely after each macrotask.
Like Chrome’s microtask queue in V8::MicrotaskQueue / Blink’s
EventLoop::PerformMicrotaskCheckpoint().
§Spec behavior
“If the microtask queue is not empty:
- Let oldestMicrotask be the result of dequeuing from microtask queue.
- Set event loop’s currently running task to oldestMicrotask.
- Run oldestMicrotask.
- Set event loop’s currently running task back to null.
- Go to step 1.“
Key: microtasks enqueued DURING drain are also drained in the same cycle.
Implementations§
Source§impl MicrotaskQueue
impl MicrotaskQueue
Sourcepub fn enqueue(&mut self, microtask: Microtask)
pub fn enqueue(&mut self, microtask: Microtask)
Enqueue a microtask.
If called during drain(), the new microtask
will be processed in the same drain cycle.
Sourcepub fn queue_microtask(&mut self, callback: impl FnOnce() + 'static)
pub fn queue_microtask(&mut self, callback: impl FnOnce() + 'static)
Convenience: enqueue a closure as a microtask.
Sourcepub fn drain(&mut self) -> usize
pub fn drain(&mut self) -> usize
Drain the microtask queue completely (microtask checkpoint).
Runs all microtasks, including any new ones enqueued during drain. Returns the number of microtasks executed.
§Re-entrancy
This takes microtasks one at a time via pop_front(). If a
microtask enqueues more, they appear at the back and will be
processed before drain() returns. To prevent infinite loops
from buggy user code, a safety limit is enforced.