preact-sigma 1.0.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. package/README.md +113 -467
  2. package/dist/index.d.mts +207 -193
  3. package/dist/index.mjs +593 -265
  4. package/llms.txt +354 -238
  5. package/package.json +1 -1
package/llms.txt CHANGED
@@ -2,301 +2,417 @@
2
2
 
3
3
  ## Glossary
4
4
 
5
- - `base state`: The private mutable state owned by one managed state definition. It may be any non-function value, including a primitive.
6
- - `public state`: The immutable data exposed on a managed state instance. It is derived from returned signals, returned top-level lenses, returned handles, and returned nested managed states.
7
- - `managed state`: An instance created by `defineManagedState()` or returned by `useManagedState()`. It exposes immutable public data, public methods, subscriptions, events, and disposal.
8
- - `manager class`: The class returned by `defineManagedState()`. Best practice is to name it with a `Manager` suffix.
9
- - `constructor`: The function passed to `defineManagedState()` or `useManagedState()`. It receives a typed `StateHandle`.
10
- - `handle`: The first constructor parameter. Its type must be `StateHandle<...>`. It is the private control surface for reading, writing, owning resources, and emitting.
11
- - `lens`: A constructor-local object for one top-level property of an object-shaped base state. A lens has `get()` and `set()`.
12
- - `signal`: A `ReadonlySignal` from `@preact/signals`.
13
- - `signal-backed public property`: A public property backed by a returned signal, returned lens, or returned handle. Keyed `get`, `peek`, and `subscribe` target only these properties.
14
- - `derived value`: A value computed from state rather than stored directly.
15
- - `public action`: A returned method intended to change state. Returned methods are action-wrapped by default.
16
- - `query method`: A returned method wrapped with `query()`. Query methods are tracked reads and are not action-wrapped.
17
- - `event map`: A type mapping event names to zero-or-one-argument tuples, for example `{ saved: []; selected: [{ id: string }] }`.
18
- - `cleanup`: A function of type `() => void`.
19
- - `disposable`: An object with a `[Symbol.dispose]()` method.
20
- - `owned resource`: A cleanup function or disposable registered through `handle.own()`.
21
- - `object-shaped state`: A base state that is a plain object. It is not a function, array, `Map`, or `Set`.
22
- - `top-level property`: A direct property on an object-shaped base state, such as `query` in `{ query: string }`.
23
- - `composition`: Returning one managed state instance from another managed state constructor so the nested instance is exposed unchanged as a public property.
24
- - `tracked read`: A read that participates in Signals tracking.
25
- - `untracked read`: A read that does not participate in Signals tracking.
26
- - `unsubscribe`: The cleanup function returned by `.on()` and `.subscribe()`.
27
-
28
- ## Purpose
29
-
30
- `preact-sigma` is a small state-management layer built on top of `@preact/signals` and `immer`.
31
-
32
- It is designed to:
33
-
34
- - keep mutable implementation details private
35
- - expose immutable public data
36
- - express state transitions through public methods
37
- - support derived reactive values
38
- - support typed custom events
39
- - support composition of nested managed states
40
- - support instance-owned resource cleanup
5
+ - `sigma type`: The builder returned by `new SigmaType<...>()`. It is also the constructor for sigma-state instances after configuration.
6
+ - `sigma state`: An instance created from a configured sigma type.
7
+ - `state property`: A top-level property from `TState`, such as `draft` in `{ draft: string }`.
8
+ - `computed`: A tracked getter declared with `.computed({ ... })`.
9
+ - `query`: A tracked method declared with `.queries({ ... })` or created with `query(fn)`.
10
+ - `action`: A method declared with `.actions({ ... })` that reads and writes through one Immer draft for one synchronous call.
11
+ - `draft boundary`: Any point where sigma cannot keep reusing the current draft.
12
+ - `setup handler`: A function declared with `.setup(fn)` that returns an array of cleanup resources.
13
+ - `cleanup resource`: A cleanup function, an `AbortController`, or an object with `[Symbol.dispose]()`.
14
+ - `signal access`: Reading the underlying `ReadonlySignal` for a state property or computed through `instance.get(key)`.
15
+
16
+ ## Navigation
17
+
18
+ - For state shape, inference, and instance shape, read `Start Here`, `Inference`, `SigmaType`, and `Public Instance Shape`.
19
+ - For mutation semantics, read `Critical Rules`, `actions`, `immerable`, and `setAutoFreeze`.
20
+ - For side effects and events, read `setup`, `Events`, `listen`, `useListener`, and `useSigma`.
21
+ - For committed-state utilities, read `observe`, `snapshot`, and `replaceState`.
22
+
23
+ ## Start Here
24
+
25
+ - `preact-sigma` is a class-builder state API built on top of `@preact/signals` and `immer`.
26
+ - Use `new SigmaType<TState, TEvents>()` to build reusable constructors for sigma states.
27
+ - Each top-level state property gets its own signal and readonly public property.
28
+ - Use `computed` for argument-free derived state.
29
+ - Use `.queries({ ... })` for reactive reads that accept parameters.
30
+ - Put writes in `.actions({ ... })`.
31
+ - `setup` is explicit and returns cleanup resources.
32
+ - `observe` sees committed state changes after successful publishes.
33
+ - `snapshot` and `replaceState` operate on committed top-level state outside action semantics.
34
+
35
+ ## Critical Rules
36
+
37
+ - Prefer explicit type arguments only on `new SigmaType<TState, TEvents>()`. Let builder methods infer from their inputs.
38
+ - `emit()` is a draft boundary.
39
+ - Any action call is a draft boundary unless it is a same-instance sync nested action call.
40
+ - That means cross-instance action calls and all async action calls are draft boundaries.
41
+ - `await` inside an async action is also a draft boundary.
42
+ - `this.commit()` is only needed when the current action has unpublished draft changes and is about to cross a draft boundary.
43
+ - A synchronous action does not need `this.commit()` when it finishes without crossing a draft boundary.
44
+ - Successful publishes deep-freeze draftable public state while auto-freezing is enabled.
45
+ - Only values that Immer considers draftable participate in that freeze path. Custom class instances without a true `[immerable]` property stay outside it.
41
46
 
