@zeix/cause-effect 0.17.3 → 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.
- package/.ai-context.md +163 -232
- package/.cursorrules +41 -35
- package/.github/copilot-instructions.md +166 -116
- package/ARCHITECTURE.md +274 -0
- package/CLAUDE.md +199 -143
- package/COLLECTION_REFACTORING.md +161 -0
- package/GUIDE.md +298 -0
- package/README.md +232 -197
- package/REQUIREMENTS.md +100 -0
- package/bench/reactivity.bench.ts +577 -0
- package/index.dev.js +1325 -997
- package/index.js +1 -1
- package/index.ts +58 -74
- package/package.json +4 -1
- package/src/errors.ts +118 -74
- package/src/graph.ts +601 -0
- package/src/nodes/collection.ts +474 -0
- package/src/nodes/effect.ts +149 -0
- package/src/nodes/list.ts +588 -0
- package/src/nodes/memo.ts +120 -0
- package/src/nodes/sensor.ts +139 -0
- package/src/nodes/state.ts +135 -0
- package/src/nodes/store.ts +383 -0
- package/src/nodes/task.ts +146 -0
- package/src/signal.ts +112 -66
- package/src/util.ts +26 -57
- package/test/batch.test.ts +96 -62
- package/test/benchmark.test.ts +473 -487
- package/test/collection.test.ts +466 -706
- package/test/effect.test.ts +293 -696
- package/test/list.test.ts +335 -592
- package/test/memo.test.ts +380 -0
- package/test/regression.test.ts +156 -0
- package/test/scope.test.ts +191 -0
- package/test/sensor.test.ts +454 -0
- package/test/signal.test.ts +220 -213
- package/test/state.test.ts +217 -265
- package/test/store.test.ts +346 -446
- package/test/task.test.ts +395 -0
- package/test/untrack.test.ts +167 -0
- package/types/index.d.ts +13 -15
- package/types/src/errors.d.ts +73 -17
- package/types/src/graph.d.ts +208 -0
- package/types/src/nodes/collection.d.ts +64 -0
- package/types/src/nodes/effect.d.ts +48 -0
- package/types/src/nodes/list.d.ts +65 -0
- package/types/src/nodes/memo.d.ts +57 -0
- package/types/src/nodes/sensor.d.ts +75 -0
- package/types/src/nodes/state.d.ts +78 -0
- package/types/src/nodes/store.d.ts +51 -0
- package/types/src/nodes/task.d.ts +73 -0
- package/types/src/signal.d.ts +43 -29
- package/types/src/util.d.ts +9 -16
- package/archive/benchmark.ts +0 -683
- package/archive/collection.ts +0 -253
- package/archive/composite.ts +0 -85
- package/archive/computed.ts +0 -195
- package/archive/list.ts +0 -483
- package/archive/memo.ts +0 -139
- package/archive/state.ts +0 -90
- package/archive/store.ts +0 -298
- package/archive/task.ts +0 -189
- package/src/classes/collection.ts +0 -245
- package/src/classes/computed.ts +0 -349
- package/src/classes/list.ts +0 -343
- package/src/classes/ref.ts +0 -70
- package/src/classes/state.ts +0 -102
- package/src/classes/store.ts +0 -262
- package/src/diff.ts +0 -138
- package/src/effect.ts +0 -93
- package/src/match.ts +0 -45
- package/src/resolve.ts +0 -49
- package/src/system.ts +0 -257
- package/test/computed.test.ts +0 -1108
- package/test/diff.test.ts +0 -955
- package/test/match.test.ts +0 -388
- package/test/ref.test.ts +0 -353
- package/test/resolve.test.ts +0 -154
- package/types/src/classes/collection.d.ts +0 -45
- package/types/src/classes/computed.d.ts +0 -94
- package/types/src/classes/list.d.ts +0 -43
- package/types/src/classes/ref.d.ts +0 -35
- package/types/src/classes/state.d.ts +0 -49
- package/types/src/classes/store.d.ts +0 -52
- package/types/src/diff.d.ts +0 -28
- package/types/src/effect.d.ts +0 -15
- package/types/src/match.d.ts +0 -21
- package/types/src/resolve.d.ts +0 -29
- package/types/src/system.d.ts +0 -78
|
@@ -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 };
|
package/types/src/signal.d.ts
CHANGED
|
@@ -1,51 +1,65 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
import { List } from './
|
|
3
|
-
import {
|
|
4
|
-
import { type
|
|
5
|
-
import type
|
|
6
|
-
type
|
|
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
|
-
|
|
10
|
-
type MutableSignal<T extends {}> = T extends readonly (infer U extends {})[] ? List<U> : T extends UnknownRecord ? Store<T> : State<T>;
|
|
11
|
-
type ReadonlySignal<T extends {}> = Computed<T>;
|
|
12
|
-
type UnknownSignalRecord = Record<string, UnknownSignal>;
|
|
13
|
-
type SignalValues<S extends UnknownSignalRecord> = {
|
|
14
|
-
[K in keyof S]: S[K] extends Signal<infer T> ? T : never;
|
|
9
|
+
set(value: T): void;
|
|
10
|
+
update(callback: (value: T) => T): void;
|
|
15
11
|
};
|
|
16
12
|
/**
|
|
17
|
-
*
|
|
13
|
+
* Create a derived signal from existing signals
|
|
18
14
|
*
|
|
19
15
|
* @since 0.9.0
|
|
20
|
-
* @param
|
|
21
|
-
* @
|
|
16
|
+
* @param callback - Computation callback function
|
|
17
|
+
* @param options - Optional configuration
|
|
22
18
|
*/
|
|
23
|
-
declare
|
|
24
|
-
|
|
25
|
-
* Check whether a value is a State, Store, or List
|
|
26
|
-
*
|
|
27
|
-
* @since 0.15.2
|
|
28
|
-
* @param {unknown} value - Value to check
|
|
29
|
-
* @returns {boolean} - True if value is a State, Store, or List, false otherwise
|
|
30
|
-
*/
|
|
31
|
-
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>;
|
|
32
21
|
/**
|
|
33
22
|
* Convert a value to a Signal.
|
|
34
23
|
*
|
|
35
24
|
* @since 0.9.6
|
|
36
25
|
*/
|
|
26
|
+
declare function createSignal<T extends {}>(value: Signal<T>): Signal<T>;
|
|
37
27
|
declare function createSignal<T extends {}>(value: readonly T[]): List<T>;
|
|
38
|
-
declare function createSignal<T extends {}>(value: T[]): List<T>;
|
|
39
28
|
declare function createSignal<T extends UnknownRecord>(value: T): Store<T>;
|
|
40
|
-
declare function createSignal<T extends {}>(value:
|
|
29
|
+
declare function createSignal<T extends {}>(value: TaskCallback<T>): Task<T>;
|
|
30
|
+
declare function createSignal<T extends {}>(value: MemoCallback<T>): Memo<T>;
|
|
41
31
|
declare function createSignal<T extends {}>(value: T): State<T>;
|
|
42
32
|
/**
|
|
43
33
|
* Convert a value to a MutableSignal.
|
|
44
34
|
*
|
|
45
35
|
* @since 0.17.0
|
|
46
36
|
*/
|
|
37
|
+
declare function createMutableSignal<T extends {}>(value: MutableSignal<T>): MutableSignal<T>;
|
|
47
38
|
declare function createMutableSignal<T extends {}>(value: readonly T[]): List<T>;
|
|
48
|
-
declare function createMutableSignal<T extends {}>(value: T[]): List<T>;
|
|
49
39
|
declare function createMutableSignal<T extends UnknownRecord>(value: T): Store<T>;
|
|
50
40
|
declare function createMutableSignal<T extends {}>(value: T): State<T>;
|
|
51
|
-
|
|
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, };
|
package/types/src/util.d.ts
CHANGED
|
@@ -1,17 +1,10 @@
|
|
|
1
|
-
declare
|
|
2
|
-
declare
|
|
3
|
-
declare
|
|
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)
|
|
9
|
-
declare
|
|
10
|
-
declare
|
|
11
|
-
declare
|
|
12
|
-
declare
|
|
13
|
-
|
|
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, };
|