▣
module · drop-in viewer
state
Reactive state container for `Z.app`. Backs the Layer 3 example in `docs/specs/ui-v3-architecture.md` — `state:default(...)`, `state.nodes`, `state:set("size", n)`, etc. Writes via the data-store path (`state.foo = bar`) or the method path (`state:set("foo", bar)`) both auto-mark…
by·posted 2mo ago
What it does
state
Reactive state container for Z.app. Backs the Layer 3 example in
docs/specs/ui-v3-architecture.md — state:default(...),
state.nodes, state:set("size", n), etc. Writes via the data-store
path (state.foo = bar) or the method path (state:set("foo", bar))
both auto-mark the owning App dirty.
Exports
State.create(initial: { [string]: any }?, app: App?) -> StateInstance— make a new container.- Instance methods (also reachable via reactive
state.foo = barwrites):state:default(initial)— write only keys not already present; idempotent. Does NOT mark dirty.state:get(key)— typed read of a data-store key.state:set(key, value)— write one key; marks dirty on change.state:update(updates)— bulk write; single dirty-mark per call.state:all()— return the raw data-store table.
Types:
App = { markDirty: ((App) -> ())? }— minimum owning-App contract.StateInstance— the metatable-bound container; supportsstate.foofield access in addition to the methods.
Usage
local State = require("@builtin::modules.zui.state")
local s = State.create({ count = 0 }, app)
s:default({ size = 10 }) -- idempotent, no dirty mark
s.nodes = { ... } -- reactive __newindex, marks dirty
s:set("count", 5) -- method path, marks dirty
Notes
- The methods table is consulted by
__indexBEFORE the data store, sostate:defaultresolves to the method even if adefaultdata key exists. :defaultis a setup-time merge and intentionally does NOT mark the App dirty — it's safe to call inside anApp.createbuilder.:updatecollapses multiple key writes into a single dirty-mark; use it in hot loops where the per-key__newindexpath would mark dirty on every assignment.
Discussion
Scoped to this part · feeds back into the world's score.
Sign in to post.sign in
No comments yet. Be the first.