42
47
  ## Runtime Exports
43
48
 
44
- - `defineManagedState`
45
- - `useManagedState`
46
- - `useSubscribe`
47
- - `useEventTarget`
48
- - `isManagedState`
49
- - `query`
50
- - `computed`
51
- - `batch`
52
- - `untracked`
53
-
54
- ## Public Type Exports
55
-
56
- - `ManagedState`
57
- - `StateConstructor`
58
- - `StateHandle`
59
- - `Lens`
60
- - `SubscribeTarget`
61
-
62
- ## Core Rules
63
-
64
- - The first constructor parameter must be explicitly typed as `StateHandle<...>`.
65
- - The constructor should be side-effect free.
66
- - The constructor may return only:
67
- - methods
68
- - signals
69
- - top-level lenses from the provided handle
70
- - the provided handle itself
71
- - a managed state instance
72
- - Event payloads may have zero or one argument only.
73
- - Returned methods are action-wrapped automatically unless wrapped with `query()`.
74
- - Returned signals and returned top-level lenses become tracked getter properties on the public instance.
75
- - Returning the handle exposes the full base state as one reactive immutable public property.
76
- - Returning a managed state instance exposes that nested managed state unchanged.
77
- - Keyed `get`, `peek`, and `subscribe` apply only to signal-backed public properties, not to composed managed-state properties.
78
-
79
- ## `defineManagedState()`
80
-
81
- Use `defineManagedState(constructor, initialState)` to create a reusable managed-state class.
49
+ Import runtime APIs from `preact-sigma`.
50
+
51
+ ```typescript
52
+ import {
53
+ SigmaType,
54
+ action,
55
+ batch,
56
+ computed,
57
+ effect,
58
+ freeze,
59
+ immerable,
60
+ isSigmaState,
61
+ listen,
62
+ query,
63
+ replaceState,
64
+ setAutoFreeze,
65
+ snapshot,
66
+ untracked,
67
+ useListener,
68
+ useSigma,
69
+ } from "preact-sigma";
70
+ ```
71
+
72
+ ## Type Exports
73
+
74
+ - `AnyDefaultState`: Describes the object accepted by `.defaultState(...)`.
75
+ - `AnyEvents`: Describes an event map from event names to payload objects or `void`.
76
+ - `AnyResource`: Describes a supported setup cleanup resource.
77
+ - `AnySigmaState`: Describes the public shape shared by all sigma-state instances.
78
+ - `AnySigmaStateWithEvents`: Describes a sigma-state instance with a typed event map.
79
+ - `AnyState`: Describes the top-level state object for a sigma type.
80
+ - `InferEventType`: Infers the supported event names for a target used with `listen(...)` or `useListener(...)`.
81
+ - `InferListener`: Infers the listener signature for a target and event name.
82
+ - `InferSetupArgs`: Infers the `setup(...)` argument list for a sigma-state instance.
83
+ - `SigmaObserveChange`: Describes the object received by `.observe(...)` listeners.
84
+ - `SigmaObserveOptions`: Describes the options object accepted by `.observe(...)`.
85
+ - `SigmaState`: Describes the public instance shape produced by a configured sigma type.
86
+
87
+ ## Builder Example
88
+
89
+ ```typescript
90
+ import { SigmaType } from "preact-sigma";
91
+
92
+ type Todo = {
93
+ id: string;
94
+ title: string;
95
+ completed: boolean;
96
+ };
97
+
98
+ type TodoListState = {
99
+ draft: string;
100
+ todos: Todo[];
101
+ };
102
+
103
+ type TodoListEvents = {
104
+ added: Todo;
105
+ };
106
+
107
+ const TodoList = new SigmaType<TodoListState, TodoListEvents>()
108
+ .defaultState({
109
+ draft: "",
110
+ todos: [],
111
+ })
112
+ .computed({
113
+ completedCount() {
114
+ return this.todos.filter((todo) => todo.completed).length;
115
+ },
116
+ })
117
+ .queries({
118
+ canAddTodo() {
119
+ return this.draft.trim().length > 0;
120
+ },
121
+ })
122
+ .actions({
123
+ setDraft(draft: string) {
124
+ this.draft = draft;
125
+ },
126
+ addTodo() {
127
+ const todo = {
128
+ id: crypto.randomUUID(),
129
+ title: this.draft,
130
+ completed: false,
131
+ };
132
+ this.todos.push(todo);
133
+ this.draft = "";
134
+ this.commit();
135
+ this.emit("added", todo);
136
+ },
137
+ });
138
+
139
+ const todoList = new TodoList();
140
+ ```
141
+
142
+ ## Inference
143
+
144
+ - `TState` and `TEvents` come from `new SigmaType<TState, TEvents>()`.
145
+ - `defaultState` comes from `.defaultState(...)`, where each property may be either a value or a zero-argument initializer that returns the value.
146
+ - Public computed names and return types come from `.computed(...)`.
147
+ - Public query names, parameter types, and return types come from `.queries(...)`.
148
+ - `observe(change)` types come from `.observe(...)`, and `patches` and `inversePatches` are present when that call uses `{ patches: true }`.
149
+ - Public action names, parameter types, and return types come from `.actions(...)`.
150
+ - `setup(...args)` argument types come from the first `.setup(...)` call and later setup calls reuse that same argument list.
151
+ - `get(key)` signal types come from `TState` and from the return types declared by `.computed(...)`.
152
+ - Do not pass explicit type arguments to the builder methods. Let inference come from each method input.
153
+
154
+ ## `SigmaType`
155
+
156
+ `new SigmaType<TState, TEvents>()` creates a mutable, reusable sigma-state builder that is also the constructor for sigma-state instances after configuration.
82
157
 
