preact-sigma 1.0.0 → 1.0.1

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 (2) hide show
  1. package/llms.txt +74 -9
  2. package/package.json +1 -1
package/llms.txt CHANGED
@@ -41,15 +41,21 @@ It is designed to:
41
41
 
42
42
  ## Runtime Exports
43
43
 
44
- - `defineManagedState`
45
- - `useManagedState`
46
- - `useSubscribe`
47
- - `useEventTarget`
48
- - `isManagedState`
49
- - `query`
50
- - `computed`
51
- - `batch`
52
- - `untracked`
44
+ All runtime functions must be imported directly from `preact-sigma`.
45
+
46
+ ```typescript
47
+ import {
48
+ defineManagedState,
49
+ useManagedState,
50
+ useSubscribe,
51
+ useEventTarget,
52
+ isManagedState,
53
+ query,
54
+ computed,
55
+ batch,
56
+ untracked
57
+ } from 'preact-sigma';
58
+ ```
53
59
 
54
60
  ## Public Type Exports
55
61
 
@@ -76,6 +82,62 @@ It is designed to:
76
82
  - Returning a managed state instance exposes that nested managed state unchanged.
77
83
  - Keyed `get`, `peek`, and `subscribe` apply only to signal-backed public properties, not to composed managed-state properties.
78
84
 
85
+ ## Core Examples
86
+
87
+ ### Defining a Manager Class
88
+ ```typescript
89
+ import { defineManagedState, query, type StateHandle } from 'preact-sigma';
90
+
91
+ type CounterState = { count: number; name: string };
92
+ type CounterEvents = { maxReached: [] };
93
+
94
+ export const useCounterManager = defineManagedState(
95
+ (handle: StateHandle<CounterState, CounterEvents>, initialName: string) => {
96
+ // Top-level lenses
97
+ const { count, name } = handle;
98
+
99
+ return {
100
+ // Expose lenses as tracked getter properties
101
+ count,
102
+ name,
103
+
104
+ // Mutating Action (uses Immer producer under the hood)
105
+ increment() {
106
+ handle.set(draft => {
107
+ draft.count += 1;
108
+ });
109
+
110
+ if (handle.peek().count >= 10) {
111
+ handle.emit('maxReached');
112
+ }
113
+ },
114
+
115
+ // Tracked read using query()
116
+ isEven: query(() => count.get() % 2 === 0),
117
+ };
118
+ },
119
+ { count: 0, name: 'Default' } // Initial State
120
+ );
121
+ ```
122
+
123
+ ### Using in a Component
124
+ ```tsx
125
+ import { useCounterManager } from './counter-manager';
126
+
127
+ export function Counter() {
128
+ const manager = useCounterManager("My Counter");
129
+
130
+ // Tracked reads in JSX (no .value needed!)
131
+ return (
132
+ <div>
133
+ <p>{manager.name}: {manager.count}</p>
134
+ <p>Is Even: {manager.isEven() ? 'Yes' : 'No'}</p>
135
+ <button onClick={() => manager.increment()}>+1</button>
136
+ </div>
137
+ );
138
+ }
139
+ ```
140
+
79
141
  ## `defineManagedState()`
80
142
 
81
143
  Use `defineManagedState(constructor, initialState)` to create a reusable managed-state class.
@@ -245,6 +307,7 @@ Use `isManagedState(value)` to check whether a value is a managed-state instance
245
307
  - component-local managed state via `useManagedState`
246
308
  - hook-based subscriptions via `useSubscribe`
247
309
  - hook-based event subscriptions via `useEventTarget`
310
+ - async actions (await promises inside returned methods, then call `handle.set()` or `lens.set()`)
248
311
 
249
312
  ## Unsupported Or Disallowed Patterns
250
313
 
@@ -300,3 +363,5 @@ Use `isManagedState(value)` to check whether a value is a managed-state instance
300
363
  - If a returned method is wrapped with `query()`, treat it as a tracked read, not a mutating action.
301
364
  - If a constructor returns `...handle`, treat each top-level object property as a public tracked property.
302
365
  - 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.
366
+ - NEVER append `.value` when reading exposed signal-backed public properties on a manager instance (e.g., use `manager.count`, NOT `manager.count.value`). They are exposed as native getters.
367
+ - When calling `handle.set(draft => ...)` or `lens.set(draft => ...)`, treat it exactly like an Immer producer. Mutate the `draft` directly. Do not return a completely new object from the callback unless you intend to entirely overwrite that state.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "preact-sigma",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "keywords": [],
5
5
  "license": "MIT",
6
6
  "author": "Alec Larson",