@but212/atom-effect 0.1.4 → 0.2.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 (68) hide show
  1. package/README.md +8 -8
  2. package/dist/index.cjs +1 -1
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.d.ts +606 -12
  5. package/dist/index.mjs +322 -500
  6. package/dist/index.mjs.map +1 -1
  7. package/package.json +4 -4
  8. package/dist/constants.d.ts +0 -64
  9. package/dist/constants.d.ts.map +0 -1
  10. package/dist/core/atom/atom.d.ts +0 -25
  11. package/dist/core/atom/atom.d.ts.map +0 -1
  12. package/dist/core/atom/index.d.ts +0 -6
  13. package/dist/core/atom/index.d.ts.map +0 -1
  14. package/dist/core/computed/computed-async-handler.d.ts +0 -237
  15. package/dist/core/computed/computed-async-handler.d.ts.map +0 -1
  16. package/dist/core/computed/computed-dependencies.d.ts +0 -173
  17. package/dist/core/computed/computed-dependencies.d.ts.map +0 -1
  18. package/dist/core/computed/computed-handlers.d.ts +0 -285
  19. package/dist/core/computed/computed-handlers.d.ts.map +0 -1
  20. package/dist/core/computed/computed-state-flags.d.ts +0 -335
  21. package/dist/core/computed/computed-state-flags.d.ts.map +0 -1
  22. package/dist/core/computed/index.d.ts +0 -35
  23. package/dist/core/computed/index.d.ts.map +0 -1
  24. package/dist/core/effect/effect.d.ts +0 -64
  25. package/dist/core/effect/effect.d.ts.map +0 -1
  26. package/dist/core/effect/index.d.ts +0 -2
  27. package/dist/core/effect/index.d.ts.map +0 -1
  28. package/dist/core/index.d.ts +0 -4
  29. package/dist/core/index.d.ts.map +0 -1
  30. package/dist/errors/errors.d.ts +0 -118
  31. package/dist/errors/errors.d.ts.map +0 -1
  32. package/dist/errors/messages.d.ts +0 -101
  33. package/dist/errors/messages.d.ts.map +0 -1
  34. package/dist/index.d.ts.map +0 -1
  35. package/dist/scheduler/batch.d.ts +0 -31
  36. package/dist/scheduler/batch.d.ts.map +0 -1
  37. package/dist/scheduler/index.d.ts +0 -3
  38. package/dist/scheduler/index.d.ts.map +0 -1
  39. package/dist/scheduler/scheduler.d.ts +0 -153
  40. package/dist/scheduler/scheduler.d.ts.map +0 -1
  41. package/dist/tracking/context.d.ts +0 -76
  42. package/dist/tracking/context.d.ts.map +0 -1
  43. package/dist/tracking/dependency-manager.d.ts +0 -224
  44. package/dist/tracking/dependency-manager.d.ts.map +0 -1
  45. package/dist/tracking/index.d.ts +0 -5
  46. package/dist/tracking/index.d.ts.map +0 -1
  47. package/dist/tracking/tracking.types.d.ts +0 -12
  48. package/dist/tracking/tracking.types.d.ts.map +0 -1
  49. package/dist/tracking/untracked.d.ts +0 -25
  50. package/dist/tracking/untracked.d.ts.map +0 -1
  51. package/dist/types/atom.d.ts +0 -13
  52. package/dist/types/atom.d.ts.map +0 -1
  53. package/dist/types/common.d.ts +0 -45
  54. package/dist/types/common.d.ts.map +0 -1
  55. package/dist/types/computed.d.ts +0 -18
  56. package/dist/types/computed.d.ts.map +0 -1
  57. package/dist/types/effect.d.ts +0 -13
  58. package/dist/types/effect.d.ts.map +0 -1
  59. package/dist/types/index.d.ts +0 -5
  60. package/dist/types/index.d.ts.map +0 -1
  61. package/dist/utils/debug.d.ts +0 -85
  62. package/dist/utils/debug.d.ts.map +0 -1
  63. package/dist/utils/object-pool.d.ts +0 -159
  64. package/dist/utils/object-pool.d.ts.map +0 -1
  65. package/dist/utils/subscriber-manager.d.ts +0 -127
  66. package/dist/utils/subscriber-manager.d.ts.map +0 -1
  67. package/dist/utils/type-guards.d.ts +0 -5
  68. package/dist/utils/type-guards.d.ts.map +0 -1