83
158
  Behavior:
84
159
 
85
- - `initialState` is the initial base state
86
- - constructor parameters after the handle become runtime constructor parameters for the class
87
- - internal state and event types are inferred from the typed `StateHandle` parameter
88
- - the resulting instance exposes immutable public data plus public methods
89
- - the resulting instance owns any resources registered through `handle.own()`
160
+ - `.defaultState(...)`, `.setup(...)`, `.computed(...)`, `.queries(...)`, `.observe(...)`, and `.actions(...)` all mutate the same builder and return it.
161
+ - Those builder methods are additive and may be called in any order.
162
+ - Builder method typing only exposes state helpers that existed when that builder call happened.
163
+ - Runtime contexts use the full accumulated builder, including definitions added by later builder calls.
164
+ - `defaultState` is optional and must be a plain object when provided.
165
+ - Function-valued `defaultState` properties act as per-instance initializers, and their return values become the state values for that instance.
166
+ - Constructor input must be a plain object when provided.
167
+ - Constructor input shallowly overrides `defaultState`.
168
+ - If every required state property is covered by `defaultState`, constructor input is optional.
169
+ - Duplicate names across state properties, computeds, queries, and actions are rejected at runtime.
170
+ - Reserved public names are `get`, `setup`, `on`, and `emit`.
90
171
 
