@zeix/cause-effect 0.17.2 → 0.18.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 (94) hide show
  1. package/.ai-context.md +163 -226
  2. package/.cursorrules +41 -35
  3. package/.github/copilot-instructions.md +166 -116
  4. package/.zed/settings.json +3 -0
  5. package/ARCHITECTURE.md +274 -0
  6. package/CLAUDE.md +197 -202
  7. package/COLLECTION_REFACTORING.md +161 -0
  8. package/GUIDE.md +298 -0
  9. package/README.md +241 -220
  10. package/REQUIREMENTS.md +100 -0
  11. package/bench/reactivity.bench.ts +577 -0
  12. package/index.dev.js +1326 -1174
  13. package/index.js +1 -1
  14. package/index.ts +58 -85
  15. package/package.json +9 -6
  16. package/src/errors.ts +118 -70
  17. package/src/graph.ts +601 -0
  18. package/src/nodes/collection.ts +474 -0
  19. package/src/nodes/effect.ts +149 -0
  20. package/src/nodes/list.ts +588 -0
  21. package/src/nodes/memo.ts +120 -0
  22. package/src/nodes/sensor.ts +139 -0
  23. package/src/nodes/state.ts +135 -0
  24. package/src/nodes/store.ts +383 -0
  25. package/src/nodes/task.ts +146 -0
  26. package/src/signal.ts +112 -64
  27. package/src/util.ts +26 -57
  28. package/test/batch.test.ts +96 -69
  29. package/test/benchmark.test.ts +473 -485
  30. package/test/collection.test.ts +455 -955
  31. package/test/effect.test.ts +293 -696
  32. package/test/list.test.ts +332 -857
  33. package/test/memo.test.ts +380 -0
  34. package/test/regression.test.ts +156 -0
  35. package/test/scope.test.ts +191 -0
  36. package/test/sensor.test.ts +454 -0
  37. package/test/signal.test.ts +220 -213
  38. package/test/state.test.ts +217 -271
  39. package/test/store.test.ts +346 -898
  40. package/test/task.test.ts +395 -0
  41. package/test/untrack.test.ts +167 -0
  42. package/test/util/dependency-graph.ts +2 -2
  43. package/tsconfig.build.json +11 -0
  44. package/tsconfig.json +5 -7
  45. package/types/index.d.ts +13 -15
  46. package/types/src/errors.d.ts +73 -19
  47. package/types/src/graph.d.ts +208 -0
  48. package/types/src/nodes/collection.d.ts +64 -0
  49. package/types/src/nodes/effect.d.ts +48 -0
  50. package/types/src/nodes/list.d.ts +65 -0
  51. package/types/src/nodes/memo.d.ts +57 -0
  52. package/types/src/nodes/sensor.d.ts +75 -0
  53. package/types/src/nodes/state.d.ts +78 -0
  54. package/types/src/nodes/store.d.ts +51 -0
  55. package/types/src/nodes/task.d.ts +73 -0
  56. package/types/src/signal.d.ts +43 -28
  57. package/types/src/util.d.ts +9 -16
  58. package/archive/benchmark.ts +0 -688
  59. package/archive/collection.ts +0 -310
  60. package/archive/computed.ts +0 -198
  61. package/archive/list.ts +0 -544
  62. package/archive/memo.ts +0 -140
  63. package/archive/state.ts +0 -90
  64. package/archive/store.ts +0 -357
  65. package/archive/task.ts +0 -191
  66. package/src/classes/collection.ts +0 -298
  67. package/src/classes/composite.ts +0 -171
  68. package/src/classes/computed.ts +0 -392
  69. package/src/classes/list.ts +0 -310
  70. package/src/classes/ref.ts +0 -96
  71. package/src/classes/state.ts +0 -131
  72. package/src/classes/store.ts +0 -227
  73. package/src/diff.ts +0 -138
  74. package/src/effect.ts +0 -96
  75. package/src/match.ts +0 -45
  76. package/src/resolve.ts +0 -49
  77. package/src/system.ts +0 -275
  78. package/test/computed.test.ts +0 -1126
  79. package/test/diff.test.ts +0 -955
  80. package/test/match.test.ts +0 -388
  81. package/test/ref.test.ts +0 -381
  82. package/test/resolve.test.ts +0 -154
  83. package/types/src/classes/collection.d.ts +0 -47
  84. package/types/src/classes/composite.d.ts +0 -15
  85. package/types/src/classes/computed.d.ts +0 -114
  86. package/types/src/classes/list.d.ts +0 -41
  87. package/types/src/classes/ref.d.ts +0 -48
  88. package/types/src/classes/state.d.ts +0 -61
  89. package/types/src/classes/store.d.ts +0 -51
  90. package/types/src/diff.d.ts +0 -28
  91. package/types/src/effect.d.ts +0 -15
  92. package/types/src/match.d.ts +0 -21
  93. package/types/src/resolve.d.ts +0 -29
  94. package/types/src/system.d.ts +0 -81
