atomirx 0.0.8 → 0.1.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.
- package/README.md +198 -2234
- package/bin/cli.js +90 -0
- package/dist/core/derived.d.ts +2 -2
- package/dist/core/effect.d.ts +3 -2
- package/dist/core/onCreateHook.d.ts +15 -2
- package/dist/core/onErrorHook.d.ts +4 -1
- package/dist/core/pool.d.ts +78 -0
- package/dist/core/pool.test.d.ts +1 -0
- package/dist/core/select-boolean.test.d.ts +1 -0
- package/dist/core/select-pool.test.d.ts +1 -0
- package/dist/core/select.d.ts +278 -86
- package/dist/core/types.d.ts +233 -1
- package/dist/core/withAbort.d.ts +95 -0
- package/dist/core/withReady.d.ts +3 -3
- package/dist/devtools/constants.d.ts +41 -0
- package/dist/devtools/index.cjs +1 -0
- package/dist/devtools/index.d.ts +29 -0
- package/dist/devtools/index.js +429 -0
- package/dist/devtools/registry.d.ts +98 -0
- package/dist/devtools/registry.test.d.ts +1 -0
- package/dist/devtools/setup.d.ts +61 -0
- package/dist/devtools/types.d.ts +311 -0
- package/dist/index-BZEnfIcB.cjs +1 -0
- package/dist/index-BbPZhsDl.js +1653 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +4 -3
- package/dist/index.js +18 -14
- package/dist/onDispatchHook-C8yLzr-o.cjs +1 -0
- package/dist/onDispatchHook-SKbiIUaJ.js +5 -0
- package/dist/onErrorHook-BGGy3tqK.js +38 -0
- package/dist/onErrorHook-DHBASmYw.cjs +1 -0
- package/dist/react/index.cjs +1 -1
- package/dist/react/index.js +191 -151
- package/dist/react/onDispatchHook.d.ts +106 -0
- package/dist/react/useAction.d.ts +4 -1
- package/dist/react-devtools/DevToolsPanel.d.ts +93 -0
- package/dist/react-devtools/EntityDetails.d.ts +10 -0
- package/dist/react-devtools/EntityList.d.ts +15 -0
- package/dist/react-devtools/LogList.d.ts +12 -0
- package/dist/react-devtools/hooks.d.ts +50 -0
- package/dist/react-devtools/index.cjs +1 -0
- package/dist/react-devtools/index.d.ts +31 -0
- package/dist/react-devtools/index.js +1589 -0
- package/dist/react-devtools/styles.d.ts +148 -0
- package/package.json +26 -2
- package/skills/atomirx/SKILL.md +456 -0
- package/skills/atomirx/references/async-patterns.md +188 -0
- package/skills/atomirx/references/atom-patterns.md +238 -0
- package/skills/atomirx/references/deferred-loading.md +191 -0
- package/skills/atomirx/references/derived-patterns.md +428 -0
- package/skills/atomirx/references/effect-patterns.md +426 -0
- package/skills/atomirx/references/error-handling.md +140 -0
- package/skills/atomirx/references/hooks.md +322 -0
- package/skills/atomirx/references/pool-patterns.md +229 -0
- package/skills/atomirx/references/react-integration.md +411 -0
- package/skills/atomirx/references/rules.md +407 -0
- package/skills/atomirx/references/select-context.md +309 -0
- package/skills/atomirx/references/service-template.md +172 -0
- package/skills/atomirx/references/store-template.md +205 -0
- package/skills/atomirx/references/testing-patterns.md +431 -0
- package/coverage/base.css +0 -224
- package/coverage/block-navigation.js +0 -87
- package/coverage/clover.xml +0 -1440
- package/coverage/coverage-final.json +0 -14
- package/coverage/favicon.png +0 -0
- package/coverage/index.html +0 -131
- package/coverage/prettify.css +0 -1
- package/coverage/prettify.js +0 -2
- package/coverage/sort-arrow-sprite.png +0 -0
- package/coverage/sorter.js +0 -210
- package/coverage/src/core/atom.ts.html +0 -889
- package/coverage/src/core/batch.ts.html +0 -223
- package/coverage/src/core/define.ts.html +0 -805
- package/coverage/src/core/emitter.ts.html +0 -919
- package/coverage/src/core/equality.ts.html +0 -631
- package/coverage/src/core/hook.ts.html +0 -460
- package/coverage/src/core/index.html +0 -281
- package/coverage/src/core/isAtom.ts.html +0 -100
- package/coverage/src/core/isPromiseLike.ts.html +0 -133
- package/coverage/src/core/onCreateHook.ts.html +0 -138
- package/coverage/src/core/scheduleNotifyHook.ts.html +0 -94
- package/coverage/src/core/types.ts.html +0 -523
- package/coverage/src/core/withUse.ts.html +0 -253
- package/coverage/src/index.html +0 -116
- package/coverage/src/index.ts.html +0 -106
- package/dist/index-CBVj1kSj.js +0 -1350
- package/dist/index-Cxk9v0um.cjs +0 -1
- package/scripts/publish.js +0 -198
- package/src/core/atom.test.ts +0 -633
- package/src/core/atom.ts +0 -311
- package/src/core/atomState.test.ts +0 -342
- package/src/core/atomState.ts +0 -256
- package/src/core/batch.test.ts +0 -257
- package/src/core/batch.ts +0 -172
- package/src/core/define.test.ts +0 -343
- package/src/core/define.ts +0 -243
- package/src/core/derived.test.ts +0 -1215
- package/src/core/derived.ts +0 -450
- package/src/core/effect.test.ts +0 -802
- package/src/core/effect.ts +0 -188
- package/src/core/emitter.test.ts +0 -364
- package/src/core/emitter.ts +0 -392
- package/src/core/equality.test.ts +0 -392
- package/src/core/equality.ts +0 -182
- package/src/core/getAtomState.ts +0 -69
- package/src/core/hook.test.ts +0 -227
- package/src/core/hook.ts +0 -177
- package/src/core/isAtom.ts +0 -27
- package/src/core/isPromiseLike.test.ts +0 -72
- package/src/core/isPromiseLike.ts +0 -16
- package/src/core/onCreateHook.ts +0 -107
- package/src/core/onErrorHook.test.ts +0 -350
- package/src/core/onErrorHook.ts +0 -52
- package/src/core/promiseCache.test.ts +0 -241
- package/src/core/promiseCache.ts +0 -284
- package/src/core/scheduleNotifyHook.ts +0 -53
- package/src/core/select.ts +0 -729
- package/src/core/selector.test.ts +0 -799
- package/src/core/types.ts +0 -389
- package/src/core/withReady.test.ts +0 -534
- package/src/core/withReady.ts +0 -191
- package/src/core/withUse.test.ts +0 -249
- package/src/core/withUse.ts +0 -56
- package/src/index.test.ts +0 -80
- package/src/index.ts +0 -65
- package/src/react/index.ts +0 -21
- package/src/react/rx.test.tsx +0 -571
- package/src/react/rx.tsx +0 -531
- package/src/react/strictModeTest.tsx +0 -71
- package/src/react/useAction.test.ts +0 -987
- package/src/react/useAction.ts +0 -607
- package/src/react/useSelector.test.ts +0 -182
- package/src/react/useSelector.ts +0 -292
- package/src/react/useStable.test.ts +0 -553
- package/src/react/useStable.ts +0 -288
- package/tsconfig.json +0 -9
- package/v2.md +0 -725
- package/vite.config.ts +0 -42
package/src/core/effect.ts
DELETED
|
@@ -1,188 +0,0 @@
|
|
|
1
|
-
import { batch } from "./batch";
|
|
2
|
-
import { derived } from "./derived";
|
|
3
|
-
import { emitter } from "./emitter";
|
|
4
|
-
import { EffectInfo, onCreateHook } from "./onCreateHook";
|
|
5
|
-
import { ReactiveSelector, SelectContext } from "./select";
|
|
6
|
-
import { EffectMeta, EffectOptions } from "./types";
|
|
7
|
-
import { WithReadySelectContext } from "./withReady";
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Context object passed to effect functions.
|
|
11
|
-
* Extends `SelectContext` with cleanup utilities.
|
|
12
|
-
*/
|
|
13
|
-
export interface EffectContext extends SelectContext, WithReadySelectContext {
|
|
14
|
-
/**
|
|
15
|
-
* Register a cleanup function that runs before the next execution or on dispose.
|
|
16
|
-
* Multiple cleanup functions can be registered; they run in FIFO order.
|
|
17
|
-
*
|
|
18
|
-
* @param cleanup - Function to run during cleanup
|
|
19
|
-
*
|
|
20
|
-
* @example
|
|
21
|
-
* ```ts
|
|
22
|
-
* effect(({ read, onCleanup }) => {
|
|
23
|
-
* const id = setInterval(() => console.log('tick'), 1000);
|
|
24
|
-
* onCleanup(() => clearInterval(id));
|
|
25
|
-
* });
|
|
26
|
-
* ```
|
|
27
|
-
*/
|
|
28
|
-
onCleanup: (cleanup: VoidFunction) => void;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export interface Effect {
|
|
32
|
-
dispose: VoidFunction;
|
|
33
|
-
meta?: EffectMeta;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Creates a side-effect that runs when accessed atom(s) change.
|
|
38
|
-
*
|
|
39
|
-
* Effects are similar to derived atoms but for side-effects rather than computed values.
|
|
40
|
-
* They inherit derived's behavior:
|
|
41
|
-
* - **Suspense-like async**: Waits for async atoms to resolve before running
|
|
42
|
-
* - **Conditional dependencies**: Only tracks atoms actually accessed via `read()`
|
|
43
|
-
* - **Automatic cleanup**: Previous cleanup runs before next execution
|
|
44
|
-
* - **Batched updates**: Atom updates within the effect are batched
|
|
45
|
-
*
|
|
46
|
-
* ## IMPORTANT: Effect Function Must Be Synchronous
|
|
47
|
-
*
|
|
48
|
-
* **The effect function MUST NOT be async or return a Promise.**
|
|
49
|
-
*
|
|
50
|
-
* ```ts
|
|
51
|
-
* // ❌ WRONG - Don't use async function
|
|
52
|
-
* effect(async ({ read }) => {
|
|
53
|
-
* const data = await fetch('/api');
|
|
54
|
-
* console.log(data);
|
|
55
|
-
* });
|
|
56
|
-
*
|
|
57
|
-
* // ✅ CORRECT - Create async atom and read with read()
|
|
58
|
-
* const data$ = atom(fetch('/api').then(r => r.json()));
|
|
59
|
-
* effect(({ read }) => {
|
|
60
|
-
* console.log(read(data$)); // Suspends until resolved
|
|
61
|
-
* });
|
|
62
|
-
* ```
|
|
63
|
-
*
|
|
64
|
-
* ## Basic Usage
|
|
65
|
-
*
|
|
66
|
-
* ```ts
|
|
67
|
-
* const dispose = effect(({ read }) => {
|
|
68
|
-
* localStorage.setItem('count', String(read(countAtom)));
|
|
69
|
-
* });
|
|
70
|
-
* ```
|
|
71
|
-
*
|
|
72
|
-
* ## With Cleanup
|
|
73
|
-
*
|
|
74
|
-
* Use `onCleanup` to register cleanup functions that run before the next execution or on dispose:
|
|
75
|
-
*
|
|
76
|
-
* ```ts
|
|
77
|
-
* const dispose = effect(({ read, onCleanup }) => {
|
|
78
|
-
* const interval = read(intervalAtom);
|
|
79
|
-
* const id = setInterval(() => console.log('tick'), interval);
|
|
80
|
-
* onCleanup(() => clearInterval(id));
|
|
81
|
-
* });
|
|
82
|
-
* ```
|
|
83
|
-
*
|
|
84
|
-
* ## IMPORTANT: Do NOT Use try/catch - Use safe() Instead
|
|
85
|
-
*
|
|
86
|
-
* **Never wrap `read()` calls in try/catch blocks.** The `read()` function throws
|
|
87
|
-
* Promises when atoms are loading (Suspense pattern). A try/catch will catch
|
|
88
|
-
* these Promises and break the Suspense mechanism.
|
|
89
|
-
*
|
|
90
|
-
* ```ts
|
|
91
|
-
* // ❌ WRONG - Catches Suspense Promise, breaks loading state
|
|
92
|
-
* effect(({ read }) => {
|
|
93
|
-
* try {
|
|
94
|
-
* const data = read(asyncAtom$);
|
|
95
|
-
* riskyOperation(data);
|
|
96
|
-
* } catch (e) {
|
|
97
|
-
* console.error(e); // Catches BOTH errors AND loading promises!
|
|
98
|
-
* }
|
|
99
|
-
* });
|
|
100
|
-
*
|
|
101
|
-
* // ✅ CORRECT - Use safe() to catch errors but preserve Suspense
|
|
102
|
-
* effect(({ read, safe }) => {
|
|
103
|
-
* const [err, data] = safe(() => {
|
|
104
|
-
* const raw = read(asyncAtom$); // Can throw Promise (Suspense)
|
|
105
|
-
* return riskyOperation(raw); // Can throw Error
|
|
106
|
-
* });
|
|
107
|
-
*
|
|
108
|
-
* if (err) {
|
|
109
|
-
* console.error('Operation failed:', err);
|
|
110
|
-
* return;
|
|
111
|
-
* }
|
|
112
|
-
* // Use data safely
|
|
113
|
-
* });
|
|
114
|
-
* ```
|
|
115
|
-
*
|
|
116
|
-
* The `safe()` utility:
|
|
117
|
-
* - **Catches errors** and returns `[error, undefined]`
|
|
118
|
-
* - **Re-throws Promises** to preserve Suspense behavior
|
|
119
|
-
* - Returns `[undefined, result]` on success
|
|
120
|
-
*
|
|
121
|
-
* @param fn - Effect callback receiving context with `{ read, all, any, race, settled, safe, onCleanup }`.
|
|
122
|
-
* Must be synchronous (not async).
|
|
123
|
-
* @param options - Optional configuration (key)
|
|
124
|
-
* @returns Dispose function to stop the effect and run final cleanup
|
|
125
|
-
* @throws Error if effect function returns a Promise
|
|
126
|
-
*/
|
|
127
|
-
export function effect(
|
|
128
|
-
fn: ReactiveSelector<void, EffectContext>,
|
|
129
|
-
options?: EffectOptions
|
|
130
|
-
): Effect {
|
|
131
|
-
let disposed = false;
|
|
132
|
-
const cleanupEmitter = emitter();
|
|
133
|
-
|
|
134
|
-
// Create the Effect object early so we can build EffectInfo
|
|
135
|
-
const e: Effect = {
|
|
136
|
-
meta: options?.meta,
|
|
137
|
-
dispose: () => {
|
|
138
|
-
// Guard against multiple dispose calls
|
|
139
|
-
if (disposed) return;
|
|
140
|
-
|
|
141
|
-
// Mark as disposed
|
|
142
|
-
disposed = true;
|
|
143
|
-
// Run final cleanup
|
|
144
|
-
cleanupEmitter.emitAndClear();
|
|
145
|
-
},
|
|
146
|
-
};
|
|
147
|
-
|
|
148
|
-
// Create EffectInfo to pass to derived for error attribution
|
|
149
|
-
const effectInfo: EffectInfo = {
|
|
150
|
-
type: "effect",
|
|
151
|
-
key: options?.meta?.key,
|
|
152
|
-
meta: options?.meta,
|
|
153
|
-
instance: e,
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
// Create a derived atom that runs the effect on each recomputation.
|
|
157
|
-
// Pass _errorSource so errors are attributed to the effect, not the internal derived
|
|
158
|
-
const derivedAtom = derived(
|
|
159
|
-
(context) => {
|
|
160
|
-
// Run previous cleanup before next execution
|
|
161
|
-
cleanupEmitter.emitAndClear();
|
|
162
|
-
|
|
163
|
-
// Skip effect execution if disposed
|
|
164
|
-
if (disposed) return;
|
|
165
|
-
|
|
166
|
-
// Run effect in a batch - multiple atom updates will only notify once
|
|
167
|
-
// Cast to EffectContext since we're adding onCleanup to the DerivedContext
|
|
168
|
-
const effectContext = {
|
|
169
|
-
...context,
|
|
170
|
-
onCleanup: cleanupEmitter.on,
|
|
171
|
-
} as unknown as EffectContext;
|
|
172
|
-
batch(() => fn(effectContext));
|
|
173
|
-
},
|
|
174
|
-
{
|
|
175
|
-
onError: options?.onError,
|
|
176
|
-
_errorSource: effectInfo,
|
|
177
|
-
}
|
|
178
|
-
);
|
|
179
|
-
|
|
180
|
-
// Access .get() to trigger initial computation (derived is lazy)
|
|
181
|
-
// Errors are handled via onError callback or safe() in the effect function
|
|
182
|
-
derivedAtom.get();
|
|
183
|
-
|
|
184
|
-
// Notify devtools/plugins of effect creation
|
|
185
|
-
onCreateHook.current?.(effectInfo);
|
|
186
|
-
|
|
187
|
-
return e;
|
|
188
|
-
}
|
package/src/core/emitter.test.ts
DELETED
|
@@ -1,364 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, vi } from "vitest";
|
|
2
|
-
import { emitter } from "./emitter";
|
|
3
|
-
|
|
4
|
-
describe("emitter", () => {
|
|
5
|
-
describe("basic functionality", () => {
|
|
6
|
-
it("should emit to listeners in order", () => {
|
|
7
|
-
const e = emitter<number>();
|
|
8
|
-
const order: number[] = [];
|
|
9
|
-
|
|
10
|
-
e.on(() => order.push(1));
|
|
11
|
-
e.on(() => order.push(2));
|
|
12
|
-
e.on(() => order.push(3));
|
|
13
|
-
|
|
14
|
-
e.emit(42);
|
|
15
|
-
|
|
16
|
-
expect(order).toEqual([1, 2, 3]);
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
it("should support array of listeners in on()", () => {
|
|
20
|
-
const e = emitter<number>();
|
|
21
|
-
const calls: number[] = [];
|
|
22
|
-
|
|
23
|
-
e.on([
|
|
24
|
-
(n) => calls.push(n),
|
|
25
|
-
(n) => calls.push(n * 2),
|
|
26
|
-
(n) => calls.push(n * 3),
|
|
27
|
-
]);
|
|
28
|
-
|
|
29
|
-
e.emit(10);
|
|
30
|
-
|
|
31
|
-
expect(calls).toEqual([10, 20, 30]);
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
it("should return size of listeners", () => {
|
|
35
|
-
const e = emitter<number>();
|
|
36
|
-
|
|
37
|
-
expect(e.size()).toBe(0);
|
|
38
|
-
|
|
39
|
-
e.on(() => {});
|
|
40
|
-
expect(e.size()).toBe(1);
|
|
41
|
-
|
|
42
|
-
e.on(() => {});
|
|
43
|
-
expect(e.size()).toBe(2);
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
it("should unsubscribe listener", () => {
|
|
47
|
-
const e = emitter<number>();
|
|
48
|
-
const listener = vi.fn();
|
|
49
|
-
|
|
50
|
-
const unsub = e.on(listener);
|
|
51
|
-
e.emit(1);
|
|
52
|
-
expect(listener).toHaveBeenCalledTimes(1);
|
|
53
|
-
|
|
54
|
-
unsub();
|
|
55
|
-
e.emit(2);
|
|
56
|
-
expect(listener).toHaveBeenCalledTimes(1);
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
it("should support initial listeners", () => {
|
|
60
|
-
const listener1 = vi.fn();
|
|
61
|
-
const listener2 = vi.fn();
|
|
62
|
-
|
|
63
|
-
const e = emitter<number>([listener1, listener2]);
|
|
64
|
-
e.emit(42);
|
|
65
|
-
|
|
66
|
-
expect(listener1).toHaveBeenCalledWith(42);
|
|
67
|
-
expect(listener2).toHaveBeenCalledWith(42);
|
|
68
|
-
});
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
describe("on() with map function", () => {
|
|
72
|
-
it("should transform values with map function", () => {
|
|
73
|
-
const e = emitter<{ type: string; data: number }>();
|
|
74
|
-
const listener = vi.fn();
|
|
75
|
-
|
|
76
|
-
e.on((event) => ({ value: event.data * 2 }), listener);
|
|
77
|
-
|
|
78
|
-
e.emit({ type: "test", data: 5 });
|
|
79
|
-
|
|
80
|
-
expect(listener).toHaveBeenCalledWith(10);
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
it("should filter events when map returns undefined", () => {
|
|
84
|
-
const e = emitter<{ type: string; data: number }>();
|
|
85
|
-
const listener = vi.fn();
|
|
86
|
-
|
|
87
|
-
e.on(
|
|
88
|
-
(event) =>
|
|
89
|
-
event.type === "success" ? { value: event.data } : undefined,
|
|
90
|
-
listener
|
|
91
|
-
);
|
|
92
|
-
|
|
93
|
-
e.emit({ type: "error", data: 1 });
|
|
94
|
-
expect(listener).not.toHaveBeenCalled();
|
|
95
|
-
|
|
96
|
-
e.emit({ type: "success", data: 2 });
|
|
97
|
-
expect(listener).toHaveBeenCalledWith(2);
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
it("should support array of listeners with map function", () => {
|
|
101
|
-
const e = emitter<number>();
|
|
102
|
-
const listener1 = vi.fn();
|
|
103
|
-
const listener2 = vi.fn();
|
|
104
|
-
|
|
105
|
-
e.on((n) => ({ value: n * 10 }), [listener1, listener2]);
|
|
106
|
-
|
|
107
|
-
e.emit(5);
|
|
108
|
-
|
|
109
|
-
expect(listener1).toHaveBeenCalledWith(50);
|
|
110
|
-
expect(listener2).toHaveBeenCalledWith(50);
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
it("should unsubscribe mapped listeners correctly", () => {
|
|
114
|
-
const e = emitter<number>();
|
|
115
|
-
const listener = vi.fn();
|
|
116
|
-
|
|
117
|
-
const unsub = e.on((n) => ({ value: n }), listener);
|
|
118
|
-
|
|
119
|
-
e.emit(1);
|
|
120
|
-
expect(listener).toHaveBeenCalledTimes(1);
|
|
121
|
-
|
|
122
|
-
unsub();
|
|
123
|
-
|
|
124
|
-
e.emit(2);
|
|
125
|
-
expect(listener).toHaveBeenCalledTimes(1);
|
|
126
|
-
});
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
describe("emitLifo", () => {
|
|
130
|
-
it("should emit in reverse order (LIFO)", () => {
|
|
131
|
-
const e = emitter<number>();
|
|
132
|
-
const order: number[] = [];
|
|
133
|
-
|
|
134
|
-
e.on(() => order.push(1));
|
|
135
|
-
e.on(() => order.push(2));
|
|
136
|
-
e.on(() => order.push(3));
|
|
137
|
-
|
|
138
|
-
e.emitLifo(42);
|
|
139
|
-
|
|
140
|
-
expect(order).toEqual([3, 2, 1]);
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
it("should be no-op when settled", () => {
|
|
144
|
-
const e = emitter<number>();
|
|
145
|
-
const listener = vi.fn();
|
|
146
|
-
|
|
147
|
-
e.on(listener);
|
|
148
|
-
e.settle(1);
|
|
149
|
-
listener.mockClear();
|
|
150
|
-
|
|
151
|
-
e.emitLifo(2);
|
|
152
|
-
|
|
153
|
-
expect(listener).not.toHaveBeenCalled();
|
|
154
|
-
});
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
describe("clear", () => {
|
|
158
|
-
it("should remove all listeners", () => {
|
|
159
|
-
const e = emitter<number>();
|
|
160
|
-
const listener1 = vi.fn();
|
|
161
|
-
const listener2 = vi.fn();
|
|
162
|
-
|
|
163
|
-
e.on(listener1);
|
|
164
|
-
e.on(listener2);
|
|
165
|
-
|
|
166
|
-
expect(e.size()).toBe(2);
|
|
167
|
-
|
|
168
|
-
e.clear();
|
|
169
|
-
|
|
170
|
-
expect(e.size()).toBe(0);
|
|
171
|
-
e.emit(1);
|
|
172
|
-
expect(listener1).not.toHaveBeenCalled();
|
|
173
|
-
expect(listener2).not.toHaveBeenCalled();
|
|
174
|
-
});
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
describe("emitAndClear", () => {
|
|
178
|
-
it("should emit to all listeners then clear", () => {
|
|
179
|
-
const e = emitter<number>();
|
|
180
|
-
const listener1 = vi.fn();
|
|
181
|
-
const listener2 = vi.fn();
|
|
182
|
-
|
|
183
|
-
e.on(listener1);
|
|
184
|
-
e.on(listener2);
|
|
185
|
-
|
|
186
|
-
e.emitAndClear(42);
|
|
187
|
-
|
|
188
|
-
expect(listener1).toHaveBeenCalledTimes(1);
|
|
189
|
-
expect(listener2).toHaveBeenCalledTimes(1);
|
|
190
|
-
expect(listener1).toHaveBeenCalledWith(42);
|
|
191
|
-
expect(listener2).toHaveBeenCalledWith(42);
|
|
192
|
-
expect(e.size()).toBe(0);
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
it("should be no-op when settled", () => {
|
|
196
|
-
const e = emitter<number>();
|
|
197
|
-
const listener = vi.fn();
|
|
198
|
-
|
|
199
|
-
e.on(listener);
|
|
200
|
-
e.settle(1);
|
|
201
|
-
listener.mockClear();
|
|
202
|
-
|
|
203
|
-
e.emitAndClear(2);
|
|
204
|
-
|
|
205
|
-
expect(listener).not.toHaveBeenCalled();
|
|
206
|
-
});
|
|
207
|
-
});
|
|
208
|
-
|
|
209
|
-
describe("emitAndClearLifo", () => {
|
|
210
|
-
it("should emit in reverse order then clear", () => {
|
|
211
|
-
const e = emitter<number>();
|
|
212
|
-
const order: number[] = [];
|
|
213
|
-
|
|
214
|
-
e.on(() => order.push(1));
|
|
215
|
-
e.on(() => order.push(2));
|
|
216
|
-
e.on(() => order.push(3));
|
|
217
|
-
|
|
218
|
-
e.emitAndClearLifo(42);
|
|
219
|
-
|
|
220
|
-
expect(order).toEqual([3, 2, 1]);
|
|
221
|
-
expect(e.size()).toBe(0);
|
|
222
|
-
});
|
|
223
|
-
|
|
224
|
-
it("should be no-op when settled", () => {
|
|
225
|
-
const e = emitter<number>();
|
|
226
|
-
const listener = vi.fn();
|
|
227
|
-
|
|
228
|
-
e.on(listener);
|
|
229
|
-
e.settle(1);
|
|
230
|
-
listener.mockClear();
|
|
231
|
-
|
|
232
|
-
e.emitAndClearLifo(2);
|
|
233
|
-
|
|
234
|
-
expect(listener).not.toHaveBeenCalled();
|
|
235
|
-
});
|
|
236
|
-
});
|
|
237
|
-
|
|
238
|
-
describe("settle", () => {
|
|
239
|
-
it("should emit to all listeners and clear", () => {
|
|
240
|
-
const e = emitter<number>();
|
|
241
|
-
const listener = vi.fn();
|
|
242
|
-
|
|
243
|
-
e.on(listener);
|
|
244
|
-
|
|
245
|
-
e.settle(99);
|
|
246
|
-
|
|
247
|
-
expect(listener).toHaveBeenCalledWith(99);
|
|
248
|
-
expect(e.size()).toBe(0);
|
|
249
|
-
});
|
|
250
|
-
|
|
251
|
-
it("should mark emitter as settled", () => {
|
|
252
|
-
const e = emitter<number>();
|
|
253
|
-
|
|
254
|
-
expect(e.settled()).toBe(false);
|
|
255
|
-
|
|
256
|
-
e.settle(1);
|
|
257
|
-
|
|
258
|
-
expect(e.settled()).toBe(true);
|
|
259
|
-
});
|
|
260
|
-
|
|
261
|
-
it("should be no-op if already settled", () => {
|
|
262
|
-
const e = emitter<number>();
|
|
263
|
-
const listener = vi.fn();
|
|
264
|
-
|
|
265
|
-
e.settle(1);
|
|
266
|
-
|
|
267
|
-
e.on(listener);
|
|
268
|
-
listener.mockClear();
|
|
269
|
-
|
|
270
|
-
e.settle(2);
|
|
271
|
-
|
|
272
|
-
expect(listener).not.toHaveBeenCalled();
|
|
273
|
-
});
|
|
274
|
-
|
|
275
|
-
it("should call new subscribers immediately with settled value", () => {
|
|
276
|
-
const e = emitter<number>();
|
|
277
|
-
|
|
278
|
-
e.settle(42);
|
|
279
|
-
|
|
280
|
-
const listener = vi.fn();
|
|
281
|
-
const unsub = e.on(listener);
|
|
282
|
-
|
|
283
|
-
expect(listener).toHaveBeenCalledWith(42);
|
|
284
|
-
expect(unsub).toBeDefined();
|
|
285
|
-
// Unsub should be no-op for settled emitters
|
|
286
|
-
unsub();
|
|
287
|
-
});
|
|
288
|
-
|
|
289
|
-
it("should call new mapped subscribers immediately with settled value", () => {
|
|
290
|
-
const e = emitter<number>();
|
|
291
|
-
|
|
292
|
-
e.settle(10);
|
|
293
|
-
|
|
294
|
-
const listener = vi.fn();
|
|
295
|
-
e.on((n) => ({ value: n * 2 }), listener);
|
|
296
|
-
|
|
297
|
-
expect(listener).toHaveBeenCalledWith(20);
|
|
298
|
-
});
|
|
299
|
-
|
|
300
|
-
it("should call multiple new subscribers immediately when settled", () => {
|
|
301
|
-
const e = emitter<number>();
|
|
302
|
-
|
|
303
|
-
e.settle(5);
|
|
304
|
-
|
|
305
|
-
const listener1 = vi.fn();
|
|
306
|
-
const listener2 = vi.fn();
|
|
307
|
-
e.on([listener1, listener2]);
|
|
308
|
-
|
|
309
|
-
expect(listener1).toHaveBeenCalledWith(5);
|
|
310
|
-
expect(listener2).toHaveBeenCalledWith(5);
|
|
311
|
-
});
|
|
312
|
-
});
|
|
313
|
-
|
|
314
|
-
describe("emit when settled", () => {
|
|
315
|
-
it("should be no-op", () => {
|
|
316
|
-
const e = emitter<number>();
|
|
317
|
-
const listener = vi.fn();
|
|
318
|
-
|
|
319
|
-
e.on(listener);
|
|
320
|
-
e.settle(1);
|
|
321
|
-
listener.mockClear();
|
|
322
|
-
|
|
323
|
-
e.emit(2);
|
|
324
|
-
|
|
325
|
-
expect(listener).not.toHaveBeenCalled();
|
|
326
|
-
});
|
|
327
|
-
});
|
|
328
|
-
|
|
329
|
-
describe("edge cases", () => {
|
|
330
|
-
it("should handle emit with no listeners", () => {
|
|
331
|
-
const e = emitter<number>();
|
|
332
|
-
|
|
333
|
-
// Should not throw
|
|
334
|
-
expect(() => e.emit(1)).not.toThrow();
|
|
335
|
-
expect(() => e.emitLifo(1)).not.toThrow();
|
|
336
|
-
expect(() => e.emitAndClear(1)).not.toThrow();
|
|
337
|
-
expect(() => e.emitAndClearLifo(1)).not.toThrow();
|
|
338
|
-
expect(() => e.settle(1)).not.toThrow();
|
|
339
|
-
});
|
|
340
|
-
|
|
341
|
-
it("should handle void emitter", () => {
|
|
342
|
-
const e = emitter();
|
|
343
|
-
const listener = vi.fn();
|
|
344
|
-
|
|
345
|
-
e.on(listener);
|
|
346
|
-
e.emit();
|
|
347
|
-
|
|
348
|
-
expect(listener).toHaveBeenCalledWith(undefined);
|
|
349
|
-
});
|
|
350
|
-
|
|
351
|
-
it("should pass on() as a callback without losing context", () => {
|
|
352
|
-
const e = emitter<number>();
|
|
353
|
-
const listener = vi.fn();
|
|
354
|
-
|
|
355
|
-
// Use the bound 'on' method
|
|
356
|
-
const { on } = e;
|
|
357
|
-
on(listener);
|
|
358
|
-
|
|
359
|
-
e.emit(42);
|
|
360
|
-
|
|
361
|
-
expect(listener).toHaveBeenCalledWith(42);
|
|
362
|
-
});
|
|
363
|
-
});
|
|
364
|
-
});
|