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:
1 parent
cca9a49c51
commit
64bf747047
15 files changed
+1940
-8
No files matched your search
@@ -0,0 +1,81 @@
|
||||
// Type keys: MoonBit's replacement for GPUI's `TypeId`-keyed maps of
|
||||
// `Box<dyn Any>` (globals, events).
|
||||
//
|
||||
// A key is created once per type, usually as a top-level `let`:
|
||||
//
|
||||
// let theme_key : @app.GlobalKey[Theme] = @app.GlobalKey::new("Theme")
|
||||
//
|
||||
// The app stores values under the key's id without knowing their type. A
|
||||
// stored value is a closure that writes the value into its key's typed slot,
|
||||
// so reading it back through the same key is type-safe without downcasts.
|
||||
|
||||
///|
|
||||
let next_key_id : Ref[Int32] = { val: 0, }
|
||||
|
||||
///|
|
||||
fn fresh_key_id() -> Int32 {
|
||||
let id = next_key_id.val
|
||||
next_key_id.val = id + 1
|
||||
id
|
||||
}
|
||||
|
||||
///|
|
||||
/// Identifies one kind of global state (GPUI: `TypeId::of::<G>()` for a
|
||||
/// `G: Global`).
|
||||
pub struct GlobalKey[G] {
|
||||
id : Int32
|
||||
name : String
|
||||
priv slot : Ref[G?]
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn[G] GlobalKey::new(name : String) -> GlobalKey[G] {
|
||||
{ id: fresh_key_id(), name, slot: { val: None, }, }
|
||||
}
|
||||
|
||||
///|
|
||||
/// A type-erased value that can only be read back through `key`.
|
||||
fn[G] GlobalKey::erase(self : GlobalKey[G], value : G) -> () -> Unit {
|
||||
fn() { self.slot.val = Some(value) }
|
||||
}
|
||||
|
||||
///|
|
||||
fn[G] GlobalKey::unerase(self : GlobalKey[G], erased : () -> Unit) -> G {
|
||||
self.slot.val = None
|
||||
erased()
|
||||
guard self.slot.val is Some(value) else {
|
||||
abort("global \{self.name}: stored value has the wrong key")
|
||||
}
|
||||
self.slot.val = None
|
||||
value
|
||||
}
|
||||
|
||||
///|
|
||||
/// Identifies one kind of event (GPUI: `TypeId::of::<E>()` for events
|
||||
/// declared with `impl EventEmitter<E> for T`).
|
||||
pub struct EventKey[E] {
|
||||
id : Int32
|
||||
name : String
|
||||
priv slot : Ref[E?]
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn[E] EventKey::new(name : String) -> EventKey[E] {
|
||||
{ id: fresh_key_id(), name, slot: { val: None, }, }
|
||||
}
|
||||
|
||||
///|
|
||||
fn[E] EventKey::erase(self : EventKey[E], event : E) -> () -> Unit {
|
||||
fn() { self.slot.val = Some(event) }
|
||||
}
|
||||
|
||||
///|
|
||||
fn[E] EventKey::unerase(self : EventKey[E], erased : () -> Unit) -> E {
|
||||
self.slot.val = None
|
||||
erased()
|
||||
guard self.slot.val is Some(event) else {
|
||||
abort("event \{self.name}: payload has the wrong key")
|
||||
}
|
||||
self.slot.val = None
|
||||
event
|
||||
}
|
||||
Reference in new issue
Block a user