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.
- package/README.md +113 -467
- package/dist/index.d.mts +207 -193
- package/dist/index.mjs +593 -265
- package/llms.txt +354 -238
- package/package.json +1 -1
package/llms.txt
CHANGED
|
@@ -2,301 +2,417 @@
|
|
|
2
2
|
|
|
3
3
|
## Glossary
|
|
4
4
|
|
|
5
|
-
- `
|
|
6
|
-
- `
|
|
7
|
-
- `
|
|
8
|
-
- `
|
|
9
|
-
- `
|
|
10
|
-
- `
|
|
11
|
-
- `
|
|
12
|
-
- `
|
|
13
|
-
- `
|
|
14
|
-
- `
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
- `
|
|
19
|
-
-
|
|
20
|
-
-
|
|
21
|
-
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
- `
|
|
26
|
-
- `
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
-
|
|
38
|
-
-
|
|
39
|
-
-
|
|
40
|
-
-
|
|
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
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
-
|
|
73
|
-
-
|
|
74
|
-
-
|
|
75
|
-
-
|
|
76
|
-
-
|
|
77
|
-
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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
|
-
- `
|
|
86
|
-
-
|
|
87
|
-
-
|
|
88
|
-
- the
|
|
89
|
-
-
|
|
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
|
-
##
|
|
172
|
+
## Public Instance Shape
|
|
92
173
|
|
|
93
|
-
|
|
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
|
-
-
|
|
98
|
-
-
|
|
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
|
-
## `
|
|
203
|
+
## `computed`
|
|
102
204
|
|
|
103
|
-
|
|
205
|
+
Computeds are added with `.computed({ ... })`.
|
|
104
206
|
|
|
105
|
-
|
|
207
|
+
Behavior:
|
|
106
208
|
|
|
107
|
-
-
|
|
108
|
-
-
|
|
109
|
-
- `
|
|
110
|
-
-
|
|
111
|
-
-
|
|
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
|
-
|
|
215
|
+
## `queries`
|
|
114
216
|
|
|
115
|
-
|
|
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
|
-
|
|
219
|
+
Behavior:
|
|
119
220
|
|
|
120
|
-
|
|
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
|
-
|
|
231
|
+
## `actions`
|
|
123
232
|
|
|
124
|
-
|
|
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
|
-
|
|
235
|
+
Behavior:
|
|
128
236
|
|
|
129
|
-
-
|
|
130
|
-
-
|
|
131
|
-
-
|
|
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
|
-
|
|
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
|
-
|
|
282
|
+
Behavior:
|
|
136
283
|
|
|
137
|
-
-
|
|
138
|
-
-
|
|
139
|
-
-
|
|
140
|
-
-
|
|
141
|
-
-
|
|
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
|
-
|
|
294
|
+
Supported cleanup resources:
|
|
144
295
|
|
|
145
|
-
-
|
|
146
|
-
- `
|
|
147
|
-
- `
|
|
148
|
-
- `peek()`: untracked read of the whole public state
|
|
296
|
+
- cleanup functions
|
|
297
|
+
- objects with `[Symbol.dispose]()`
|
|
298
|
+
- `AbortController`
|
|
149
299
|
|
|
150
|
-
|
|
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
|
-
|
|
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
|
-
|
|
304
|
+
## Events
|
|
157
305
|
|
|
158
|
-
|
|
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
|
-
|
|
308
|
+
Behavior:
|
|
163
309
|
|
|
164
|
-
-
|
|
165
|
-
- `
|
|
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
|
-
## `
|
|
316
|
+
## `immerable`
|
|
168
317
|
|
|
169
|
-
`
|
|
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
|
-
-
|
|
174
|
-
-
|
|
175
|
-
-
|
|
176
|
-
-
|
|
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
|
-
|
|
328
|
+
## `query(fn)`
|
|
179
329
|
|
|
180
|
-
|
|
330
|
+
`query(fn)` creates a standalone tracked query helper.
|
|
181
331
|
|
|
182
|
-
|
|
332
|
+
Behavior:
|
|
183
333
|
|
|
184
|
-
|
|
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
|
-
|
|
342
|
+
## `setAutoFreeze`
|
|
187
343
|
|
|
188
|
-
|
|
344
|
+
`setAutoFreeze(autoFreeze)` controls whether sigma deep-freezes published public state at runtime.
|
|
189
345
|
|
|
190
|
-
|
|
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
|
-
|
|
353
|
+
## `snapshot`
|
|
193
354
|
|
|
194
|
-
|
|
355
|
+
`snapshot(instance)` returns a shallow snapshot of an instance's committed public state.
|
|
195
356
|
|
|
196
|
-
|
|
357
|
+
Behavior:
|
|
197
358
|
|
|
198
|
-
|
|
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
|
-
## `
|
|
365
|
+
## `replaceState`
|
|
201
366
|
|
|
202
|
-
|
|
367
|
+
`replaceState(instance, snapshot)` replaces an instance's committed public state from a snapshot object.
|
|
203
368
|
|
|
204
369
|
Behavior:
|
|
205
370
|
|
|
206
|
-
-
|
|
207
|
-
-
|
|
208
|
-
-
|
|
209
|
-
-
|
|
210
|
-
-
|
|
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
|
-
## `
|
|
383
|
+
## `useSigma`
|
|
213
384
|
|
|
214
|
-
|
|
385
|
+
`useSigma(create, setupParams?)` creates one sigma-state instance for a component and manages setup cleanup.
|
|
215
386
|
|
|
216
387
|
Behavior:
|
|
217
388
|
|
|
218
|
-
-
|
|
219
|
-
-
|
|
220
|
-
-
|
|
221
|
-
-
|
|
222
|
-
-
|
|
223
|
-
|
|
224
|
-
## `
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
-
|
|
231
|
-
-
|
|
232
|
-
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
-
|
|
241
|
-
-
|
|
242
|
-
-
|
|
243
|
-
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
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.
|