M4 executors: coroutine executor, tasks, timers, test dispatcher, AsyncApp

- executor: coroutines on MoonBit's async intrinsics (%async.suspend /
  run / capture_cancellation / raise_cancellation_signal), scheduled by a
  Dispatcher trait (the platform hook) instead of core/async's event loop;
  works on wasm, wasm-gc, js and native.
- Task[T]: wait (re-raises task errors), cancel (unwinds at the next
  suspension point, running defers; waiters get TaskCancelled), ready,
  try_result, detach. Executor: spawn, timer, sleep, yield_now, now.
- TestDispatcher: deterministic FIFO queue and virtual clock with GPUI's
  advance_clock semantics (run, then step to each due timer).
- app: AsyncApp, App::spawn, Context::spawn (weak handle),
  foreground/background executors, run_until_parked / advance_clock;
  App::new() owns a TestDispatcher unless given a dispatcher.
- Tests ported/adapted from the scheduler crate; app async tests; the
  counter example gains a ticker task. Docs: M4 status and deviations.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
SpriteandClaude Opus 5.5 committed 2026-11-24 22:36:23 +00:00
1 parent 64bf747047
commit eeab51348e
16 files changed
+1108 -13

No files matched your search

+19 -1
View File
@@ -2,6 +2,7 @@
package "experiments/gpui/app"
import {
"experiments/gpui/executor",
"moonbitlang/core/debug",
}
@@ -13,14 +14,17 @@ import {
pub struct App {
// private fields
}
pub fn App::advance_clock(Self, Int32) -> Unit
pub fn App::background_executor(Self) -> @executor.Executor
pub fn[G] App::default_global(Self, GlobalKey[G], () -> G) -> G
pub fn App::defer_call(Self, (Self) -> Unit) -> Unit
pub fn App::entity_count(Self) -> Int32
pub fn App::foreground_executor(Self) -> @executor.Executor
pub fn[G] App::global(Self, GlobalKey[G]) -> G
pub fn[G] App::global_mut(Self, GlobalKey[G]) -> G
pub fn[G] App::has_global(Self, GlobalKey[G]) -> Bool
pub fn[T] App::insert_entity(Self, Reservation[T], (Context[T]) -> T) -> Entity[T]
pub fn App::new() -> Self
pub fn App::new(dispatcher? : &@executor.Dispatcher) -> Self
pub fn[T] App::new_entity(Self, (Context[T]) -> T) -> Entity[T]
pub fn App::notify(Self, EntityId) -> Unit
pub fn[T] App::observe(Self, Entity[T], (Entity[T], Self) -> Unit) -> Subscription
@@ -30,13 +34,26 @@ pub fn[T] App::read_entity(Self, Entity[T]) -> T
pub fn[T] App::release(Self, Entity[T]) -> Unit
pub fn[G] App::remove_global(Self, GlobalKey[G]) -> G
pub fn[T] App::reserve_entity(Self) -> Reservation[T]
pub fn App::run_until_parked(Self) -> Unit
pub fn[G] App::set_global(Self, GlobalKey[G], G) -> Unit
pub fn[R] App::spawn(Self, async (AsyncApp) -> R) -> @executor.Task[R]
pub fn[T, E] App::subscribe(Self, Entity[T], EventKey[E], (Entity[T], E, Self) -> Unit) -> Subscription
pub fn App::to_async(Self) -> AsyncApp
pub fn[G] App::try_global(Self, GlobalKey[G]) -> G?
pub fn[R] App::update(Self, (Self) -> R) -> R
pub fn[T, R] App::update_entity(Self, Entity[T], (T, Context[T]) -> R) -> R
pub fn[G, R] App::update_global(Self, GlobalKey[G], (G, Self) -> R) -> R
pub struct AsyncApp {
app : App
}
pub fn AsyncApp::background_executor(Self) -> @executor.Executor
pub fn AsyncApp::foreground_executor(Self) -> @executor.Executor
pub fn[T] AsyncApp::read_entity(Self, Entity[T]) -> T
pub fn[R] AsyncApp::spawn(Self, async (Self) -> R) -> @executor.Task[R]
pub fn[R] AsyncApp::update(Self, (App) -> R) -> R
pub fn[T, R] AsyncApp::update_entity(Self, Entity[T], (T, Context[T]) -> R) -> R
pub struct Context[T] {
app : App
// private fields
@@ -51,6 +68,7 @@ pub fn[T, G] Context::observe_global(Self[T], GlobalKey[G], (T, Self[T]) -> Unit
pub fn[T, W] Context::observe_release(Self[T], Entity[W], (T, W, Self[T]) -> Unit) -> Subscription
pub fn[T] Context::observe_self(Self[T], (T, Self[T]) -> Unit) -> Subscription
pub fn[T] Context::on_release(Self[T], (T, App) -> Unit) -> Subscription
pub fn[T, R] Context::spawn(Self[T], async (WeakEntity[T], AsyncApp) -> R) -> @executor.Task[R]
pub fn[T, W, E] Context::subscribe(Self[T], Entity[W], EventKey[E], (T, Entity[W], E, Self[T]) -> Unit) -> Subscription
pub fn[T, E] Context::subscribe_self(Self[T], EventKey[E], (T, E, Self[T]) -> Unit) -> Subscription
pub fn[T] Context::weak_entity(Self[T]) -> WeakEntity[T]