91
- ## `useManagedState()`
172
+ ## Public Instance Shape
92
173
 
93
- Use `useManagedState(constructor, initialState)` to create a managed state directly inside a component.
174
+ A sigma-state instance exposes:
175
+
176
+ - one readonly enumerable own property for every state property
177
+ - one tracked non-enumerable getter for every computed
178
+ - one method for every query
179
+ - one method for every action
180
+ - `get(key): ReadonlySignal<...>` for state-property and computed keys
181
+ - `setup(...args): () => void` when the builder has at least one setup handler
182
+ - `on(name, listener): () => void`
183
+ - `Object.keys(instance)` includes only top-level state properties
184
+
185
+ ## Reactivity Model
186
+
187
+ - each top-level state property is backed by its own Preact signal
188
+ - public state reads are reactive
189
+ - signal access is reactive, so reading `.value` tracks like any other Preact signal read
190
+ - computed getters are reactive and lazily memoized
191
+ - queries are reactive at the call site, including queries with arguments
192
+ - query calls are not memoized across invocations; each call uses a fresh `computed(...)` wrapper and does not retain that signal
193
+
194
+ ## `get(key)`
195
+
196
+ `instance.get(key)` returns the underlying `ReadonlySignal` for one top-level state property or computed.
94
197
 
95
198
  Behavior:
96
199
 
97
- - follows the same constructor rules as `defineManagedState()`
98
- - accepts either a concrete initial state or a lazy initializer function
99
- - returns one stable managed state instance for the component
200
+ - state-property keys return that property's signal
201
+ - computed keys return that computed getter's signal
100
202
 
101
- ## `StateHandle`
203
+ ## `computed`
102
204
 
103
- `StateHandle<TState, TEvents>` is the constructor-local control surface.
205
+ Computeds are added with `.computed({ ... })`.
104
206
 
105
- Methods:
207
+ Behavior:
106
208
 
107
- - `get()`: tracked read of the current immutable base state
108
- - `peek()`: untracked read of the current immutable base state
109
- - `set(next)`: replace the base state or update it with an Immer producer
110
- - `own(resources)`: attach cleanup functions or disposables to the managed state instance
111
- - `emit(name, arg?)`: emit a typed custom event with zero or one argument
209
+ - each computed is exposed as a tracked getter property
210
+ - computed getters are non-enumerable on the public instance
211
+ - `this` inside a computed exposes readonly state plus other computeds
212
+ - computeds do not receive query or action methods on `this`
213
+ - computeds cannot accept arguments
112
214
 
113
- For object-shaped base state only:
215
+ ## `queries`
114
216
 
115
- - each top-level property is available as a `Lens`
116
- - spreading the handle into the returned constructor object exposes all current top-level lenses as tracked public properties
217
+ Queries are added with `.queries({ ... })`.
117
218
 
118
- ## `Lens`
219
+ Behavior:
119
220
 
120
- `Lens<T>` exists only on object-shaped `StateHandle`s.
221
+ - queries may accept arbitrary parameters
222
+ - `this` inside a query exposes readonly state, computeds, and other queries
223
+ - queries do not receive action methods on `this`
224
+ - when a query runs inside an action, it reads from the current draft-aware state
225
+ - query results are reactive at the call site but are not memoized across calls
226
+ - prefer `.queries({ ... })` for commonly needed instance methods
227
+ - not every calculation belongs in `.queries({ ... })`; keeping a calculation local to the module that uses it is often clearer until it becomes a common use case
228
+ - query wrappers are shared across instances
229
+ - query typing only exposes computeds and queries that were already present when its `.queries(...)` call happened
121
230
 