@@ -1,64 +0,0 @@
1
- import { EffectFunction, EffectObject, EffectOptions } from '../../types';
2
- /**
3
- * Creates a reactive effect that automatically re-executes when its dependencies change.
4
- *
5
- * @param fn - The effect function to execute. May return a cleanup function
6
- * or a Promise that resolves to a cleanup function.
7
- * @param options - Configuration options for the effect
8
- * @param options.sync - If true, re-executes synchronously on dependency changes.
9
- * Defaults to false (scheduled/batched execution).
10
- * @param options.maxExecutionsPerSecond - Maximum executions per second before
11
- * infinite loop detection triggers.
12
- * Defaults to `SCHEDULER_CONFIG.MAX_EXECUTIONS_PER_SECOND`.
13
- * @param options.trackModifications - If true, tracks and warns about dependencies
14
- * that are both read and modified. Defaults to false.
15
- *
16
- * @returns An {@link EffectObject} with `run()`, `dispose()`, and state properties
17
- *
18
- * @throws {EffectError} If `fn` is not a function
19
- *
20
- * @remarks
21
- * Effects are the primary way to perform side effects in response to reactive
22
- * state changes. They automatically track which reactive values (atoms, computed)
23
- * are accessed during execution and re-run when those values change.
24
- *
25
- * The effect function may return a cleanup function that will be called before
26
- * the next execution or when the effect is disposed. This is useful for
27
- * cleaning up subscriptions, timers, or other resources.
28
- *
29
- * @example
30
- * Basic usage:
31
- * ```typescript
32
- * const counter = atom(0);
33
- *
34
- * const fx = effect(() => {
35
- * console.log('Counter:', counter.value);
36
- * });
37
- * // Logs: "Counter: 0"
38
- *
39
- * counter.value = 1;
40
- * // Logs: "Counter: 1"
41
- *
42
- * fx.dispose(); // Stop the effect
43
- * ```
44
- *
45
- * @example
46
- * With cleanup function:
47
- * ```typescript
48
- * const fx = effect(() => {
49
- * const timer = setInterval(() => console.log('tick'), 1000);
50
- * return () => clearInterval(timer); // Cleanup
51
- * });
52
- * ```
53
- *
54
- * @example
55
- * Synchronous execution:
56
- * ```typescript
57
- * const fx = effect(
58
- * () => console.log(counter.value),
59
- * { sync: true }
60
- * );
61
- * ```
62
- */
63
- export declare function effect(fn: EffectFunction, options?: EffectOptions): EffectObject;
64
- //# sourceMappingURL=effect.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"effect.d.ts","sourceRoot":"","sources":["../../../src/core/effect/effect.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAQH,OAAO,KAAK,EAAc,cAAc,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAiiB3F;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4DG;AACH,wBAAgB,MAAM,CAAC,EAAE,EAAE,cAAc,EAAE,OAAO,GAAE,aAAkB,GAAG,YAAY,CAUpF"}
@@ -1,2 +0,0 @@
1
- export * from './effect';
2
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/core/effect/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC"}
@@ -1,4 +0,0 @@
1
- export * from './atom';
2
- export * from './computed';
3
- export * from './effect';
4
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAC;AACvB,cAAc,YAAY,CAAC;AAC3B,cAAc,UAAU,CAAC"}
@@ -1,118 +0,0 @@
1
- /**
2
- * @fileoverview Error class hierarchy for atom-effect library
3
- * @description Structured error classes with cause tracking and recoverability flags
4
- */
5
- /**
6
- * Base error class for all atom-effect errors
7
- *
8
- * Provides enhanced error information including:
9
- * - Original cause tracking for error chains
10
- * - Recoverability flag for error handling strategies
11
- * - Timestamp for debugging and logging
12
- *
13
- * @example
14
- * ```ts
15
- * throw new AtomError('Invalid state', originalError, false);
16
- * ```
17
- */
18
- export declare class AtomError extends Error {
19
- /** Original error that caused this error, if any */
20
- cause: Error | null;
21
- /** Whether this error can be recovered from */
22
- recoverable: boolean;
23
- /** When this error occurred */
24
- timestamp: Date;
25
- /**
26
- * Creates a new AtomError
27
- * @param message - Error message describing what went wrong
28
- * @param cause - Original error that caused this error
29
- * @param recoverable - Whether the operation can be retried
30
- */
31
- constructor(message: string, cause?: Error | null, recoverable?: boolean);
32
- }
33
- /**
34
- * Error thrown during computed value computation
35
- *
36
- * Computed errors are considered recoverable by default since they typically
37
- * result from transient data issues rather than programming errors.
38
- */
39
- export declare class ComputedError extends AtomError {
40
- /**
41
- * Creates a new ComputedError
42
- * @param message - Error message
43
- * @param cause - Original error
44
- */
45
- constructor(message: string, cause?: Error | null);
46
- }
47
- /**
48
- * Error thrown during effect execution
49
- *
50
- * Effect errors are considered non-recoverable by default since effects
51
- * typically represent critical side effects that shouldn't fail silently.
52
- */
53
- export declare class EffectError extends AtomError {
54
- /**
55
- * Creates a new EffectError
56
- * @param message - Error message
57
- * @param cause - Original error
58
- */
59
- constructor(message: string, cause?: Error | null);
60
- }
61
- /**
62
- * Error thrown by the scheduler system
63
- *
64
- * Scheduler errors indicate fundamental issues with the batching/scheduling
65
- * mechanism and are considered non-recoverable.
66
- */
67
- export declare class SchedulerError extends AtomError {
68
- /**
69
- * Creates a new SchedulerError
70
- * @param message - Error message
71
- * @param cause - Original error
72
- */
73
- constructor(message: string, cause?: Error | null);
74
- }
75
- /**
76
- * Wraps an unknown error in the appropriate AtomError subclass
77
- *
78
- * Provides consistent error handling by:
79
- * - Preserving original error information in the cause field
80
- * - Adding contextual information about where the error occurred
81
- * - Returning existing AtomErrors unchanged
82
- * - Handling various error types (TypeError, ReferenceError, etc.)
83
- *
84
- * @param error - Unknown error to wrap
85
- * @param ErrorClass - AtomError subclass to use for wrapping
86
- * @param context - Context string describing where the error occurred
87
- * @returns Wrapped error with contextual information
88
- *
89
- * @example
90
- * ```ts
91
- * try {
92
- * computeFn();
93
- * } catch (err) {
94
- * throw wrapError(err, ComputedError, 'computation phase');
95
- * }
96
- * ```
97
- */
98
- export declare function wrapError(error: unknown, ErrorClass: typeof AtomError, context: string): AtomError;
99
- /**
100
- * Type guard to check if a value is a Promise
101
- *
102
- * Uses duck-typing to detect Promise-like objects by checking for
103
- * the presence of a `then` method.
104
- *
105
- * @template T - The type the Promise resolves to
106
- * @param value - Value to check
107
- * @returns True if value has a `then` method (is Promise-like)
108
- *
109
- * @example
110
- * ```ts
111
- * const result = computeFn();
112
- * if (isPromise(result)) {
113
- * await result;
114
- * }
115
- * ```
116
- */
117
- export declare function isPromise<T>(value: unknown): value is Promise<T>;
118
- //# sourceMappingURL=errors.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/errors/errors.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;;;;;;;GAYG;AACH,qBAAa,SAAU,SAAQ,KAAK;IAClC,oDAAoD;IACpD,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;IACpB,+CAA+C;IAC/C,WAAW,EAAE,OAAO,CAAC;IACrB,+BAA+B;IAC/B,SAAS,EAAE,IAAI,CAAC;IAEhB;;;;;OAKG;gBACS,OAAO,EAAE,MAAM,EAAE,KAAK,GAAE,KAAK,GAAG,IAAW,EAAE,WAAW,GAAE,OAAc;CAOrF;AAED;;;;;GAKG;AACH,qBAAa,aAAc,SAAQ,SAAS;IAC1C;;;;OAIG;gBACS,OAAO,EAAE,MAAM,EAAE,KAAK,GAAE,KAAK,GAAG,IAAW;CAIxD;AAED;;;;;GAKG;AACH,qBAAa,WAAY,SAAQ,SAAS;IACxC;;;;OAIG;gBACS,OAAO,EAAE,MAAM,EAAE,KAAK,GAAE,KAAK,GAAG,IAAW;CAIxD;AAED;;;;;GAKG;AACH,qBAAa,cAAe,SAAQ,SAAS;IAC3C;;;;OAIG;gBACS,OAAO,EAAE,MAAM,EAAE,KAAK,GAAE,KAAK,GAAG,IAAW;CAIxD;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,SAAS,CACvB,KAAK,EAAE,OAAO,EACd,UAAU,EAAE,OAAO,SAAS,EAC5B,OAAO,EAAE,MAAM,GACd,SAAS,CAeX;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,OAAO,CAAC,CAAC,CAAC,CAMhE"}
@@ -1,101 +0,0 @@
1
- /**
2
- * @fileoverview Centralized error messages for better maintainability
3
- * @description All error messages in English for international accessibility
4
- * @module errors/messages
5
- */
6
- /**
7
- * Centralized error message constants for the atom-effect library.
8
- *
9
- * @description
10
- * Provides consistent, maintainable error messages across the library.
11
- * All messages are in English for international accessibility.
12
- *
13
- * @remarks
14
- * - Computed errors: Related to computed atom creation and execution
15
- * - Atom errors: Related to atom subscription and notification
16
- * - Effect errors: Related to effect lifecycle and cleanup
17
- * - Debug warnings: Non-critical warnings for debugging
18
- *
19
- * @example
20
- * ```ts
21
- * import { ERROR_MESSAGES } from './messages';
22
- *
23
- * if (typeof fn !== 'function') {
24
- * throw new Error(ERROR_MESSAGES.COMPUTED_MUST_BE_FUNCTION);
25
- * }
26
- * ```
27
- */
28
- export declare const ERROR_MESSAGES: {
29
- /**
30
- * Error thrown when computed() receives a non-function argument.
31
- */
32
- readonly COMPUTED_MUST_BE_FUNCTION: "Computed function must be a function";
33
- /**
34
- * Error thrown when subscribe() receives a non-function listener.
35
- */
36
- readonly COMPUTED_SUBSCRIBER_MUST_BE_FUNCTION: "Subscriber listener must be a function";
37
- /**
38
- * Error thrown when accessing a pending async computed without a default value.
39
- */
40
- readonly COMPUTED_ASYNC_PENDING_NO_DEFAULT: "Async computation is pending. No default value provided";
41
- /**
42
- * Error thrown when a synchronous computed computation fails.
43
- */
44
- readonly COMPUTED_COMPUTATION_FAILED: "Computed computation failed";
45
- /**
46
- * Error thrown when an asynchronous computed computation fails.
47
- */
48
- readonly COMPUTED_ASYNC_COMPUTATION_FAILED: "Async computed computation failed";
49
- /**
50
- * Error thrown when subscribing to a dependency fails.
51
- */
52
- readonly COMPUTED_DEPENDENCY_SUBSCRIPTION_FAILED: "Failed to subscribe to dependency";
53
- /**
54
- * Error thrown when atom.subscribe() receives a non-function listener.
55
- */
56
- readonly ATOM_SUBSCRIBER_MUST_BE_FUNCTION: "Subscription listener must be a function";
57
- /**
58
- * Error thrown when the atom subscriber notification process fails.
59
- */
60
- readonly ATOM_SUBSCRIBER_EXECUTION_FAILED: "Error occurred while executing atom subscribers";
61
- /**
62
- * Error logged when an individual subscriber throws during notification.
63
- * @remarks This error is caught and logged to prevent cascading failures.
64
- */
65
- readonly ATOM_INDIVIDUAL_SUBSCRIBER_FAILED: "Error during individual atom subscriber execution";
66
- /**
67
- * Error thrown when effect() receives a non-function argument.
68
- */
69
- readonly EFFECT_MUST_BE_FUNCTION: "Effect function must be a function";
70
- /**
71
- * Error thrown when an effect's execution fails.
72
- */
73
- readonly EFFECT_EXECUTION_FAILED: "Effect execution failed";
74
- /**
75
- * Error thrown when an effect's cleanup function fails.
76
- */
77
- readonly EFFECT_CLEANUP_FAILED: "Effect cleanup function execution failed";
78
- /**
79
- * Warning message for large dependency graphs.
80
- *
81
- * @param count - The number of dependencies detected
82
- * @returns Formatted warning message with dependency count
83
- *
84
- * @example
85
- * ```ts
86
- * console.warn(ERROR_MESSAGES.LARGE_DEPENDENCY_GRAPH(150));
87
- * // Output: "Large dependency graph detected: 150 dependencies"
88
- * ```
89
- */
90
- readonly LARGE_DEPENDENCY_GRAPH: (count: number) => string;
91
- /**
92
- * Warning logged when attempting to unsubscribe a non-existent listener.
93
- */
94
- readonly UNSUBSCRIBE_NON_EXISTENT: "Attempted to unsubscribe a non-existent listener";
95
- /**
96
- * Error logged when the onError callback itself throws an error.
97
- * @remarks This prevents cascading failures from masking the original error.
98
- */
99
- readonly CALLBACK_ERROR_IN_ERROR_HANDLER: "Error occurred during onError callback execution";
100
- };
101
- //# sourceMappingURL=messages.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"messages.d.ts","sourceRoot":"","sources":["../../src/errors/messages.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,eAAO,MAAM,cAAc;IAKzB;;OAEG;;IAGH;;OAEG;;IAGH;;OAEG;;IAGH;;OAEG;;IAGH;;OAEG;;IAGH;;OAEG;;IAOH;;OAEG;;IAGH;;OAEG;;IAGH;;;OAGG;;IAOH;;OAEG;;IAGH;;OAEG;;IAGH;;OAEG;;IAOH;;;;;;;;;;;OAWG;6CAC6B,MAAM,KAAG,MAAM;IAG/C;;OAEG;;IAGH;;;OAGG;;CAEK,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,WAAW,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AACtF,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhD,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAExF,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACvC,cAAc,SAAS,CAAC;AAExB,OAAO,EAAE,KAAK,IAAI,aAAa,EAAE,MAAM,eAAe,CAAC;AACvD,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC"}
@@ -1,31 +0,0 @@
1
- /**
2
- * Executes multiple reactive updates in a single batch.
3
- *
4
- * Batching groups multiple state changes together, deferring notifications
5
- * until all updates are complete. This prevents intermediate states from
6
- * triggering unnecessary recomputations and improves performance.
7
- *
8
- * @template T - The return type of the callback function
9
- * @param callback - The function containing batched updates
10
- * @returns The result of the callback function
11
- * @throws {AtomError} If the callback is not a function
12
- * @throws {AtomError} If an error occurs during batch execution
13
- *
14
- * @example
15
- * ```typescript
16
- * const firstName = atom('John');
17
- * const lastName = atom('Doe');
18
- *
19
- * // Without batching: triggers 2 separate updates
20
- * firstName.value = 'Jane';
21
- * lastName.value = 'Smith';
22
- *
23
- * // With batching: triggers 1 combined update
24
- * batch(() => {
25
- * firstName.value = 'Jane';
26
- * lastName.value = 'Smith';
27
- * });
28
- * ```
29
- */
30
- export declare function batch<T>(callback: () => T): T;
31
- //# sourceMappingURL=batch.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"batch.d.ts","sourceRoot":"","sources":["../../src/scheduler/batch.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,KAAK,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,CAAC,GAAG,CAAC,CAc7C"}
@@ -1,3 +0,0 @@
1
- export * from './batch';
2
- export * from './scheduler';
3
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/scheduler/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC"}
@@ -1,153 +0,0 @@
1
- /**
2
- * Scheduler for managing reactive updates and batching operations.
3
- *
4
- * The Scheduler is responsible for coordinating when reactive computations
5
- * are executed. It supports both immediate (microtask) execution and
6
- * batched synchronous execution for optimal performance.
7
- *
8
- * Key features:
9
- * - Deduplication of callbacks via Set
10
- * - Nested batch support with depth tracking
11
- * - Infinite loop protection with configurable iteration limit
12
- * - Error isolation to prevent one callback from breaking others
13
- *
14
- * @example
15
- * ```typescript
16
- * // Schedule a callback for microtask execution
17
- * scheduler.schedule(() => console.log('Updated!'));
18
- *
19
- * // Batch multiple updates
20
- * scheduler.startBatch();
21
- * scheduler.schedule(() => console.log('Update 1'));
22
- * scheduler.schedule(() => console.log('Update 2'));
23
- * scheduler.endBatch(); // Both execute synchronously here
24
- * ```
25
- */
26
- declare class Scheduler {
27
- /** Queue of callbacks waiting for microtask execution */
28
- /** Queue buffers for double buffering optimization */
29
- private queueA;
30
- private queueB;
31
- /** Currently active queue receiving new tasks */
32
- private queue;
33
- /** Whether the scheduler is currently processing the queue */
34
- private isProcessing;
35
- /** Whether batching is currently active */
36
- isBatching: boolean;
37
- /** Current nesting depth of batch operations */
38
- private batchDepth;
39
- /** Array of callbacks queued during batching */
40
- private batchQueue;
41
- /** Current size of the batch queue (for array reuse) */
42
- private batchQueueSize;
43
- /** Whether synchronous flush is in progress */
44
- private isFlushingSync;
45
- /** Maximum iterations allowed during flush to prevent infinite loops */
46
- private maxFlushIterations;
47
- /**
48
- * Schedules a callback for execution.
49
- *
50
- * If batching is active or a sync flush is in progress, the callback
51
- * is added to the batch queue. Otherwise, it's added to the main queue
52
- * and a flush is triggered via microtask.
53
- *
54
- * @param callback - The function to schedule for execution
55
- * @throws {SchedulerError} If callback is not a function
56
- *
57
- * @example
58
- * ```typescript
59
- * scheduler.schedule(() => {
60
- * // This runs in the next microtask (or sync if batching)
61
- * updateUI();
62
- * });
63
- * ```
64
- */
65
- schedule(callback: () => void): void;
66
- /**
67
- * Flushes the queue asynchronously via microtask.
68
- *
69
- * Executes all queued callbacks in a microtask, allowing the current
70
- * synchronous execution to complete first. Errors in individual
71
- * callbacks are caught and logged without interrupting others.
72
- *
73
- * @private
74
- * @remarks
75
- * This method is idempotent - calling it multiple times while
76
- * processing is active has no effect.
77
- */
78
- private flush;
79
- /**
80
- * Flushes all queued callbacks synchronously.
81
- *
82
- * This method is called when a batch ends. It processes all callbacks
83
- * in the batch queue and main queue synchronously, allowing callbacks
84
- * to schedule additional callbacks that are processed in the same flush.
85
- *
86
- * @private
87
- * @remarks
88
- * - Includes infinite loop protection via maxFlushIterations
89
- * - Errors in callbacks are caught and logged individually
90
- * - The isFlushingSync flag prevents re-entrancy issues
91
- */
92
- private flushSync;
93
- /**
94
- * Starts a new batch operation.
95
- *
96
- * While batching is active, all scheduled callbacks are deferred
97
- * until endBatch() is called. Batches can be nested - only the
98
- * outermost endBatch() triggers execution.
99
- *
100
- * @example
101
- * ```typescript
102
- * scheduler.startBatch();
103
- * // All updates here are deferred
104
- * atom1.value = 'a';
105
- * atom2.value = 'b';
106
- * scheduler.endBatch(); // Both updates processed together
107
- * ```
108
- */
109
- startBatch(): void;
110
- /**
111
- * Ends a batch operation.
112
- *
113
- * Decrements the batch depth counter. When depth reaches zero,
114
- * all queued callbacks are flushed synchronously and batching
115
- * is disabled.
116
- *
117
- * @remarks
118
- * Safe to call even if startBatch() wasn't called - depth is
119
- * clamped to zero minimum.
120
- *
121
- * @example
122
- * ```typescript
123
- * scheduler.startBatch();
124
- * try {
125
- * // ... batched operations
126
- * } finally {
127
- * scheduler.endBatch(); // Always end batch, even on error
128
- * }
129
- * ```
130
- */
131
- endBatch(): void;
132
- /**
133
- * Sets the maximum number of flush iterations allowed.
134
- *
135
- * This limit prevents infinite loops when reactive dependencies
136
- * form cycles. If exceeded, the queue is cleared and an error
137
- * is logged.
138
- *
139
- * @param max - Maximum iterations (must be at least 10)
140
- * @throws {SchedulerError} If max is less than 10
141
- *
142
- * @example
143
- * ```typescript
144
- * // Increase limit for complex dependency graphs
145
- * scheduler.setMaxFlushIterations(5000);
146
- * ```
147
- */
148
- setMaxFlushIterations(max: number): void;
149
- }
150
- /** Global scheduler instance for reactive updates */
151
- export declare const scheduler: Scheduler;
152
- export {};
153
- //# sourceMappingURL=scheduler.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"scheduler.d.ts","sourceRoot":"","sources":["../../src/scheduler/scheduler.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,cAAM,SAAS;IACb,yDAAyD;IACzD,sDAAsD;IACtD,OAAO,CAAC,MAAM,CAA8B;IAC5C,OAAO,CAAC,MAAM,CAA8B;IAE5C,iDAAiD;IACjD,OAAO,CAAC,KAAK,CAAgC;IAE7C,8DAA8D;IAC9D,OAAO,CAAC,YAAY,CAAkB;IAEtC,2CAA2C;IACpC,UAAU,EAAE,OAAO,CAAS;IAEnC,gDAAgD;IAChD,OAAO,CAAC,UAAU,CAAa;IAE/B,gDAAgD;IAChD,OAAO,CAAC,UAAU,CAAyB;IAE3C,wDAAwD;IACxD,OAAO,CAAC,cAAc,CAAK;IAE3B,+CAA+C;IAC/C,OAAO,CAAC,cAAc,CAAkB;IAExC,wEAAwE;IACxE,OAAO,CAAC,kBAAkB,CAAgB;IAE1C;;;;;;;;;;;;;;;;;OAiBG;IACH,QAAQ,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,IAAI;IAepC;;;;;;;;;;;OAWG;IACH,OAAO,CAAC,KAAK;IAgCb;;;;;;;;;;;;OAYG;IACH,OAAO,CAAC,SAAS;IAuDjB;;;;;;;;;;;;;;;OAeG;IACH,UAAU,IAAI,IAAI;IAKlB;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,QAAQ,IAAI,IAAI;IAShB;;;;;;;;;;;;;;;OAeG;IACH,qBAAqB,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;CAMzC;AAED,qDAAqD;AACrD,eAAO,MAAM,SAAS,WAAkB,CAAC"}
@@ -1,76 +0,0 @@
1
- import { Listener } from './tracking.types';
2
- /**
3
- * Interface for the tracking context that manages dependency tracking.
4
- *
5
- * The tracking context is responsible for maintaining the current listener
6
- * during reactive computations, enabling automatic dependency collection.
7
- *
8
- * @interface TrackingContext
9
- */
10
- export interface TrackingContext {
11
- /**
12
- * The currently active listener being tracked.
13
- * `null` when no tracking is in progress.
14
- */
15
- current: Listener | null;
16
- /**
17
- * Executes a function within a tracking context.
18
- *
19
- * Sets the provided listener as the current tracking target,
20
- * executes the function, and restores the previous context.
21
- *
22
- * @template T - The return type of the function
23
- * @param listener - The listener to set as current during execution
24
- * @param fn - The function to execute within the tracking context
25
- * @returns The result of the executed function
26
- *
27
- * @example
28
- * ```typescript
29
- * const result = trackingContext.run(myListener, () => {
30
- * // Any atom access here will be tracked
31
- * return someAtom.value + otherAtom.value;
32
- * });
33
- * ```
34
- */
35
- run<T>(listener: Listener, fn: () => T): T;
36
- /**
37
- * Gets the currently active listener.
38
- *
39
- * @returns The current listener or `null` if no tracking is active
40
- *
41
- * @example
42
- * ```typescript
43
- * const current = trackingContext.getCurrent();
44
- * if (current) {
45
- * // Dependency tracking is active
46
- * }
47
- * ```
48
- */
49
- getCurrent(): Listener | null;
50
- }
51
- /**
52
- * Global tracking context singleton for dependency tracking.
53
- *
54
- * This object manages the current listener during reactive computations,
55
- * enabling atoms and computed values to automatically track their dependencies.
56
- *
57
- * @remarks
58
- * - The context uses a stack-like behavior via the `run` method
59
- * - Nested `run` calls properly restore the previous context
60
- * - Thread-safe within a single JavaScript execution context
61
- *
62
- * @example
63
- * ```typescript
64
- * // Setting up tracking for a computed value
65
- * const value = trackingContext.run(computedListener, () => {
66
- * return atom1.value + atom2.value; // Both atoms are tracked
67
- * });
68
- *
69
- * // Checking if tracking is active
70
- * if (trackingContext.getCurrent()) {
71
- * // Register this atom as a dependency
72
- * }
73
- * ```
74
- */
75
- export declare const trackingContext: TrackingContext;
76
- //# sourceMappingURL=context.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/tracking/context.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAEjD;;;;;;;GAOG;AACH,MAAM,WAAW,eAAe;IAC9B;;;OAGG;IACH,OAAO,EAAE,QAAQ,GAAG,IAAI,CAAC;IAEzB;;;;;;;;;;;;;;;;;;OAkBG;IACH,GAAG,CAAC,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC;IAE3C;;;;;;;;;;;;OAYG;IACH,UAAU,IAAI,QAAQ,GAAG,IAAI,CAAC;CAC/B;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,eAAO,MAAM,eAAe,EAAE,eAsB7B,CAAC"}