atomirx 0.0.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 +1666 -0
- package/coverage/base.css +224 -0
- package/coverage/block-navigation.js +87 -0
- package/coverage/clover.xml +1440 -0
- package/coverage/coverage-final.json +14 -0
- package/coverage/favicon.png +0 -0
- package/coverage/index.html +131 -0
- package/coverage/prettify.css +1 -0
- package/coverage/prettify.js +2 -0
- package/coverage/sort-arrow-sprite.png +0 -0
- package/coverage/sorter.js +210 -0
- package/coverage/src/core/atom.ts.html +889 -0
- package/coverage/src/core/batch.ts.html +223 -0
- package/coverage/src/core/define.ts.html +805 -0
- package/coverage/src/core/emitter.ts.html +919 -0
- package/coverage/src/core/equality.ts.html +631 -0
- package/coverage/src/core/hook.ts.html +460 -0
- package/coverage/src/core/index.html +281 -0
- package/coverage/src/core/isAtom.ts.html +100 -0
- package/coverage/src/core/isPromiseLike.ts.html +133 -0
- package/coverage/src/core/onCreateHook.ts.html +136 -0
- package/coverage/src/core/scheduleNotifyHook.ts.html +94 -0
- package/coverage/src/core/types.ts.html +523 -0
- package/coverage/src/core/withUse.ts.html +253 -0
- package/coverage/src/index.html +116 -0
- package/coverage/src/index.ts.html +106 -0
- package/dist/core/atom.d.ts +63 -0
- package/dist/core/atom.test.d.ts +1 -0
- package/dist/core/atomState.d.ts +104 -0
- package/dist/core/atomState.test.d.ts +1 -0
- package/dist/core/batch.d.ts +126 -0
- package/dist/core/batch.test.d.ts +1 -0
- package/dist/core/define.d.ts +173 -0
- package/dist/core/define.test.d.ts +1 -0
- package/dist/core/derived.d.ts +102 -0
- package/dist/core/derived.test.d.ts +1 -0
- package/dist/core/effect.d.ts +120 -0
- package/dist/core/effect.test.d.ts +1 -0
- package/dist/core/emitter.d.ts +237 -0
- package/dist/core/emitter.test.d.ts +1 -0
- package/dist/core/equality.d.ts +62 -0
- package/dist/core/equality.test.d.ts +1 -0
- package/dist/core/hook.d.ts +134 -0
- package/dist/core/hook.test.d.ts +1 -0
- package/dist/core/isAtom.d.ts +9 -0
- package/dist/core/isPromiseLike.d.ts +9 -0
- package/dist/core/isPromiseLike.test.d.ts +1 -0
- package/dist/core/onCreateHook.d.ts +79 -0
- package/dist/core/promiseCache.d.ts +134 -0
- package/dist/core/promiseCache.test.d.ts +1 -0
- package/dist/core/scheduleNotifyHook.d.ts +51 -0
- package/dist/core/select.d.ts +151 -0
- package/dist/core/selector.test.d.ts +1 -0
- package/dist/core/types.d.ts +279 -0
- package/dist/core/withUse.d.ts +38 -0
- package/dist/core/withUse.test.d.ts +1 -0
- package/dist/index-2ok7ilik.js +1217 -0
- package/dist/index-B_5SFzfl.cjs +1 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +20 -0
- package/dist/index.test.d.ts +1 -0
- package/dist/react/index.cjs +30 -0
- package/dist/react/index.d.ts +7 -0
- package/dist/react/index.js +823 -0
- package/dist/react/rx.d.ts +250 -0
- package/dist/react/rx.test.d.ts +1 -0
- package/dist/react/strictModeTest.d.ts +10 -0
- package/dist/react/useAction.d.ts +381 -0
- package/dist/react/useAction.test.d.ts +1 -0
- package/dist/react/useStable.d.ts +183 -0
- package/dist/react/useStable.test.d.ts +1 -0
- package/dist/react/useValue.d.ts +134 -0
- package/dist/react/useValue.test.d.ts +1 -0
- package/package.json +57 -0
- package/scripts/publish.js +198 -0
- package/src/core/atom.test.ts +369 -0
- package/src/core/atom.ts +189 -0
- package/src/core/atomState.test.ts +342 -0
- package/src/core/atomState.ts +256 -0
- package/src/core/batch.test.ts +257 -0
- package/src/core/batch.ts +172 -0
- package/src/core/define.test.ts +342 -0
- package/src/core/define.ts +243 -0
- package/src/core/derived.test.ts +381 -0
- package/src/core/derived.ts +339 -0
- package/src/core/effect.test.ts +196 -0
- package/src/core/effect.ts +184 -0
- package/src/core/emitter.test.ts +364 -0
- package/src/core/emitter.ts +392 -0
- package/src/core/equality.test.ts +392 -0
- package/src/core/equality.ts +182 -0
- package/src/core/hook.test.ts +227 -0
- package/src/core/hook.ts +177 -0
- package/src/core/isAtom.ts +27 -0
- package/src/core/isPromiseLike.test.ts +72 -0
- package/src/core/isPromiseLike.ts +16 -0
- package/src/core/onCreateHook.ts +92 -0
- package/src/core/promiseCache.test.ts +239 -0
- package/src/core/promiseCache.ts +279 -0
- package/src/core/scheduleNotifyHook.ts +53 -0
- package/src/core/select.ts +454 -0
- package/src/core/selector.test.ts +257 -0
- package/src/core/types.ts +311 -0
- package/src/core/withUse.test.ts +249 -0
- package/src/core/withUse.ts +56 -0
- package/src/index.test.ts +80 -0
- package/src/index.ts +51 -0
- package/src/react/index.ts +20 -0
- package/src/react/rx.test.tsx +416 -0
- package/src/react/rx.tsx +300 -0
- package/src/react/strictModeTest.tsx +71 -0
- package/src/react/useAction.test.ts +989 -0
- package/src/react/useAction.ts +605 -0
- package/src/react/useStable.test.ts +553 -0
- package/src/react/useStable.ts +288 -0
- package/src/react/useValue.test.ts +182 -0
- package/src/react/useValue.ts +261 -0
- package/tsconfig.json +9 -0
- package/v2.md +725 -0
- package/vite.config.ts +39 -0
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from "vitest";
|
|
2
|
+
import { atom } from "./atom";
|
|
3
|
+
import { SYMBOL_ATOM } from "./types";
|
|
4
|
+
|
|
5
|
+
describe("atom", () => {
|
|
6
|
+
describe("basic functionality", () => {
|
|
7
|
+
it("should create an atom with initial value", () => {
|
|
8
|
+
const count$ = atom(0);
|
|
9
|
+
expect(count$.value).toBe(0);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it("should have SYMBOL_ATOM marker", () => {
|
|
13
|
+
const count$ = atom(0);
|
|
14
|
+
expect(count$[SYMBOL_ATOM]).toBe(true);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("should set value directly", () => {
|
|
18
|
+
const count$ = atom(0);
|
|
19
|
+
count$.set(5);
|
|
20
|
+
expect(count$.value).toBe(5);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("should set value with reducer", () => {
|
|
24
|
+
const count$ = atom(0);
|
|
25
|
+
count$.set((prev) => prev + 1);
|
|
26
|
+
expect(count$.value).toBe(1);
|
|
27
|
+
count$.set((prev) => prev * 2);
|
|
28
|
+
expect(count$.value).toBe(2);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("should reset to initial value", () => {
|
|
32
|
+
const count$ = atom(10);
|
|
33
|
+
count$.set(42);
|
|
34
|
+
expect(count$.value).toBe(42);
|
|
35
|
+
count$.reset();
|
|
36
|
+
expect(count$.value).toBe(10);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("should store objects", () => {
|
|
40
|
+
const user$ = atom({ name: "John", age: 30 });
|
|
41
|
+
expect(user$.value).toEqual({ name: "John", age: 30 });
|
|
42
|
+
user$.set({ name: "Jane", age: 25 });
|
|
43
|
+
expect(user$.value).toEqual({ name: "Jane", age: 25 });
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("should store null and undefined", () => {
|
|
47
|
+
const nullable$ = atom<string | null>(null);
|
|
48
|
+
expect(nullable$.value).toBe(null);
|
|
49
|
+
nullable$.set("hello");
|
|
50
|
+
expect(nullable$.value).toBe("hello");
|
|
51
|
+
nullable$.set(null);
|
|
52
|
+
expect(nullable$.value).toBe(null);
|
|
53
|
+
|
|
54
|
+
const undef$ = atom<string | undefined>(undefined);
|
|
55
|
+
expect(undef$.value).toBe(undefined);
|
|
56
|
+
undef$.set("world");
|
|
57
|
+
expect(undef$.value).toBe("world");
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("should store arrays", () => {
|
|
61
|
+
const items$ = atom<number[]>([1, 2, 3]);
|
|
62
|
+
expect(items$.value).toEqual([1, 2, 3]);
|
|
63
|
+
items$.set((prev) => [...prev, 4]);
|
|
64
|
+
expect(items$.value).toEqual([1, 2, 3, 4]);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
describe("subscriptions", () => {
|
|
69
|
+
it("should notify subscribers on value change", () => {
|
|
70
|
+
const count$ = atom(0);
|
|
71
|
+
const listener = vi.fn();
|
|
72
|
+
count$.on(listener);
|
|
73
|
+
count$.set(1);
|
|
74
|
+
expect(listener).toHaveBeenCalledTimes(1);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("should not notify if value is the same (strict equality)", () => {
|
|
78
|
+
const count$ = atom(5);
|
|
79
|
+
const listener = vi.fn();
|
|
80
|
+
count$.on(listener);
|
|
81
|
+
count$.set(5);
|
|
82
|
+
expect(listener).not.toHaveBeenCalled();
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("should allow unsubscribing", () => {
|
|
86
|
+
const count$ = atom(0);
|
|
87
|
+
const listener = vi.fn();
|
|
88
|
+
const unsub = count$.on(listener);
|
|
89
|
+
count$.set(1);
|
|
90
|
+
expect(listener).toHaveBeenCalledTimes(1);
|
|
91
|
+
unsub();
|
|
92
|
+
count$.set(2);
|
|
93
|
+
expect(listener).toHaveBeenCalledTimes(1);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("should support multiple subscribers", () => {
|
|
97
|
+
const count$ = atom(0);
|
|
98
|
+
const listener1 = vi.fn();
|
|
99
|
+
const listener2 = vi.fn();
|
|
100
|
+
count$.on(listener1);
|
|
101
|
+
count$.on(listener2);
|
|
102
|
+
count$.set(1);
|
|
103
|
+
expect(listener1).toHaveBeenCalledTimes(1);
|
|
104
|
+
expect(listener2).toHaveBeenCalledTimes(1);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("should notify on reset if value changed", () => {
|
|
108
|
+
const count$ = atom(0);
|
|
109
|
+
const listener = vi.fn();
|
|
110
|
+
count$.set(5);
|
|
111
|
+
count$.on(listener);
|
|
112
|
+
count$.reset();
|
|
113
|
+
expect(listener).toHaveBeenCalledTimes(1);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("should not notify on reset if already at initial value", () => {
|
|
117
|
+
const count$ = atom(0);
|
|
118
|
+
const listener = vi.fn();
|
|
119
|
+
count$.on(listener);
|
|
120
|
+
count$.reset();
|
|
121
|
+
expect(listener).not.toHaveBeenCalled();
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
describe("dirty", () => {
|
|
126
|
+
it("should return false when just initialized", () => {
|
|
127
|
+
const count$ = atom(42);
|
|
128
|
+
expect(count$.dirty()).toBe(false);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it("should return true after value is set", () => {
|
|
132
|
+
const count$ = atom(0);
|
|
133
|
+
expect(count$.dirty()).toBe(false);
|
|
134
|
+
|
|
135
|
+
count$.set(10);
|
|
136
|
+
expect(count$.dirty()).toBe(true);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("should return false after reset", () => {
|
|
140
|
+
const count$ = atom(0);
|
|
141
|
+
count$.set(10);
|
|
142
|
+
expect(count$.dirty()).toBe(true);
|
|
143
|
+
|
|
144
|
+
count$.reset();
|
|
145
|
+
expect(count$.dirty()).toBe(false);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it("should stay dirty after multiple sets", () => {
|
|
149
|
+
const count$ = atom(0);
|
|
150
|
+
count$.set(1);
|
|
151
|
+
count$.set(2);
|
|
152
|
+
count$.set(3);
|
|
153
|
+
expect(count$.dirty()).toBe(true);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it("should not become dirty if set to same value (equality check)", () => {
|
|
157
|
+
const count$ = atom(42);
|
|
158
|
+
count$.set(42); // Same value, equality check prevents change
|
|
159
|
+
expect(count$.dirty()).toBe(false);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it("should work with objects and shallow equality", () => {
|
|
163
|
+
const obj$ = atom({ a: 1 }, { equals: "shallow" });
|
|
164
|
+
expect(obj$.dirty()).toBe(false);
|
|
165
|
+
|
|
166
|
+
obj$.set({ a: 1 }); // Shallow equal, no change
|
|
167
|
+
expect(obj$.dirty()).toBe(false);
|
|
168
|
+
|
|
169
|
+
obj$.set({ a: 2 }); // Different value
|
|
170
|
+
expect(obj$.dirty()).toBe(true);
|
|
171
|
+
|
|
172
|
+
obj$.reset();
|
|
173
|
+
expect(obj$.dirty()).toBe(false);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it("should be useful for tracking unsaved changes", () => {
|
|
177
|
+
const form$ = atom({ name: "", email: "" });
|
|
178
|
+
|
|
179
|
+
expect(form$.dirty()).toBe(false); // No changes yet
|
|
180
|
+
|
|
181
|
+
form$.set({ name: "John", email: "" });
|
|
182
|
+
expect(form$.dirty()).toBe(true); // Has unsaved changes
|
|
183
|
+
|
|
184
|
+
form$.reset();
|
|
185
|
+
expect(form$.dirty()).toBe(false); // Changes discarded
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
describe("equality options", () => {
|
|
190
|
+
it("should use strict equality by default", () => {
|
|
191
|
+
const obj$ = atom({ a: 1 });
|
|
192
|
+
const listener = vi.fn();
|
|
193
|
+
obj$.on(listener);
|
|
194
|
+
obj$.set({ a: 1 }); // Different object, same content
|
|
195
|
+
expect(listener).toHaveBeenCalledTimes(1);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it("should support shallow equality", () => {
|
|
199
|
+
const obj$ = atom({ a: 1 }, { equals: "shallow" });
|
|
200
|
+
const listener = vi.fn();
|
|
201
|
+
obj$.on(listener);
|
|
202
|
+
obj$.set({ a: 1 }); // Same content
|
|
203
|
+
expect(listener).not.toHaveBeenCalled();
|
|
204
|
+
obj$.set({ a: 2 }); // Different content
|
|
205
|
+
expect(listener).toHaveBeenCalledTimes(1);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it("should support custom equality function", () => {
|
|
209
|
+
const user$ = atom(
|
|
210
|
+
{ id: 1, name: "John" },
|
|
211
|
+
{ equals: (a, b) => a.id === b.id }
|
|
212
|
+
);
|
|
213
|
+
const listener = vi.fn();
|
|
214
|
+
user$.on(listener);
|
|
215
|
+
user$.set({ id: 1, name: "Jane" }); // Same id
|
|
216
|
+
expect(listener).not.toHaveBeenCalled();
|
|
217
|
+
user$.set({ id: 2, name: "Jane" }); // Different id
|
|
218
|
+
expect(listener).toHaveBeenCalledTimes(1);
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
describe("Promise storage (raw)", () => {
|
|
223
|
+
it("should store Promise as-is", () => {
|
|
224
|
+
const promise = Promise.resolve(42);
|
|
225
|
+
const data$ = atom(promise);
|
|
226
|
+
expect(data$.value).toBe(promise);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it("should store a new Promise on set", () => {
|
|
230
|
+
const promise1 = Promise.resolve(1);
|
|
231
|
+
const promise2 = Promise.resolve(2);
|
|
232
|
+
const data$ = atom(promise1);
|
|
233
|
+
expect(data$.value).toBe(promise1);
|
|
234
|
+
data$.set(promise2);
|
|
235
|
+
expect(data$.value).toBe(promise2);
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it("should reset to original Promise object", () => {
|
|
239
|
+
const originalPromise = Promise.resolve(42);
|
|
240
|
+
const data$ = atom(originalPromise);
|
|
241
|
+
data$.set(Promise.resolve(100));
|
|
242
|
+
expect(data$.value).not.toBe(originalPromise);
|
|
243
|
+
data$.reset();
|
|
244
|
+
expect(data$.value).toBe(originalPromise);
|
|
245
|
+
});
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
describe("reducer errors", () => {
|
|
249
|
+
it("should throw synchronously if reducer throws", () => {
|
|
250
|
+
const count$ = atom(0);
|
|
251
|
+
const error = new Error("Reducer failed");
|
|
252
|
+
expect(() => {
|
|
253
|
+
count$.set(() => {
|
|
254
|
+
throw error;
|
|
255
|
+
});
|
|
256
|
+
}).toThrow(error);
|
|
257
|
+
// Value should remain unchanged
|
|
258
|
+
expect(count$.value).toBe(0);
|
|
259
|
+
});
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
describe("lazy initialization", () => {
|
|
263
|
+
it("should support lazy initializer function", () => {
|
|
264
|
+
const initializer = vi.fn(() => 42);
|
|
265
|
+
const count$ = atom(initializer);
|
|
266
|
+
expect(initializer).toHaveBeenCalledTimes(1);
|
|
267
|
+
expect(count$.value).toBe(42);
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
it("should only call initializer once", () => {
|
|
271
|
+
const initializer = vi.fn(() => ({ data: "expensive" }));
|
|
272
|
+
const obj$ = atom(initializer);
|
|
273
|
+
expect(initializer).toHaveBeenCalledTimes(1);
|
|
274
|
+
// Access value multiple times
|
|
275
|
+
obj$.value;
|
|
276
|
+
obj$.value;
|
|
277
|
+
expect(initializer).toHaveBeenCalledTimes(1);
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
it("should re-run initializer on reset", () => {
|
|
281
|
+
let callCount = 0;
|
|
282
|
+
const initializer = () => {
|
|
283
|
+
callCount++;
|
|
284
|
+
return callCount; // Returns different value each call
|
|
285
|
+
};
|
|
286
|
+
const count$ = atom(initializer);
|
|
287
|
+
expect(count$.value).toBe(1); // First call returns 1
|
|
288
|
+
expect(callCount).toBe(1);
|
|
289
|
+
|
|
290
|
+
count$.set(100);
|
|
291
|
+
expect(count$.value).toBe(100);
|
|
292
|
+
|
|
293
|
+
count$.reset();
|
|
294
|
+
expect(count$.value).toBe(2); // Re-runs initializer, gets fresh value
|
|
295
|
+
expect(callCount).toBe(2); // Initializer called again
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
it("should work with lazy initializer returning object", () => {
|
|
299
|
+
const obj$ = atom(() => ({ count: 0, items: [] as number[] }));
|
|
300
|
+
expect(obj$.value).toEqual({ count: 0, items: [] });
|
|
301
|
+
obj$.set((prev) => ({ ...prev, count: 1 }));
|
|
302
|
+
expect(obj$.value).toEqual({ count: 1, items: [] });
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
it("should work with lazy initializer returning Promise", () => {
|
|
306
|
+
const promise = Promise.resolve(42);
|
|
307
|
+
const data$ = atom(() => promise);
|
|
308
|
+
expect(data$.value).toBe(promise);
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
it("should still work with direct value (non-function)", () => {
|
|
312
|
+
const count$ = atom(10);
|
|
313
|
+
expect(count$.value).toBe(10);
|
|
314
|
+
});
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
describe("metadata", () => {
|
|
318
|
+
it("should store meta", () => {
|
|
319
|
+
const count$ = atom(0, { meta: { key: "count" } });
|
|
320
|
+
expect(count$.meta).toEqual({ key: "count" });
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
it("should have undefined meta if not provided", () => {
|
|
324
|
+
const count$ = atom(0);
|
|
325
|
+
expect(count$.meta).toBe(undefined);
|
|
326
|
+
});
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
describe("plugin system (use)", () => {
|
|
330
|
+
it("should support .use() for extensions", () => {
|
|
331
|
+
// Note: Don't use ...source spread as it copies values, not getters
|
|
332
|
+
// Instead, access source properties through the reference
|
|
333
|
+
const base$ = atom(0);
|
|
334
|
+
const count$ = base$.use((source) => ({
|
|
335
|
+
get value() {
|
|
336
|
+
return source.value;
|
|
337
|
+
},
|
|
338
|
+
set: source.set,
|
|
339
|
+
reset: source.reset,
|
|
340
|
+
on: source.on,
|
|
341
|
+
increment: () => source.set((v) => v + 1),
|
|
342
|
+
}));
|
|
343
|
+
|
|
344
|
+
expect(count$.value).toBe(0);
|
|
345
|
+
count$.increment();
|
|
346
|
+
expect(count$.value).toBe(1);
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
it("should support .use() with source reference pattern", () => {
|
|
350
|
+
// Simpler pattern: just reference the original atom
|
|
351
|
+
const base$ = atom(0);
|
|
352
|
+
const enhanced$ = base$.use(() => ({
|
|
353
|
+
get value() {
|
|
354
|
+
return base$.value;
|
|
355
|
+
},
|
|
356
|
+
increment: () => base$.set((v) => v + 1),
|
|
357
|
+
decrement: () => base$.set((v) => v - 1),
|
|
358
|
+
}));
|
|
359
|
+
|
|
360
|
+
expect(enhanced$.value).toBe(0);
|
|
361
|
+
enhanced$.increment();
|
|
362
|
+
expect(enhanced$.value).toBe(1);
|
|
363
|
+
enhanced$.increment();
|
|
364
|
+
expect(enhanced$.value).toBe(2);
|
|
365
|
+
enhanced$.decrement();
|
|
366
|
+
expect(enhanced$.value).toBe(1);
|
|
367
|
+
});
|
|
368
|
+
});
|
|
369
|
+
});
|
package/src/core/atom.ts
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { onCreateHook } from "./onCreateHook";
|
|
2
|
+
import { emitter } from "./emitter";
|
|
3
|
+
import { resolveEquality } from "./equality";
|
|
4
|
+
import { scheduleNotifyHook } from "./scheduleNotifyHook";
|
|
5
|
+
import { AtomOptions, MutableAtom, SYMBOL_ATOM, Equality } from "./types";
|
|
6
|
+
import { withUse } from "./withUse";
|
|
7
|
+
import { isPromiseLike } from "./isPromiseLike";
|
|
8
|
+
import { trackPromise } from "./promiseCache";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Creates a mutable atom - a reactive state container that holds a single value.
|
|
12
|
+
*
|
|
13
|
+
* MutableAtom is a raw storage container. It stores values as-is, including Promises.
|
|
14
|
+
* If you store a Promise, `.value` returns the Promise object itself.
|
|
15
|
+
*
|
|
16
|
+
* Features:
|
|
17
|
+
* - Raw storage: stores any value including Promises
|
|
18
|
+
* - Lazy initialization: pass a function to defer computation
|
|
19
|
+
* - Equality checking: configurable equality for reducer-based updates
|
|
20
|
+
* - Plugin system: chainable `.use()` method for extensions
|
|
21
|
+
* - Subscriptions: `.on()` for change notifications
|
|
22
|
+
*
|
|
23
|
+
* @template T - The type of value stored in the atom
|
|
24
|
+
* @param valueOrInit - Initial value or lazy initializer function `() => T`
|
|
25
|
+
* @param options - Configuration options
|
|
26
|
+
* @param options.meta - Optional metadata for debugging/devtools
|
|
27
|
+
* @param options.equals - Equality strategy for change detection (default: strict)
|
|
28
|
+
* @returns A mutable atom with value, set/reset methods
|
|
29
|
+
*
|
|
30
|
+
* @example Synchronous value
|
|
31
|
+
* ```ts
|
|
32
|
+
* const count = atom(0);
|
|
33
|
+
* count.set(1);
|
|
34
|
+
* count.set(prev => prev + 1);
|
|
35
|
+
* console.log(count.value); // 2
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* @example Lazy initialization
|
|
39
|
+
* ```ts
|
|
40
|
+
* // Initial value computed at creation
|
|
41
|
+
* const config = atom(() => parseExpensiveConfig());
|
|
42
|
+
*
|
|
43
|
+
* // reset() re-runs the initializer for fresh values
|
|
44
|
+
* const timestamp = atom(() => Date.now());
|
|
45
|
+
* timestamp.reset(); // Gets new timestamp
|
|
46
|
+
*
|
|
47
|
+
* // To store a function as value, wrap it:
|
|
48
|
+
* const callback = atom(() => () => console.log('hello'));
|
|
49
|
+
* ```
|
|
50
|
+
*
|
|
51
|
+
* @example Async value (stores Promise as-is)
|
|
52
|
+
* ```ts
|
|
53
|
+
* const posts = atom(fetchPosts());
|
|
54
|
+
* posts.value; // Promise<Post[]>
|
|
55
|
+
*
|
|
56
|
+
* // Refetch - set a new Promise
|
|
57
|
+
* posts.set(fetchPosts());
|
|
58
|
+
*
|
|
59
|
+
* // Reset with direct value - restores original Promise (does NOT refetch)
|
|
60
|
+
* // Reset with lazy init - re-runs initializer (DOES refetch)
|
|
61
|
+
* const lazyPosts = atom(() => fetchPosts());
|
|
62
|
+
* lazyPosts.reset(); // Refetches!
|
|
63
|
+
* ```
|
|
64
|
+
*
|
|
65
|
+
* @example With equals option
|
|
66
|
+
* ```ts
|
|
67
|
+
* const state = atom({ count: 0 }, { equals: "shallow" });
|
|
68
|
+
* state.set(prev => ({ ...prev })); // No notification (shallow equal)
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
export function atom<T>(
|
|
72
|
+
valueOrInit: T | (() => T),
|
|
73
|
+
options: AtomOptions<T> = {}
|
|
74
|
+
): MutableAtom<T> {
|
|
75
|
+
const changeEmitter = emitter();
|
|
76
|
+
const eq = resolveEquality(options.equals as Equality<unknown>);
|
|
77
|
+
|
|
78
|
+
// Resolve initial value (supports lazy initialization)
|
|
79
|
+
const initialValue: T =
|
|
80
|
+
typeof valueOrInit === "function"
|
|
81
|
+
? (valueOrInit as () => T)()
|
|
82
|
+
: valueOrInit;
|
|
83
|
+
|
|
84
|
+
// Current value
|
|
85
|
+
let value: T = initialValue;
|
|
86
|
+
|
|
87
|
+
// Track if value has changed since init/reset
|
|
88
|
+
let isDirty = false;
|
|
89
|
+
|
|
90
|
+
isPromiseLike(value) && trackPromise(value);
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Schedules notification to all subscribers.
|
|
94
|
+
*/
|
|
95
|
+
const notify = () => {
|
|
96
|
+
changeEmitter.forEach((listener) => {
|
|
97
|
+
scheduleNotifyHook.current(listener);
|
|
98
|
+
});
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Updates the atom's value.
|
|
103
|
+
*
|
|
104
|
+
* @param newValue - New value or reducer function (prev) => newValue
|
|
105
|
+
*/
|
|
106
|
+
const set = (newValue: T | ((prev: T) => T)) => {
|
|
107
|
+
let nextValue: T;
|
|
108
|
+
|
|
109
|
+
if (typeof newValue === "function") {
|
|
110
|
+
// Reducer function
|
|
111
|
+
nextValue = (newValue as (prev: T) => T)(value);
|
|
112
|
+
} else {
|
|
113
|
+
nextValue = newValue;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Check equality
|
|
117
|
+
if (eq(nextValue, value)) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
value = nextValue;
|
|
122
|
+
isDirty = true;
|
|
123
|
+
isPromiseLike(value) && trackPromise(value);
|
|
124
|
+
notify();
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Resets the atom to its initial value and clears dirty flag.
|
|
129
|
+
*/
|
|
130
|
+
const reset = () => {
|
|
131
|
+
// Re-run initializer if function, otherwise use initial value
|
|
132
|
+
const nextValue: T =
|
|
133
|
+
typeof valueOrInit === "function"
|
|
134
|
+
? (valueOrInit as () => T)()
|
|
135
|
+
: valueOrInit;
|
|
136
|
+
|
|
137
|
+
// Track promise if needed
|
|
138
|
+
isPromiseLike(nextValue) && trackPromise(nextValue);
|
|
139
|
+
|
|
140
|
+
// Check if value actually changed
|
|
141
|
+
const changed = !eq(nextValue, value);
|
|
142
|
+
|
|
143
|
+
value = nextValue;
|
|
144
|
+
isDirty = false; // Always clear dirty flag on reset
|
|
145
|
+
|
|
146
|
+
if (changed) {
|
|
147
|
+
notify();
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Returns true if the value has changed since initialization or last reset().
|
|
153
|
+
*/
|
|
154
|
+
const dirty = (): boolean => {
|
|
155
|
+
return isDirty;
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
// Create the atom object
|
|
159
|
+
const a = withUse({
|
|
160
|
+
[SYMBOL_ATOM]: true as const,
|
|
161
|
+
meta: options.meta,
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Current value (raw, including Promises).
|
|
165
|
+
*/
|
|
166
|
+
get value(): T {
|
|
167
|
+
return value;
|
|
168
|
+
},
|
|
169
|
+
|
|
170
|
+
set,
|
|
171
|
+
reset,
|
|
172
|
+
dirty,
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Subscribe to value changes.
|
|
176
|
+
*/
|
|
177
|
+
on: changeEmitter.on,
|
|
178
|
+
}) as MutableAtom<T>;
|
|
179
|
+
|
|
180
|
+
// Notify devtools/plugins of atom creation
|
|
181
|
+
onCreateHook.current?.({
|
|
182
|
+
type: "mutable",
|
|
183
|
+
key: options.meta?.key,
|
|
184
|
+
meta: options.meta,
|
|
185
|
+
atom: a as MutableAtom<unknown>,
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
return a;
|
|
189
|
+
}
|