M4: executors — coroutine tasks, timers, deterministic test dispatcher, AsyncApp (#5)

This commit was merged in pull request #5.
This commit is contained in:
nandi committed 2026-11-24 22:42:56 +00:00
1 parent 64bf747047
commit 1ae73d13c3
16 files changed
+1108 -13

No files matched your search

+17 -2
View File
@@ -1,5 +1,6 @@
// The app model (M3) without a window: two entities talking through
// notifications, typed events and a global.
// The app model (M3) and executors (M4) without a window: two entities
// talking through notifications, typed events and a global, and a task
// ticking on the app's simulated clock.
//
// Run with: moon run src/examples/counter --target js
@@ -66,6 +67,20 @@ fn report() -> String {
increment()
app.update_global(config_key, fn(config, _) { config.step = 3 })
increment()
// A task that increments every 100ms until cancelled. `App::new()` uses a
// deterministic test dispatcher, so time only moves when we advance it.
let ticker = app.spawn(async fn(cx) {
while true {
cx.background_executor().sleep(100)
cx.update_entity(counter, fn(counter, cx) {
counter.count += 1
cx.notify()
})
}
})
app.advance_clock(250)
ticker.cancel()
app.advance_clock(1000)
app.release(counter)
// Updating a released entity would abort; the history keeps working.
history.read(app).entries.join("\n")
+3 -1
View File
@@ -7,7 +7,9 @@ test "counter report" {
#|count is 2
#|milestone 5!
#|count is 5
#|counter released at 5
#|count is 6
#|count is 7
#|counter released at 7
),
)
}