122
- Methods:
231
+ ## `actions`
123
232
 
124
- - `get()`: tracked read of that top-level property
125
- - `set(next)`: replace that property or update it with an Immer producer for that property
233
+ Actions are added with `.actions({ ... })`.
126
234
 
127
- Important:
235
+ Behavior:
128
236
 
129
- - lenses are constructor-local unless returned
130
- - returning one lens exposes one reactive public property
131
- - spreading an object-shaped handle exposes all current top-level lenses at once
237
+ - actions create drafts lazily when reads or writes need draft-backed mutation semantics
238
+ - actions may call other actions, queries, and computeds
239
+ - same-instance sync nested action calls reuse the current draft
240
+ - any other action call starts a different invocation and is a draft boundary
241
+ - `emit()` is a draft boundary
242
+ - `await` inside an async action is a draft boundary
243
+ - `this.commit()` publishes the current draft immediately
244
+ - `this.commit()` is only needed when the current action has unpublished draft changes and is about to cross a draft boundary
245
+ - a synchronous action does not need `this.commit()` when it finishes without crossing a draft boundary
246
+ - declared async actions publish their initial synchronous draft on return
247
+ - after an async action resumes from `await`, top-level reads of draftable state and state writes may open a hidden draft for that async invocation
248
+ - non-async actions must stay synchronous; if one returns a promise, sigma throws
249
+ - if an async action reaches `await` or `return` with unpublished changes, the action promise rejects when it settles
250
+ - if an action crosses a boundary while it owns unpublished changes, sigma throws until `this.commit()` publishes them
251
+ - if a different invocation crosses a boundary while unpublished changes still exist, sigma warns and discards them before continuing
252
+ - successful publishes deep-freeze draftable public state and write it back to per-property signals while auto-freezing is enabled
253
+ - custom classes participate in Immer drafting only when the class opts into drafting with `[immerable] = true`
254
+ - actions can emit typed events with `this.emit(...)`
255
+ - action wrappers are shared across instances
256
+ - action typing only exposes computeds, queries, and actions that were already present when its `.actions(...)` call happened
257
+
258
+ Nested sigma states stored in state stay usable as values. Actions do not proxy direct mutation into a nested sigma state's internals.
259
+
260
+ ## `observe`
261
+
262
+ Observers are added with `.observe(listener, options?)`.
132
263
 
133
- ## `ManagedState`
264
+ Behavior:
265
+
266
+ - each observer runs after a successful action commit that changes base state
267
+ - observers do not run for actions that leave base state unchanged
268
+ - `change.newState` is the committed base-state snapshot for that action
269
+ - `change.oldState` is the base-state snapshot from before that action started
270
+ - `this` inside an observer exposes readonly state, computeds, and queries
271
+ - observers do not receive action methods or `emit(...)` on `this`
272
+ - same-instance sync nested action calls produce one observer notification after the outer action commits
273
+ - patch generation is opt-in with `{ patches: true }`
274
+ - when patch generation is enabled, `change.patches` and `change.inversePatches` come from Immer
275
+ - applications are responsible for calling `enablePatches()` before using observer patch generation
276
+ - observer typing only exposes computeds and queries that were already present when that `.observe(...)` call happened
277
+
278
+ ## `setup`
279
+
280
+ Setup is added with `.setup(fn)`.
134
281
 
135
- A managed state instance exposes:
282
+ Behavior:
136
283
 
137
- - immutable public data as normal properties
138
- - public methods returned by the constructor
139
- - subscription methods
140
- - event subscription methods
141
- - disposal methods
284
+ - setup is explicit; a new instance does not run setup automatically
285
+ - each `.setup(...)` call adds another setup handler
286
+ - `useSigma(...)` calls `.setup(...)` for component-owned instances that define setup
287
+ - calling `.setup(...)` again cleans up the previous setup first
288
+ - one `.setup(...)` call runs every registered setup handler in definition order
289
+ - the public `.setup(...)` method always returns one cleanup function
290
+ - `this` inside a setup handler exposes the public instance plus `emit(...)`
291
+ - each setup handler returns an array of cleanup resources
292
+ - setup typing only exposes computeds, queries, and actions that were already present when that `.setup(...)` call happened
142
293
 
