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,77 @@
|
||||
// The app model (M3) without a window: two entities talking through
|
||||
// notifications, typed events and a global.
|
||||
//
|
||||
// Run with: moon run src/examples/counter --target js
|
||||
|
||||
///|
|
||||
struct Counter {
|
||||
mut count : Int32
|
||||
}
|
||||
|
||||
///|
|
||||
struct History {
|
||||
entries : Array[String]
|
||||
}
|
||||
|
||||
///|
|
||||
/// An event a counter emits when it passes a threshold.
|
||||
let milestone : @app.EventKey[Int32] = @app.EventKey::new("Milestone")
|
||||
|
||||
///|
|
||||
/// App-wide configuration.
|
||||
struct Config {
|
||||
mut step : Int32
|
||||
}
|
||||
|
||||
///|
|
||||
let config_key : @app.GlobalKey[Config] = @app.GlobalKey::new("Config")
|
||||
|
||||
///|
|
||||
fn report() -> String {
|
||||
let app = @app.App::new()
|
||||
app.set_global(config_key, { step: 1, })
|
||||
let counter : @app.Entity[Counter] = app.new_entity(fn(_) { { count: 0, } })
|
||||
// The history observes the counter and subscribes to its milestones; both
|
||||
// stop automatically when either entity is released.
|
||||
let history : @app.Entity[History] = app.new_entity(fn(cx) {
|
||||
cx
|
||||
.observe(counter, fn(history, counter, cx) {
|
||||
history.entries.push("count is \{counter.read(cx.app).count}")
|
||||
})
|
||||
.detach()
|
||||
cx
|
||||
.subscribe(counter, milestone, fn(history, _, value, _) {
|
||||
history.entries.push("milestone \{value}!")
|
||||
})
|
||||
.detach()
|
||||
cx
|
||||
.observe_release(counter, fn(history, counter, _) {
|
||||
history.entries.push("counter released at \{counter.count}")
|
||||
})
|
||||
.detach()
|
||||
{ entries: [], }
|
||||
})
|
||||
let increment = fn() {
|
||||
counter.update(app, fn(counter, cx) {
|
||||
counter.count += cx.app.global(config_key).step
|
||||
if counter.count % 5 == 0 {
|
||||
cx.emit(milestone, counter.count)
|
||||
}
|
||||
// Notifying twice in one update still runs observers once.
|
||||
cx.notify()
|
||||
cx.notify()
|
||||
})
|
||||
}
|
||||
increment()
|
||||
increment()
|
||||
app.update_global(config_key, fn(config, _) { config.step = 3 })
|
||||
increment()
|
||||
app.release(counter)
|
||||
// Updating a released entity would abort; the history keeps working.
|
||||
history.read(app).entries.join("\n")
|
||||
}
|
||||
|
||||
///|
|
||||
fn main {
|
||||
println(report())
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
///|
|
||||
test "counter report" {
|
||||
inspect(
|
||||
report(),
|
||||
content=(
|
||||
#|count is 1
|
||||
#|count is 2
|
||||
#|milestone 5!
|
||||
#|count is 5
|
||||
#|counter released at 5
|
||||
),
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import {
|
||||
"experiments/gpui/app",
|
||||
}
|
||||
|
||||
pkgtype(kind: "executable")
|
||||
@@ -0,0 +1,17 @@
|
||||
// Generated using `moon info`, DON'T EDIT IT
|
||||
package "experiments/gpui/examples/counter"
|
||||
|
||||
// Values
|
||||
|
||||
// Errors
|
||||
|
||||
// Types and methods
|
||||
type Config
|
||||
|
||||
type Counter
|
||||
|
||||
type History
|
||||
|
||||
// Type aliases
|
||||
|
||||
// Traits
|
||||
Reference in new issue
Block a user