M3: app model — App, Entity, Context, typed events and globals #4

Merged
nandi merged 1 commits from feat/app into feat/layout 2026-11-24 22:27:49 +00:00
Owner

Stacked on #3 (base feat/layout); the diff shows only M3. Retarget to main once #1–#3 are merged.

What's here

  • app, a port of gpui's app model (app.rs, app/entity_map.rs, app/context.rs, subscription.rs, global.rs):
    • Effect cycle. App::update flushes effects when the outermost update returns. New subscriptions activate in deferred order, notifications are deduplicated, and effects cascade within a single flush.
    • Entities. Entity[T], WeakEntity[T] and Reservation[T], with new_entity, reserve_entity/insert_entity, update_entity, read_entity and release.
    • Context[T]. notify, emit, observe, subscribe, observe_self, subscribe_self, on_release, observe_release, observe_global and defer_call.
    • Globals. set_global, global, try_global, global_mut, default_global, update_global (which holds the global for the duration of the update), remove_global and observe_global.
    • Subscriptions. SubscriberSet, and Subscription with unsubscribe, detach and join.
  • src/examples/counter, a runnable example of the model. Its output is snapshot-tested.
  • PORTING.md: M3 status, M3 deviations (§6) and new toolchain notes (§8).

Design: replacing what Rust gives gpui

  • TypeId/dyn Any become EventKey[E] and GlobalKey[G]. A key is created once per type, usually as a top-level let. Values cross the app's untyped maps as closures that write into the key's typed slot, so values are read back through the same key without any downcast.
  • Drop becomes App::release(entity). Weak handles stop upgrading immediately. At the next flush, the entity's observers and subscriptions are removed and its release listeners run.
  • A Subscription stays active until unsubscribe() is called or its entity is released. In gpui, dropping a subscription unsubscribes it.
  • Misuse fails loudly, matching where gpui panics: a re-entrant update, a read during an update, use after release, use with another App, a missing global, reading a global during update_global, and global_mut called outside an update.
  • Renames:
    • App::new creates the app, so creating an entity is new_entity.
    • defer is a MoonBit keyword, so cx.defer is defer_call.
    • Context has an app field instead of dereferencing to App.

Testing

moon check --target all and moon test --target all pass: 106 tests on each of the four backends.

  • gpui's subscription tests are ported (immediate and pending insertions, activation after a drop during retain, unsubscribing during a callback while inserting, a callback dropped by an earlier one, self-drop, drop).
  • New tests cover entity creation, reading and updating; coalesced notifications; nested updates; deferred activation; typed event delivery; entity-to-entity observe/subscribe/release; cascading effects; release semantics; globals; and defer_call.
  • Seven tests check that misuse aborts.

Not yet ported

  • observe_new.
  • Accessed-entity tracking and view invalidation (M6).
  • spawn and AsyncApp (M4).
  • Actions (M5).
  • Leak detection.

🤖 Generated with Claude Code

Stacked on #3 (base `feat/layout`); the diff shows only M3. Retarget to `main` once #1–#3 are merged. ## What's here - **`app`**, a port of gpui's app model (`app.rs`, `app/entity_map.rs`, `app/context.rs`, `subscription.rs`, `global.rs`): - **Effect cycle.** `App::update` flushes effects when the outermost update returns. New subscriptions activate in deferred order, notifications are deduplicated, and effects cascade within a single flush. - **Entities.** `Entity[T]`, `WeakEntity[T]` and `Reservation[T]`, with `new_entity`, `reserve_entity`/`insert_entity`, `update_entity`, `read_entity` and `release`. - **`Context[T]`.** `notify`, `emit`, `observe`, `subscribe`, `observe_self`, `subscribe_self`, `on_release`, `observe_release`, `observe_global` and `defer_call`. - **Globals.** `set_global`, `global`, `try_global`, `global_mut`, `default_global`, `update_global` (which holds the global for the duration of the update), `remove_global` and `observe_global`. - **Subscriptions.** `SubscriberSet`, and `Subscription` with `unsubscribe`, `detach` and `join`. - **`src/examples/counter`**, a runnable example of the model. Its output is snapshot-tested. - **PORTING.md**: M3 status, M3 deviations (§6) and new toolchain notes (§8). ## Design: replacing what Rust gives gpui - **`TypeId`/`dyn Any` become `EventKey[E]` and `GlobalKey[G]`.** A key is created once per type, usually as a top-level `let`. Values cross the app's untyped maps as closures that write into the key's typed slot, so values are read back through the same key without any downcast. - **`Drop` becomes `App::release(entity)`.** Weak handles stop upgrading immediately. At the next flush, the entity's observers and subscriptions are removed and its release listeners run. - **A `Subscription` stays active until `unsubscribe()` is called or its entity is released.** In gpui, dropping a subscription unsubscribes it. - **Misuse fails loudly**, matching where gpui panics: a re-entrant update, a read during an update, use after release, use with another `App`, a missing global, reading a global during `update_global`, and `global_mut` called outside an update. - **Renames:** - `App::new` creates the app, so creating an entity is `new_entity`. - `defer` is a MoonBit keyword, so `cx.defer` is `defer_call`. - `Context` has an `app` field instead of dereferencing to `App`. ## Testing `moon check --target all` and `moon test --target all` pass: 106 tests on each of the four backends. - gpui's subscription tests are ported (immediate and pending insertions, activation after a drop during `retain`, unsubscribing during a callback while inserting, a callback dropped by an earlier one, self-drop, drop). - New tests cover entity creation, reading and updating; coalesced notifications; nested updates; deferred activation; typed event delivery; entity-to-entity observe/subscribe/release; cascading effects; release semantics; globals; and `defer_call`. - Seven tests check that misuse aborts. ## Not yet ported - `observe_new`. - Accessed-entity tracking and view invalidation (M6). - `spawn` and `AsyncApp` (M4). - Actions (M5). - Leak detection. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
nandi added 1 commit 2026-11-24 22:23:16 +00:00
- 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>
nandi merged commit 2a4aed1849 into feat/layout 2026-11-24 22:27:49 +00:00
Sign in to join this conversation.
No Reviewers
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: experiments/moon-gpui#4