|
1 | | -# Cause & Effect - Reactive State Management Library |
| 1 | +# Cause & Effect - Reactive State Management Primitives |
2 | 2 |
|
3 | 3 | ## Project Type |
4 | | -TypeScript/JavaScript reactive state management library using signals pattern |
| 4 | +TypeScript reactive state management primitives library using a unified signal graph |
5 | 5 |
|
6 | 6 | ## Core Concepts |
7 | | -- Signals: Base reactive primitives with .get() method |
8 | | -- State: new State() for primitives/simple values |
9 | | -- Ref: new Ref() for external objects (DOM, Map, Set) requiring manual .notify() |
10 | | -- Computed: new Memo() for derived/memoized values and new Task() for asynchronous computations |
11 | | -- Store: createStore() for objects with reactive properties |
12 | | -- List: new List() for arrays with stable keys and reactive items |
13 | | -- Collection: Interface for reactive collections (DerivedCollection, ElementCollection) |
14 | | -- Effects: createEffect() for side effects |
| 7 | +- Signals: Reactive primitives with .get() method for dependency tracking |
| 8 | +- State: createState() for mutable source values |
| 9 | +- Sensor: createSensor() for external input with lazy lifecycle |
| 10 | +- Memo: createMemo() for synchronous memoized derivations |
| 11 | +- Task: createTask() for asynchronous derivations with cancellation |
| 12 | +- Store: createStore() for reactive objects with per-property signals |
| 13 | +- List: createList() for reactive arrays with stable keys |
| 14 | +- Collection: createCollection() for reactive collections (external source or derived, item-level memoization) |
| 15 | +- Effect: createEffect() for side-effect sinks |
15 | 16 |
|
16 | 17 | ## Code Style |
17 | 18 | - Use const for immutable values |
18 | 19 | - Generic constraints: T extends {} (excludes null/undefined) |
19 | 20 | - Factory functions: create* prefix |
20 | | -- Type predicates: is* prefix |
| 21 | +- Type predicates: is* prefix |
21 | 22 | - Constants: TYPE_* or UPPER_CASE |
22 | 23 | - Pure functions marked with /*#__PURE__*/ comment |
23 | 24 |
|
24 | 25 | ## Key Patterns |
25 | 26 | - All signals have .get() method |
26 | 27 | - Mutable signals have .set(value) and .update(fn) |
27 | | -- Store properties auto-become reactive signals |
28 | | -- Computed callbacks receive oldValue as first parameter for reducer patterns |
29 | | -- Async computed and effect callbacks receive AbortSignal for async cancellation |
30 | | -- Use UNSET symbol for uninitialized states |
31 | | -- Batch updates for performance |
32 | | -- JSDoc comments on all public APIs |
| 28 | +- Store properties auto-become reactive signals via Proxy |
| 29 | +- Memo callbacks receive previous value as first parameter for reducer patterns |
| 30 | +- Task callbacks receive (next, AbortSignal) |
| 31 | +- Sensor and Collection use start callbacks for lazy resource management |
| 32 | +- Store and List support optional watched callbacks |
| 33 | +- batch() for grouping multiple updates |
| 34 | +- untrack() for reading signals without creating edges |
33 | 35 |
|
34 | 36 | ## File Structure |
35 | | -- src/signal.ts - Base signal types |
36 | | -- src/classes/state.ts - Mutable state signals |
37 | | -- src/classes/ref.ts - Signal wrappers for external objects |
38 | | -- src/classes/store.ts - Object stores |
39 | | -- src/classes/list.ts - Array stores with stable keys |
40 | | -- src/classes/collection.ts - Collection interface and DerivedCollection |
41 | | -- src/classes/computed.ts - Computed signals |
42 | | -- src/effect.ts - Effect system |
43 | | -- src/system.ts - Core reactivity (watchers, batching) |
44 | | -- index.ts - Main exports |
45 | | - |
46 | | -## Error Handling |
| 37 | +- src/graph.ts - Core reactive engine (nodes, edges, propagation, flush) |
| 38 | +- src/nodes/state.ts - Mutable state signals |
| 39 | +- src/nodes/sensor.ts - External input tracking |
| 40 | +- src/nodes/memo.ts - Synchronous derived signals |
| 41 | +- src/nodes/task.ts - Asynchronous derived signals |
| 42 | +- src/nodes/effect.ts - Side-effect system |
| 43 | +- src/nodes/store.ts - Reactive objects |
| 44 | +- src/nodes/list.ts - Reactive arrays with stable keys |
| 45 | +- src/nodes/collection.ts - Externally-driven and derived collections |
| 46 | +- next.ts - Public API exports |
| 47 | + |
| 48 | +## Error Handling |
47 | 49 | - Custom error classes in src/errors.ts |
48 | | -- Validate inputs, throw descriptive errors |
49 | | -- Support AbortSignal in async operations |
| 50 | +- Validate inputs with validateSignalValue() and validateCallback() |
| 51 | +- AbortSignal integration for async cancellation in Task and Collection |
50 | 52 |
|
51 | 53 | ## Performance |
52 | | -- Use Set<Watcher> for subscriptions |
53 | | -- Shallow equality with isEqual() utility |
54 | | -- Tree-shaking friendly code |
55 | | -- Minimize effect re-runs |
| 54 | +- Linked-list edges for O(1) dependency tracking |
| 55 | +- Flag-based dirty checking (CLEAN, CHECK, DIRTY) |
| 56 | +- Tree-shaking friendly, zero dependencies |
| 57 | +- Minimize effect re-runs via equality checks |
| 58 | + |
| 59 | +## Testing |
| 60 | +- bun:test with describe/test/expect |
| 61 | +- Test files: test/*.test.ts |
56 | 62 |
|
57 | 63 | ## Build |
58 | 64 | - Bun build tool and runtime |
|
0 commit comments