143
- Read APIs:
294
+ Supported cleanup resources:
144
295
 
145
- - `get(key)`: return the underlying signal for one exposed signal-backed public property
146
- - `get()`: return the underlying signal for the whole public state
147
- - `peek(key)`: untracked read of one exposed signal-backed public property
148
- - `peek()`: untracked read of the whole public state
296
+ - cleanup functions
297
+ - objects with `[Symbol.dispose]()`
298
+ - `AbortController`
149
299
 
150
- Subscription APIs:
300
+ When a parent setup wants to own a nested sigma state's setup, call the child sigma state's `setup(...)` method and return that cleanup function.
151
301
 
152
- - `subscribe(key, listener)`: subscribe to the current and future values of one exposed signal-backed public property
153
- - `subscribe(listener)`: subscribe to the current and future whole public state
154
- - both forms return an unsubscribe function
302
+ Cleanup runs in reverse order. If multiple cleanup steps throw, cleanup rethrows an `AggregateError`.
155
303
 
156
- Event API:
304
+ ## Events
157
305
 
158
- - `on(name, listener)`: subscribe to one custom event
159
- - listener receives the emitted argument directly, or no argument at all
160
- - returns an unsubscribe function
306
+ Events are emitted from actions or setup through `this.emit(name, payload?)`.
161
307
 
162
- Disposal API:
308
+ Behavior:
163
309
 
164
- - `dispose()`: dispose the managed state instance and its owned resources
165
- - `[Symbol.dispose]()` does the same thing as `dispose()`
310
+ - the event map controls allowed event names and payload types
311
+ - `void` events emit no payload
312
+ - object events emit one payload object
313
+ - `.on(name, listener)` returns an unsubscribe function
314
+ - listeners receive the payload directly, or no argument for `void` events
166
315
 
167
- ## `query()`
316
+ ## `immerable`
168
317
 
169
- `query(fn)` marks a returned method as a tracked public read.
318
+ `immerable` is re-exported from Immer so custom classes can opt into drafting with `[immerable] = true`.
170
319
 
171
320
  Behavior:
172
321
 
173
- - wraps the method body in `computed()`
174
- - lets reads inside the method participate in Signals tracking after the method is exposed publicly
175
- - query functions read from closed-over handles or signals and do not use an instance receiver
176
- - skips the default `action()` wrapping step
322
+ - unmarked custom class instances stay outside Immer drafting
323
+ - marking a class with `[immerable] = true` makes that class participate in Immer drafting
324
+ - sigma only freezes published values that Immer considers draftable
325
+ - custom class instances without a true `[immerable]` property stay outside that freeze path
326
+ - plain objects, arrays, `Map`, and `Set` already participate in normal Immer drafting without extra markers
177
327
 
178
- Use `query()` for public methods that conceptually answer a question.
328
+ ## `query(fn)`
179
329
 
180
- Do not use `query()` for ordinary mutating actions.
330
+ `query(fn)` creates a standalone tracked query helper.
181
331
 
182
- ## `computed`
332
+ Behavior:
183
333
 
184
- `computed` is re-exported from `@preact/signals`.
334
+ - it returns a function with the same parameter and return types as `fn`
335
+ - it evaluates `fn` inside a signal `computed(...)`
336
+ - its calls are reactive but not memoized across invocations
337
+ - it does not use an instance receiver
338
+ - prefer `.queries({ ... })` for commonly needed instance methods
339
+ - use `query(fn)` when a tracked helper is large, rarely needed, or better kept local to a consumer module for tree-shaking
340
+ - query helpers do not need to live on the sigma state or in the same module as it
185
341
 
186
- Use it when a public derived value should be memoized and reactive.
342
+ ## `setAutoFreeze`
187
343
 
188
- ## `batch`
344
+ `setAutoFreeze(autoFreeze)` controls whether sigma deep-freezes published public state at runtime.
189
345
 
