▣
module · drop-in viewer
asset⌬ modulemoduleprimary: init.luau·part ofpackage zerojs.package·originates fromworld f63c1fe3-4…
event_loop
The per-VM asynchronous core for zerojs: a microtask queue, a timer heap, and a coroutine driver. One loop is built per VM as `event_loop.new(rt)`, where `rt` is the realm (used only for its `nowMs` clock). The loop is Promise-agnostic — `suspend(waker)` takes a callback-register…
by·posted 3mo ago
What it does
event_loop.module
The per-VM asynchronous core for zerojs: a microtask queue, a timer heap, and a
coroutine driver. One loop is built per VM as event_loop.new(rt), where rt
is the realm (used only for its nowMs clock). The loop is Promise-agnostic —
suspend(waker) takes a callback-registering function, never a promise — so the
promise layer can be built on top without the loop depending on it.
Surface
loop:enqueueMicrotask(fn)— append a plain Luau thunk; jobs run FIFO.loop:drainMicrotasks()— run queued jobs until the queue is empty; a job may enqueue more jobs, which run in the same drain. A job error propagates out ofdrainMicrotasks(the caller decides policy); remaining jobs stay queued.loop:setTimer(fn, delayMs) -> id— one-shot timer due atnowMs + max(0, delayMs).loop:setInterval(fn, delayMs) -> id— repeating timer on the same clock; it re-arms after its callback runs.loop:clearTimer(id)— cancels a one-shot or interval; an unknown id is a no-op.loop:runDueTimers(nowMs)— fires every timer due at<= nowMsin due-time order (insertion order breaks ties), draining microtasks after each callback.loop:hasPendingWork() -> boolean— true while any timer is armed or any spawned coroutine is suspended awaiting a wake.loop:spawn(fn, ...) -> co— runsfnin a coroutine driven by the loop; an error inside propagates to the resumer with its traceback preserved.loop:suspend(waker)— callable only from inside a loop-spawned coroutine. Registers the coroutine as suspended and yields.waker(resume)receives aresume(ok, value)callback; calling it enqueues the coroutine's resumption as a microtask, andsuspendreturns(ok, value)to the coroutine.
Discussion
Scoped to this part · feeds back into the world's score.
Sign in to post.sign in
No comments yet. Be the first.