M3 app model: App, Entity, Context, subscriptions, events, globals

- app: port of GPUI's App effect cycle (update / flush_effects with
  deferred subscription activation and notification dedup), EntityMap
  (typed cells, leasing, explicit release), Context<T> (notify, emit,
  observe, subscribe, observe_self/subscribe_self, on_release,
  observe_release, observe_global, defer_call), SubscriberSet and
  Subscription.
- TypeId-keyed maps become EventKey[E] / GlobalKey[G]: values cross the
  app's untyped maps as closures writing into the key's typed slot.
- No Drop: App::release replaces the last-handle drop; subscriptions end
  with unsubscribe() or when their entity is released.
- Fail loudly on re-entrant updates, reads during updates, use after
  release, cross-app use, missing globals, global_mut outside an update.
- Tests: GPUI's subscription tests ported, plus app-model and panic tests.
- examples/counter; PORTING.md M3 status, deviations, toolchain notes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
SpriteandClaude Opus 5.5 committed 2026-11-24 22:23:03 +00:00
1 parent cca9a49c51
commit 64bf747047
15 files changed
+1940 -8

No files matched your search

+53 -8
View File
@@ -135,7 +135,7 @@ stays isolated.
| `layout` | `taffy.rs` (engine on `Milky2018/chicle`, a taffy port) | pure | **done** (no cross-frame node cache yet) |
| `keymap` | `keymap.rs`, `keymap/*`, `platform/keystroke.rs` | pure | planned |
| `action` | `action.rs` | pure | planned |
| `app` | `app.rs`, `app/*`, `subscription.rs`, `global.rs`, `gpui.rs` traits | pure | planned |
| `app` | `app.rs`, `app/entity_map.rs`, `app/context.rs`, `subscription.rs`, `global.rs` | pure | **done** (no windows, executors or actions yet) |
| `executor` | `executor.rs`, `platform_scheduler.rs`, `scheduler` crate | pure core + platform hooks | planned |
| `element` | `element.rs`, `view.rs`, `elements/div.rs`, `interactive.rs` | pure | planned |
| `window` | `window.rs`, `key_dispatch.rs`, `tab_stop.rs` | pure (talks to platform traits) | planned |
@@ -352,13 +352,15 @@ gantt
- Tests: GPUI's rounding and stroke-snapping tests, plus layout
scenarios. The engine is checked separately by the 1,038-fixture
conformance suite in `conformance/chicle`.
- **M3: app model.**
- `App`, `Entity[T]`/`WeakEntity[T]`, and `Context[T]`.
- `notify`, `observe`, `subscribe`/`emit` with typed event keys, globals,
and observers.
- The effect queue and `flush_effects` semantics: deferral, and dedup of
notifications.
- Tests ported from GPUI's `app` tests.
- **M3: app model (done).**
- `App`, `Entity[T]`/`WeakEntity[T]`/`Reservation[T]`, and `Context[T]`.
- `notify`, `observe`, `subscribe`/`emit` with typed `EventKey`s,
globals with `GlobalKey`s, release listeners, and `defer_call`.
- The effect queue and `flush_effects` semantics: deferred activation of
new subscriptions, dedup of notifications, and cascading effects.
- Tests: GPUI's subscription tests are ported, plus app-model tests,
including the aborts expected on misuse.
- `src/examples/counter` shows the model end to end.
- **M4: executor.**
- Foreground executor, timers, and `Task[T]` (cancel and detach).
- `TestScheduler` with a virtual clock and `run_until_parked`.
@@ -460,6 +462,43 @@ gantt
`parent`/`children` queries. Misuse fails loudly: an unknown or stale
`LayoutId`, or a child given two parents, aborts.
### Deviations taken in M3 (app model)
- **Type keys replace `TypeId`.** Events and globals are identified by
`EventKey[E]` and `GlobalKey[G]` values, created once per type (usually a
top-level `let`). The app stores values under the key's id as closures
that write into the key's typed slot. Reading one back through the same
key is type-safe and needs no downcast.
- `EventEmitter<E>` (a compile-time check that `T` may emit `E`) has no
equivalent: any entity may emit any `EventKey`.
- **Entities are not reference counted.** A handle points at a typed cell.
`App::release(entity)` plays the role of the last handle being dropped:
- Weak handles stop upgrading at once.
- At the next flush, observers and subscriptions are removed and release
listeners run.
- Double release is a no-op.
- Use after release, re-entrant updates, reads during an update, and using
an entity with another `App` all abort, as GPUI panics.
- **Subscriptions do not end when dropped.** A `Subscription` stays active
until `unsubscribe()` is called, or until the entity it belongs to is
released. GPUI unsubscribes on drop; `detach()` is kept for parity.
- **Entity state is passed by reference.** Update callbacks receive the
state; entity types should be structs with `mut` fields (GPUI's `&mut T`).
- **Renames.**
- Entity creation is `App::new_entity`, because `App::new` creates the app.
- `cx.defer` is `defer_call`, because `defer` is a MoonBit keyword.
- `Context<T>` does not deref to `App`; use `cx.app`.
- **Top-level `App` methods open an update.** Methods such as `notify`,
`set_global`, `release` and `defer_call` flush their effects when called
outside an update.
- `global_mut` and `default_global` return the global to be mutated, so
they must be called inside `App::update`; outside one they abort.
- **Not yet ported:**
- `observe_new` (needs a per-type key for entities).
- Accessed-entity tracking and window invalidation (M6).
- `spawn` and `AsyncApp` (M4).
- Actions (M5).
- Leak detection.
## 7. Design decisions
Decided on 2026-11-24, following the recommendations below.
@@ -497,4 +536,10 @@ Decided on 2026-11-24, following the recommendations below.
Always run `python3 tools/gen_style.py && moon fmt` together.
- A value converts implicitly to a trait object (`&IntoLength`) at a call
site. That is how GPUI's `impl Into<Length>` parameters are expressed.
- `defer` is a keyword (GPUI's `cx.defer` became `defer_call`).
- Closures are not allowed to raise implicitly: `assert_*` cannot be called
inside a callback in a test. Record values and assert afterwards. A test
whose name starts with `panic` passes only if it aborts; use this to test
the fail-loudly paths.
- `Ref(x)` builds a `Ref`; `{ val: x }` needs a type annotation.
- Test-only imports go in `import { ... } for "test"` in `moon.pkg`.