190
- `batch` is re-exported from `@preact/signals`.
346
+ Behavior:
347
+
348
+ - auto-freezing starts enabled
349
+ - `setAutoFreeze(false)` leaves later published draftable public state unfrozen
350
+ - `setAutoFreeze(true)` restores deep freezing for later published draftable state
351
+ - the setting is shared across sigma state instances
191
352
 
192
- Use it when multiple state updates should be grouped into one reactive batch.
353
+ ## `snapshot`
193
354
 
194
- ## `untracked`
355
+ `snapshot(instance)` returns a shallow snapshot of an instance's committed public state.
195
356
 
196
- `untracked` is re-exported from `@preact/signals`.
357
+ Behavior:
197
358
 
198
- Use it when code must read reactive values without subscribing to them.
359
+ - the snapshot includes one own property for each top-level state key
360
+ - each value comes from the current committed public state
361
+ - the snapshot does not include computeds, queries, actions, events, or setup helpers
362
+ - nested sigma states remain as referenced values; `snapshot(...)` does not recurse into their internal state
363
+ - the return type is inferred from the instance's sigma-state definition
199
364
 
200
- ## `useSubscribe()`
365
+ ## `replaceState`
201
366
 
202
- Use `useSubscribe(target, listener)` inside a component.
367
+ `replaceState(instance, snapshot)` replaces an instance's committed public state from a snapshot object.
203
368
 
204
369
  Behavior:
205
370
 
206
- - accepts any subscribable target, including a managed state or a Preact signal
207
- - keeps the listener fresh automatically
208
- - calls the listener immediately with the current value, then with future updates
209
- - does not take a dependency array
210
- - accepts `null` to disable the subscription temporarily
371
+ - the replacement snapshot must be a plain object with exactly the instance's top-level state keys
372
+ - it updates the committed public state without going through an action method
373
+ - it notifies observers when the committed state changes
374
+ - when observer patch generation is enabled, `replaceState(...)` also delivers patches and inverse patches
375
+ - it throws if an action still owns unpublished changes
376
+ - the snapshot parameter type is inferred from the instance's sigma-state definition
377
+
378
+ ## Passthrough Exports
379
+
380
+ - `action`, `batch`, `computed`, `effect`, and `untracked` are re-exported from `@preact/signals`.
381
+ - `freeze` is re-exported from `immer`. A frozen object cannot be mutated through Immer drafts, including inside sigma actions.
211
382
 
212
- ## `useEventTarget()`
383
+ ## `useSigma`
213
384
 
214
- Use `useEventTarget(target, name, listener)` inside a component.
385
+ `useSigma(create, setupParams?)` creates one sigma-state instance for a component and manages setup cleanup.
215
386
 
216
387
  Behavior:
217
388
 