@@ -0,0 +1,78 @@
1
+ import { type SignalOptions } from '../graph';
2
+ /**
3
+ * A callback function for states that updates a value based on the previous value.
4
+ *
5
+ * @template T - The type of value
6
+ * @param prev - The previous state value
7
+ * @returns The new state value
8
+ */
9
+ type UpdateCallback<T extends {}> = (prev: T) => T;
10
+ /**
11
+ * A mutable reactive state container.
12
+ * Changes to the state will automatically propagate to dependent computations and effects.
13
+ *
14
+ * @template T - The type of value stored in the state
15
+ */
16
+ type State<T extends {}> = {
17
+ readonly [Symbol.toStringTag]: 'State';
18
+ /**
19
+ * Gets the current value of the state.
20
+ * When called inside a memo, task, or effect, creates a dependency.
21
+ * @returns The current value
22
+ */
23
+ get(): T;
24
+ /**
25
+ * Sets a new value for the state.
26
+ * If the new value is different (according to the equality function), all dependents will be notified.
27
+ * @param next - The new value to set
28
+ */
29
+ set(next: T): void;
30
+ /**
31
+ * Updates the state with a new value computed by a callback function.
32
+ * The callback receives the current value as an argument.
33
+ * @param fn - The callback function to compute the new value
34
+ */
35
+ update(fn: UpdateCallback<T>): void;
36
+ };
37
+ /**
38
+ * Creates a mutable reactive state container.
39
+ *
40
+ * @since 0.9.0
41
+ * @template T - The type of value stored in the state
42
+ * @param value - The initial value
43
+ * @param options - Optional configuration for the state
44
+ * @returns A State object with get() and set() methods
45
+ *
46
+ * @example
47
+ * ```ts
48
+ * const count = createState(0);
49
+ * count.set(1);
50
+ * console.log(count.get()); // 1
51
+ * ```
52
+ *
53
+ * @example
54
+ * ```ts
55
+ * // With type guard
56
+ * const count = createState(0, {
57
+ * guard: (v): v is number => typeof v === 'number'
58
+ * });
59
+ * ```
60
+ */
61
+ declare function createState<T extends {}>(value: T, options?: SignalOptions<T>): State<T>;
62
+ /**
63
+ * Checks if a value is a State signal.
64
+ *
65
+ * @since 0.9.0
66
+ * @param value - The value to check
67
+ * @returns True if the value is a State
68
+ *
69
+ * @example
70
+ * ```ts
71
+ * const state = createState(0);
72
+ * if (isState(state)) {
73
+ * state.set(1); // TypeScript knows state has set()
74
+ * }
75
+ * ```
76
+ */
77
+ declare function isState<T extends {} = unknown & {}>(value: unknown): value is State<T>;
78
+ export { createState, isState, type State, type UpdateCallback };
@@ -0,0 +1,51 @@
1
+ import { type Cleanup, TYPE_STORE } from '../graph';
2
+ import { type List, type UnknownRecord } from './list';
3
+ import { type State } from './state';
4
+ type StoreOptions = {
5
+ watched?: () => Cleanup;
6
+ };
7
+ type BaseStore<T extends UnknownRecord> = {
8
+ readonly [Symbol.toStringTag]: 'Store';
9
+ readonly [Symbol.isConcatSpreadable]: false;
10
+ [Symbol.iterator](): IterableIterator<[
11
+ string,
12
+ State<T[keyof T] & {}> | Store<UnknownRecord> | List<unknown & {}>
13
+ ]>;
14
+ keys(): IterableIterator<string>;
15
+ byKey<K extends keyof T & string>(key: K): T[K] extends readonly (infer U extends {})[] ? List<U> : T[K] extends UnknownRecord ? Store<T[K]> : T[K] extends unknown & {} ? State<T[K] & {}> : State<T[K] & {}> | undefined;
16
+ get(): T;
17
+ set(newValue: T): void;
18
+ update(fn: (oldValue: T) => T): void;
19
+ add<K extends keyof T & string>(key: K, value: T[K]): K;
20
+ remove(key: string): void;
21
+ };
22
+ type Store<T extends UnknownRecord> = BaseStore<T> & {
23
+ [K in keyof T]: T[K] extends readonly (infer U extends {})[] ? List<U> : T[K] extends UnknownRecord ? Store<T[K]> : T[K] extends unknown & {} ? State<T[K] & {}> : State<T[K] & {}> | undefined;
24
+ };
25
+ /**
26
+ * Creates a reactive store with deeply nested reactive properties.
27
+ * Each property becomes its own signal (State for primitives, nested Store for objects, List for arrays).
28
+ * Properties are accessible directly via proxy.
29
+ *
30
+ * @since 0.15.0
31
+ * @param initialValue - Initial object value of the store
32
+ * @param options - Optional configuration for watch lifecycle
33
+ * @returns A Store with reactive properties
34
+ *
35
+ * @example
36
+ * ```ts
37
+ * const user = createStore({ name: 'Alice', age: 30 });
38
+ * user.name.set('Bob'); // Only name subscribers react
39
+ * console.log(user.get()); // { name: 'Bob', age: 30 }
40
+ * ```
41
+ */
42
+ declare function createStore<T extends UnknownRecord>(initialValue: T, options?: StoreOptions): Store<T>;
43
+ /**
44
+ * Checks if a value is a Store signal.
45
+ *
46
+ * @since 0.15.0
47
+ * @param value - The value to check
48
+ * @returns True if the value is a Store
49
+ */
50
+ declare function isStore<T extends UnknownRecord>(value: unknown): value is Store<T>;
51
+ export { createStore, isStore, type Store, type StoreOptions, TYPE_STORE };
@@ -0,0 +1,73 @@
1
+ import { type ComputedOptions, type TaskCallback } from '../graph';
2
+ /**
3
+ * An asynchronous reactive computation (colorless async).
4
+ * Automatically tracks dependencies and re-executes when they change.
5
+ * Provides abort semantics and pending state tracking.
6
+ *
7
+ * @template T - The type of value resolved by the task
8
+ */
9
+ type Task<T extends {}> = {
10
+ readonly [Symbol.toStringTag]: 'Task';
11
+ /**
12
+ * Gets the current value of the task.
13
+ * Returns the last resolved value, even while a new computation is pending.
14
+ * When called inside another reactive context, creates a dependency.
15
+ * @returns The current value
16
+ * @throws UnsetSignalValueError If the task value is still unset when read.
17
+ */
18
+ get(): T;
19
+ /**
20
+ * Checks if the task is currently executing.
21
+ * @returns True if a computation is in progress
22
+ */
23
+ isPending(): boolean;
24
+ /**
25
+ * Aborts the current computation if one is running.
26
+ * The task's AbortSignal will be triggered.
27
+ */
28
+ abort(): void;
29
+ };
30
+ /**
31
+ * Creates an asynchronous reactive computation (colorless async).
32
+ * The computation automatically tracks dependencies and re-executes when they change.
33
+ * Provides abort semantics - in-flight computations are aborted when dependencies change.
34
+ *
35
+ * @since 0.18.0
36
+ * @template T - The type of value resolved by the task
37
+ * @param fn - The async computation function that receives the previous value and an AbortSignal
38
+ * @param options - Optional configuration for the task
39
+ * @returns A Task object with get(), isPending(), and abort() methods
40
+ *
41
+ * @example
42
+ * ```ts
43
+ * const userId = createState(1);
44
+ * const user = createTask(async (prev, signal) => {
45
+ * const response = await fetch(`/api/users/${userId.get()}`, { signal });
46
+ * return response.json();
47
+ * });
48
+ *
49
+ * // When userId changes, the previous fetch is aborted
50
+ * userId.set(2);
51
+ * ```
52
+ *
53
+ * @example
54
+ * ```ts
55
+ * // Check pending state
56
+ * if (user.isPending()) {
57
+ * console.log('Loading...');
58
+ * }
59
+ * ```
60
+ */
61
+ declare function createTask<T extends {}>(fn: (prev: T, signal: AbortSignal) => Promise<T>, options: ComputedOptions<T> & {
62
+ value: T;
63
+ }): Task<T>;
64
+ declare function createTask<T extends {}>(fn: TaskCallback<T>, options?: ComputedOptions<T>): Task<T>;
65
+ /**
66
+ * Checks if a value is a Task signal.
67
+ *
68
+ * @since 0.18.0
69
+ * @param value - The value to check
70
+ * @returns True if the value is a Task
71
+ */
72
+ declare function isTask<T extends {} = unknown & {}>(value: unknown): value is Task<T>;
73
+ export { createTask, isTask, type Task };
@@ -1,50 +1,65 @@
1
- import { type Computed } from './classes/computed';
2
- import { List } from './classes/list';
3
- import { State } from './classes/state';
4
- import { type Store } from './classes/store';
5
- import type { UnknownRecord } from './diff';
6
- type Signal<T extends {}> = {
1
+ import { type ComputedOptions, type MemoCallback, type Signal, type TaskCallback } from './graph';
2
+ import { type List, type UnknownRecord } from './nodes/list';
3
+ import { type Memo } from './nodes/memo';
4
+ import { type State } from './nodes/state';
5
+ import { type Store } from './nodes/store';
6
+ import { type Task } from './nodes/task';
7
+ type MutableSignal<T extends {}> = {
7
8
  get(): T;
8
- };
9
- type MutableSignal<T extends {}> = T extends readonly (infer U extends {})[] ? List<U> : T extends UnknownRecord ? Store<T> : State<T>;
10
- type ReadonlySignal<T extends {}> = Computed<T>;
11
- type UnknownSignalRecord = Record<string, Signal<unknown & {}>>;
12
- type SignalValues<S extends UnknownSignalRecord> = {
13
- [K in keyof S]: S[K] extends Signal<infer T> ? T : never;
9
+ set(value: T): void;
10
+ update(callback: (value: T) => T): void;
14
11
  };
15
12
  /**
16
- * Check whether a value is a Signal
13
+ * Create a derived signal from existing signals
17
14
  *
18
15
  * @since 0.9.0
19
- * @param {unknown} value - value to check
20
- * @returns {boolean} - true if value is a Signal, false otherwise
16
+ * @param callback - Computation callback function
17
+ * @param options - Optional configuration
21
18
  */
22
- declare const isSignal: <T extends {}>(value: unknown) => value is Signal<T>;
23
- /**
24
- * Check whether a value is a State, Store, or List
25
- *
26
- * @since 0.15.2
27
- * @param {unknown} value - Value to check
28
- * @returns {boolean} - True if value is a State, Store, or List, false otherwise
29
- */
30
- declare const isMutableSignal: (value: unknown) => value is MutableSignal<unknown & {}>;
19
+ declare function createComputed<T extends {}>(callback: TaskCallback<T>, options?: ComputedOptions<T>): Task<T>;
20
+ declare function createComputed<T extends {}>(callback: MemoCallback<T>, options?: ComputedOptions<T>): Memo<T>;
31
21
  /**
32
22
  * Convert a value to a Signal.
33
23
  *
34
24
  * @since 0.9.6
35
25
  */
26
+ declare function createSignal<T extends {}>(value: Signal<T>): Signal<T>;
36
27
  declare function createSignal<T extends {}>(value: readonly T[]): List<T>;
37
- declare function createSignal<T extends {}>(value: T[]): List<T>;
38
28
  declare function createSignal<T extends UnknownRecord>(value: T): Store<T>;
39
- declare function createSignal<T extends {}>(value: () => T): Computed<T>;
29
+ declare function createSignal<T extends {}>(value: TaskCallback<T>): Task<T>;
30
+ declare function createSignal<T extends {}>(value: MemoCallback<T>): Memo<T>;
40
31
  declare function createSignal<T extends {}>(value: T): State<T>;
41
32
  /**
42
33
  * Convert a value to a MutableSignal.
43
34
  *
44
35
  * @since 0.17.0
45
36
  */
37
+ declare function createMutableSignal<T extends {}>(value: MutableSignal<T>): MutableSignal<T>;
46
38
  declare function createMutableSignal<T extends {}>(value: readonly T[]): List<T>;
47
- declare function createMutableSignal<T extends {}>(value: T[]): List<T>;
48
39
  declare function createMutableSignal<T extends UnknownRecord>(value: T): Store<T>;
49
40
  declare function createMutableSignal<T extends {}>(value: T): State<T>;
50
- export { createMutableSignal, createSignal, isMutableSignal, isSignal, type MutableSignal, type ReadonlySignal, type Signal, type SignalValues, type UnknownSignalRecord, };
41
+ /**
42
+ * Check if a value is a computed signal
43
+ *
44
+ * @since 0.9.0
45
+ * @param value - Value to check
46
+ * @returns True if value is a computed signal, false otherwise
47
+ */
48
+ declare function isComputed<T extends {}>(value: unknown): value is Memo<T>;
49
+ /**
50
+ * Check whether a value is a Signal
51
+ *
52
+ * @since 0.9.0
53
+ * @param value - Value to check
54
+ * @returns True if value is a Signal, false otherwise
55
+ */
56
+ declare function isSignal<T extends {}>(value: unknown): value is Signal<T>;
57
+ /**
58
+ * Check whether a value is a State, Store, or List
59
+ *
60
+ * @since 0.15.2
61
+ * @param value - Value to check
62
+ * @returns True if value is a State, Store, or List, false otherwise
63
+ */
64
+ declare function isMutableSignal(value: unknown): value is MutableSignal<unknown & {}>;
65
+ export { type MutableSignal, createComputed, createSignal, createMutableSignal, isComputed, isSignal, isMutableSignal, };
@@ -1,17 +1,10 @@
1
- declare const isString: (value: unknown) => value is string;
2
- declare const isNumber: (value: unknown) => value is number;
3
- declare const isSymbol: (value: unknown) => value is symbol;
4
- declare const isFunction: <T>(fn: unknown) => fn is (...args: unknown[]) => T;
5
- declare const isAsyncFunction: <T>(fn: unknown) => fn is (...args: unknown[]) => Promise<T>;
6
- declare const isSyncFunction: <T extends unknown & {
1
+ declare function isFunction<T>(fn: unknown): fn is (...args: unknown[]) => T;
2
+ declare function isAsyncFunction<T>(fn: unknown): fn is (...args: unknown[]) => Promise<T>;
3
+ declare function isSyncFunction<T extends unknown & {
7
4
  then?: undefined;
8
- }>(fn: unknown) => fn is (...args: unknown[]) => T;
9
- declare const isNonNullObject: (value: unknown) => value is NonNullable<object>;
10
- declare const isObjectOfType: <T>(value: unknown, type: string) => value is T;
11
- declare const isRecord: <T extends Record<string, unknown>>(value: unknown) => value is T;
12
- declare const isRecordOrArray: <T extends Record<string | number, unknown> | ReadonlyArray<unknown>>(value: unknown) => value is T;
13
- declare const isUniformArray: <T>(value: unknown, guard?: (item: T) => item is T & {}) => value is T[];
14
- declare const hasMethod: <T extends object & Record<string, (...args: unknown[]) => unknown>>(obj: T, methodName: string) => obj is T & Record<string, (...args: unknown[]) => unknown>;
15
- declare const isAbortError: (error: unknown) => boolean;
16
- declare const valueString: (value: unknown) => string;
17
- export { isString, isNumber, isSymbol, isFunction, isAsyncFunction, isSyncFunction, isNonNullObject, isObjectOfType, isRecord, isRecordOrArray, isUniformArray, hasMethod, isAbortError, valueString, };
5
+ }>(fn: unknown): fn is (...args: unknown[]) => T;
6
+ declare function isObjectOfType<T>(value: unknown, type: string): value is T;
7
+ declare function isRecord<T extends Record<string, unknown>>(value: unknown): value is T;
8
+ declare function isUniformArray<T>(value: unknown, guard?: (item: T) => item is T & {}): value is T[];
9
+ declare function valueString(value: unknown): string;
10
+ export { isFunction, isAsyncFunction, isSyncFunction, isObjectOfType, isRecord, isUniformArray, valueString, };