218
- - accepts either a DOM-style `EventTarget` or a managed state
219
- - keeps the listener fresh automatically
220
- - does not take a dependency array
221
- - accepts `null` to disable the subscription temporarily
222
- - for managed-state events, the listener receives the emitted argument directly, or no argument at all
223
-
224
- ## `isManagedState()`
225
-
226
- Use `isManagedState(value)` to check whether a value is a managed-state instance.
227
-
228
- ## Supported Patterns
229
-
230
- - primitive base state
231
- - plain-object base state
232
- - Immer producer updates
233
- - lifecycle-owned cleanup via `handle.own()`
234
- - disposal via `dispose()` or `[Symbol.dispose]()`
235
- - reactive derived properties via returned signals
236
- - tracked read methods via `query()`
237
- - public exposure of one top-level property via a returned lens
238
- - public exposure of all top-level properties via `...handle`
239
- - public exposure of the whole base state via the returned handle
240
- - nested managed-state composition
241
- - keyed and whole-state signal access
242
- - keyed and whole-state snapshot access
243
- - keyed and whole-state subscriptions
244
- - typed custom events with zero or one argument
245
- - component-local managed state via `useManagedState`
246
- - hook-based subscriptions via `useSubscribe`
247
- - hook-based event subscriptions via `useEventTarget`
248
-
249
- ## Unsupported Or Disallowed Patterns
250
-
251
- - function-valued base state
252
- - returning arbitrary plain objects from the constructor unless each returned property is one of the supported public shapes
253
- - multi-argument event payloads outside a single object payload
254
- - relying on untyped constructor parameters for state or event inference
255
- - using arrays, `Map`, or `Set` as object-shaped state for lens generation
256
-
257
- ## Best Practices
258
-
259
- ### Inference And Naming
260
-
261
- - Explicitly type the first constructor parameter as `StateHandle<...>`.
262
- - Prefer a `${ModelName}State` alias near the constructor, even for simple state.
263
- - Prefer a `${ModelName}Events` alias when events exist.
264
- - Name the class returned by `defineManagedState()` with a `Manager` suffix.
265
- - Name the handle like an instance of the state model, not a generic word like `state` or `value`.
266
-
267
- ### State Shape And Exposure
268
-
269
- - Use top-level lenses for constructor-local reads and writes to top-level object fields.
270
- - Return one top-level lens when one public field should stay reactive.
271
- - Spread an object-shaped handle when the public managed state should mirror the base state's top-level shape.
272
- - Return the full handle only when the whole base state should be one public property.
273
- - Use `handle.own()` when the managed-state instance should control external cleanup.
274
-
275
- ### Derivations
276
-
277
- - Prefer plain external derivation functions for ordinary derived values so unused helpers can be tree-shaken.
278
- - Use `computed(() => derive(handle.get()))` only when a derived value needs memoized reactive reads for performance.
279
- - Use `query()` for tracked public read methods.
280
-
281
- ### Public API Design
282
-
283
- - Keep public actions domain-specific.
284
- - Prefer verbs like `save`, `submit`, `open`, `close`, and `rename` over low-level mutation names.
285
- - Avoid unnecessary binding. Public methods work through closure over the typed handle, not through `this`.
286
- - Keep nested features as separate managed states when they already have a clean public API.
287
- - Prefer explicit disposal when a managed state owns external resources.
288
-
289
- ### Events
290
-
291
- - Keep events domain-specific.
292
- - Do not use generic event names like `changed` or `updated` for ordinary state observation.
293
- - When you need multiple event fields, wrap them in one object payload.
294
-
295
- ## Example Interpretation Rules For Agents
296
-
297
- - If a snippet assigns `const stop = thing.on(...)`, `stop` is an unsubscribe function.
298
- - If a snippet assigns `const stop = thing.subscribe(...)`, `stop` is an unsubscribe function.
299
- - If a snippet calls `handle.own([...])`, those resources are released by `instance.dispose()` or `instance[Symbol.dispose]()`.
300
- - If a returned method is wrapped with `query()`, treat it as a tracked read, not a mutating action.
301
- - If a constructor returns `...handle`, treat each top-level object property as a public tracked property.
302
- - If a constructor returns `field: handle.someField`, treat `field` as a public tracked property whose value is the lens's current value, not as the lens object itself.
389
+ - calls `create()` once per mounted component instance
390
+ - returns the same sigma-state instance for the component lifetime
391
+ - if the sigma state defines setup, calls `sigmaState.setup(...setupParams)` in an effect
392
+ - reruns setup when `setupParams` change
393
+ - the cleanup returned by `setup(...)` runs when `setupParams` change or when the component unmounts
394
+
395
+ ## `listen`
396
+
397
+ `listen(target, name, listener)` adds an event listener and returns a cleanup function.
398
+
399
+ Behavior:
400
+
401
+ - it subscribes with `addEventListener(...)` and returns a cleanup function that removes that listener
402
+ - for sigma-state targets, the listener receives the typed payload directly
403
+ - for DOM targets, the listener receives the typed DOM event object
404
+
405
+ ## `useListener`
406
+
407
+ `useListener(target, name, listener)` attaches an event listener inside a component.
408
+
409
+ Behavior:
410
+
411
+ - subscribes in `useEffect`
412
+ - unsubscribes automatically when `target` or `name` changes or when the component unmounts
413
+ - keeps the latest listener callback without requiring it in the effect dependency list
414
+ - passing `null` disables the listener
415
+
416
+ ## `isSigmaState`
417
+
418
+ `isSigmaState(value)` checks whether a value is a sigma-state instance.