bansa 0.0.25 → 0.0.26
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 +112 -85
- package/dist/atom.d.mts +10 -10
- package/dist/atom.d.mts.map +1 -1
- package/dist/atom.mjs +126 -85
- package/dist/atom.mjs.map +1 -1
- package/dist/browser/index.d.ts +10 -10
- package/dist/browser/index.d.ts.map +1 -1
- package/dist/browser/index.js +1 -1
- package/dist/browser/index.js.map +1 -1
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +2 -2
- package/dist/react.d.mts +25 -5
- package/dist/react.d.mts.map +1 -1
- package/dist/react.mjs +44 -18
- package/dist/react.mjs.map +1 -1
- package/dist/utils.mjs +5 -3
- package/dist/utils.mjs.map +1 -1
- package/package.json +7 -5
- package/tsconfig.json +2 -2
- package/.oxfmtrc.jsonc +0 -3
- package/README.ko.md +0 -490
- package/oxlint.config.ts +0 -17
- package/tsconfig.app.json +0 -39
- package/tsconfig.lib.json +0 -35
package/dist/atom.mjs
CHANGED
|
@@ -5,17 +5,18 @@ var CommonAtomInternal = class {
|
|
|
5
5
|
_children;
|
|
6
6
|
_watchers;
|
|
7
7
|
_subscribers;
|
|
8
|
+
_valueChanged = true;
|
|
8
9
|
get() {
|
|
9
|
-
if (!this.
|
|
10
|
+
if (!this.state.active) {
|
|
10
11
|
execute(this);
|
|
11
12
|
disableAtom(this);
|
|
12
13
|
}
|
|
13
|
-
if (this.state.error) throw this.state.error;
|
|
14
14
|
if (this.state.promise) throw this.state.promise;
|
|
15
|
+
if (this.state.error) throw this.state.error;
|
|
15
16
|
return this.state.value;
|
|
16
17
|
}
|
|
17
18
|
watch(watcher) {
|
|
18
|
-
if (!this.
|
|
19
|
+
if (!this.state.active) requestActivate(this);
|
|
19
20
|
(this._watchers ||= /* @__PURE__ */ new Set()).add(watcher);
|
|
20
21
|
return () => {
|
|
21
22
|
this._watchers.delete(watcher);
|
|
@@ -29,7 +30,7 @@ var CommonAtomInternal = class {
|
|
|
29
30
|
return (atomSubscriber._ctrl ||= createThenableSignal()).signal;
|
|
30
31
|
} }
|
|
31
32
|
};
|
|
32
|
-
if (!this.
|
|
33
|
+
if (!this.state.active) requestActivate(this);
|
|
33
34
|
else if (!this.state.error && !this.state.promise) try {
|
|
34
35
|
subscriber(this.state.value, atomSubscriber._options);
|
|
35
36
|
} catch (e) {
|
|
@@ -54,10 +55,10 @@ var PrimitiveAtomInternal = class extends CommonAtomInternal {
|
|
|
54
55
|
_marked = false;
|
|
55
56
|
constructor(init, options) {
|
|
56
57
|
super();
|
|
57
|
-
this._init = init;
|
|
58
|
+
this._nextValue = this._init = init;
|
|
58
59
|
this._equals = options?.equals;
|
|
59
|
-
this._nextValue = init;
|
|
60
60
|
this.state = {
|
|
61
|
+
active: true,
|
|
61
62
|
promise: void 0,
|
|
62
63
|
error: void 0,
|
|
63
64
|
value: init
|
|
@@ -65,24 +66,26 @@ var PrimitiveAtomInternal = class extends CommonAtomInternal {
|
|
|
65
66
|
}
|
|
66
67
|
set(value) {
|
|
67
68
|
const nextValue = value instanceof Function ? value(this._nextValue) : value;
|
|
68
|
-
if (!
|
|
69
|
+
if (!Object.is(nextValue, this._nextValue)) {
|
|
69
70
|
this._nextValue = nextValue;
|
|
70
71
|
requestPropagate(this);
|
|
71
72
|
}
|
|
72
73
|
}
|
|
73
74
|
};
|
|
74
75
|
PrimitiveAtomInternal.prototype._source = true;
|
|
75
|
-
PrimitiveAtomInternal.prototype.
|
|
76
|
+
PrimitiveAtomInternal.prototype._hasValue = true;
|
|
76
77
|
PrimitiveAtomInternal.prototype._needExecute = false;
|
|
77
78
|
var DerivedAtomInternal = class extends CommonAtomInternal {
|
|
78
|
-
|
|
79
|
+
_hasValue = false;
|
|
79
80
|
_needExecute = false;
|
|
80
81
|
_needPropagate = false;
|
|
81
82
|
_marked = false;
|
|
82
83
|
_counter = 0;
|
|
84
|
+
_resolve;
|
|
85
|
+
_reject;
|
|
83
86
|
_ctrl;
|
|
84
87
|
_dependencies;
|
|
85
|
-
|
|
88
|
+
_allDependencies;
|
|
86
89
|
constructor(init, options) {
|
|
87
90
|
super();
|
|
88
91
|
this._init = init;
|
|
@@ -93,15 +96,14 @@ var DerivedAtomInternal = class extends CommonAtomInternal {
|
|
|
93
96
|
return (self._ctrl ||= createThenableSignal()).signal;
|
|
94
97
|
} };
|
|
95
98
|
this.state = {
|
|
96
|
-
|
|
99
|
+
active: false,
|
|
100
|
+
promise: void 0,
|
|
97
101
|
error: void 0,
|
|
98
102
|
value: void 0
|
|
99
103
|
};
|
|
100
104
|
}
|
|
101
105
|
};
|
|
102
106
|
DerivedAtomInternal.prototype._source = false;
|
|
103
|
-
const inactive = Promise.reject();
|
|
104
|
-
inactive.catch(() => {});
|
|
105
107
|
const $ = (init, options) => {
|
|
106
108
|
if (init instanceof Function) return new DerivedAtomInternal(init, options);
|
|
107
109
|
return new PrimitiveAtomInternal(init, options);
|
|
@@ -118,7 +120,7 @@ const createScope = (parentScope, atomValuePairs) => {
|
|
|
118
120
|
const parentAtom = parentScope?.(baseAtom, true);
|
|
119
121
|
if (strict) return parentAtom;
|
|
120
122
|
const realBaseAtom = parentAtom || baseAtom;
|
|
121
|
-
atomMap.set(baseAtom, scopedAtom = realBaseAtom._init instanceof Function ? $((get, options) => realBaseAtom._init((atom
|
|
123
|
+
atomMap.set(baseAtom, scopedAtom = realBaseAtom._init instanceof Function ? $((get, options) => realBaseAtom._init((atom) => get(scope(atom)), options), {
|
|
122
124
|
equals: realBaseAtom._equals,
|
|
123
125
|
persist: realBaseAtom._persist
|
|
124
126
|
}) : parentAtom || $(realBaseAtom._init));
|
|
@@ -153,9 +155,31 @@ const updateAtoms = () => {
|
|
|
153
155
|
const updatedAtoms = updateQueue;
|
|
154
156
|
updateQueue = [];
|
|
155
157
|
for (const atom of updatedAtoms) {
|
|
156
|
-
atom.state.
|
|
157
|
-
|
|
158
|
-
|
|
158
|
+
if (atom.state.active) {
|
|
159
|
+
const prevSuccess = atom._hasValue && !atom.state.promise && !atom.state.error;
|
|
160
|
+
if (atom.state.error = atom._nextError) {
|
|
161
|
+
atom._nextValue = atom.state.value;
|
|
162
|
+
if (atom._reject) {
|
|
163
|
+
atom._reject(atom._nextError);
|
|
164
|
+
atom._resolve = atom._reject = atom.state.promise = void 0;
|
|
165
|
+
}
|
|
166
|
+
} else {
|
|
167
|
+
if (!atom._hasValue || !Object.is(atom._nextValue, atom.state.value) && !atom._equals?.(atom._nextValue, atom.state.value)) {
|
|
168
|
+
atom.state.value = atom._nextValue;
|
|
169
|
+
atom._valueChanged = atom._hasValue = true;
|
|
170
|
+
} else {
|
|
171
|
+
atom._nextValue = atom.state.value;
|
|
172
|
+
if (prevSuccess) {
|
|
173
|
+
atom._needPropagate = false;
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
if (atom._resolve) {
|
|
178
|
+
atom._resolve(atom._nextValue);
|
|
179
|
+
atom._resolve = atom._reject = atom.state.promise = void 0;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
159
183
|
mark(atom);
|
|
160
184
|
}
|
|
161
185
|
}
|
|
@@ -178,8 +202,25 @@ const propagate = (atom) => {
|
|
|
178
202
|
} catch (e) {
|
|
179
203
|
logError(e);
|
|
180
204
|
}
|
|
181
|
-
if (
|
|
182
|
-
if (atom.
|
|
205
|
+
if (atom.state.promise) {
|
|
206
|
+
if (atom._children) for (const child of atom._children) {
|
|
207
|
+
child.state.promise ??= new Promise((resolve, reject) => {
|
|
208
|
+
child._resolve = resolve;
|
|
209
|
+
child._reject = reject;
|
|
210
|
+
});
|
|
211
|
+
child._needPropagate = true;
|
|
212
|
+
}
|
|
213
|
+
} else if (atom.state.error) {
|
|
214
|
+
if (atom._children) for (const child of atom._children) {
|
|
215
|
+
child.state.error = child._nextError = atom.state.error;
|
|
216
|
+
if (child._reject) {
|
|
217
|
+
child._reject(child._nextError);
|
|
218
|
+
child._resolve = child._reject = child.state.promise = void 0;
|
|
219
|
+
}
|
|
220
|
+
child._needPropagate = true;
|
|
221
|
+
}
|
|
222
|
+
} else {
|
|
223
|
+
if (atom._valueChanged && atom._subscribers) for (const subscriber of atom._subscribers) {
|
|
183
224
|
if (subscriber._ctrl) {
|
|
184
225
|
subscriber._ctrl.abort();
|
|
185
226
|
subscriber._ctrl = void 0;
|
|
@@ -192,6 +233,7 @@ const propagate = (atom) => {
|
|
|
192
233
|
}
|
|
193
234
|
if (atom._children) for (const child of atom._children) child._needExecute = true;
|
|
194
235
|
}
|
|
236
|
+
atom._valueChanged = false;
|
|
195
237
|
};
|
|
196
238
|
const mark = (atom) => {
|
|
197
239
|
if (!atom._marked) {
|
|
@@ -207,81 +249,89 @@ var Wrapped = class {
|
|
|
207
249
|
}
|
|
208
250
|
};
|
|
209
251
|
const expired = Symbol();
|
|
252
|
+
const loading = Symbol();
|
|
210
253
|
const execute = (atom) => {
|
|
211
254
|
const counter = ++atom._counter;
|
|
212
|
-
const
|
|
213
|
-
atom.
|
|
255
|
+
const prevSuccess = atom._hasValue && !atom.state.promise && !atom.state.error;
|
|
256
|
+
atom.state.active = true;
|
|
214
257
|
atom._needExecute = false;
|
|
215
|
-
atom.
|
|
258
|
+
if (atom._dependencies) {
|
|
259
|
+
for (const dep of atom._dependencies) dep._children.delete(atom);
|
|
260
|
+
atom._dependencies.clear();
|
|
261
|
+
}
|
|
216
262
|
if (atom._ctrl) {
|
|
217
263
|
atom._ctrl.abort();
|
|
218
264
|
atom._ctrl = void 0;
|
|
219
265
|
}
|
|
220
266
|
try {
|
|
221
|
-
const value = atom._init((anotherAtom
|
|
267
|
+
const value = atom._init((anotherAtom) => {
|
|
222
268
|
if (counter !== atom._counter) throw expired;
|
|
223
269
|
if (atom !== anotherAtom) {
|
|
224
|
-
if (!anotherAtom.
|
|
225
|
-
|
|
226
|
-
|
|
270
|
+
if (!anotherAtom.state.active) execute(anotherAtom);
|
|
271
|
+
if (!atom._allDependencies) {
|
|
272
|
+
atom._allDependencies = /* @__PURE__ */ new Set();
|
|
273
|
+
atom._dependencies = /* @__PURE__ */ new Set();
|
|
227
274
|
}
|
|
228
|
-
|
|
275
|
+
atom._dependencies.add(anotherAtom);
|
|
276
|
+
atom._allDependencies.add(anotherAtom);
|
|
229
277
|
(anotherAtom._children ||= /* @__PURE__ */ new Set()).add(atom);
|
|
230
278
|
}
|
|
231
|
-
|
|
232
|
-
if (
|
|
233
|
-
if (
|
|
234
|
-
return
|
|
279
|
+
const { state } = anotherAtom;
|
|
280
|
+
if (state.promise) throw loading;
|
|
281
|
+
if (state.error) throw new Wrapped(state.error);
|
|
282
|
+
return state.value;
|
|
235
283
|
}, atom._options);
|
|
236
284
|
if (isPromiseLike(value)) {
|
|
237
|
-
atom.state.promise
|
|
285
|
+
atom.state.promise ??= new Promise((resolve, reject) => {
|
|
286
|
+
atom._resolve = resolve;
|
|
287
|
+
atom._reject = reject;
|
|
288
|
+
});
|
|
238
289
|
value.then((value) => {
|
|
239
290
|
if (counter === atom._counter) {
|
|
240
|
-
|
|
241
|
-
if (
|
|
242
|
-
|
|
243
|
-
atom._nextValue = value;
|
|
244
|
-
atom._nextError = void 0;
|
|
245
|
-
}
|
|
291
|
+
++atom._counter;
|
|
292
|
+
if (!atom._hasValue || !Object.is(value, atom._nextValue)) atom._nextValue = value;
|
|
293
|
+
atom._nextError = void 0;
|
|
246
294
|
requestPropagate(atom);
|
|
247
295
|
}
|
|
248
296
|
}, (e) => {
|
|
249
|
-
if (counter === atom._counter) {
|
|
250
|
-
|
|
251
|
-
if (e
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
297
|
+
if (counter === atom._counter && e !== expired) {
|
|
298
|
+
++atom._counter;
|
|
299
|
+
if (e !== loading) {
|
|
300
|
+
if (e instanceof Wrapped) e = e.e;
|
|
301
|
+
else logError(e);
|
|
302
|
+
atom._nextError = e;
|
|
303
|
+
requestPropagate(atom);
|
|
304
|
+
}
|
|
255
305
|
}
|
|
256
306
|
});
|
|
257
307
|
} else {
|
|
258
|
-
|
|
259
|
-
atom.
|
|
260
|
-
|
|
261
|
-
|
|
308
|
+
++atom._counter;
|
|
309
|
+
if (!atom._hasValue || !Object.is(value, atom._nextValue) && !atom._equals?.(value, atom._nextValue)) {
|
|
310
|
+
atom.state.value = atom._nextValue = value;
|
|
311
|
+
atom._valueChanged = atom._hasValue = true;
|
|
312
|
+
} else if (prevSuccess) atom._needPropagate = false;
|
|
313
|
+
atom.state.error = atom._nextError = void 0;
|
|
314
|
+
if (atom._resolve) {
|
|
315
|
+
atom._resolve(atom._nextValue);
|
|
316
|
+
atom._resolve = atom._reject = atom.state.promise = void 0;
|
|
317
|
+
}
|
|
262
318
|
}
|
|
263
319
|
} catch (e) {
|
|
264
|
-
|
|
265
|
-
if (e ===
|
|
320
|
+
++atom._counter;
|
|
321
|
+
if (e === loading) atom.state.promise ??= new Promise((resolve, reject) => {
|
|
322
|
+
atom._resolve = resolve;
|
|
323
|
+
atom._reject = reject;
|
|
324
|
+
});
|
|
266
325
|
else {
|
|
267
326
|
if (e instanceof Wrapped) e = e.e;
|
|
268
327
|
else logError(e);
|
|
269
|
-
atom.state.error = e;
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
++atom._counter;
|
|
275
|
-
const oldDependencies = atom._dependencies;
|
|
276
|
-
atom._dependencies = atom._nextDependencies;
|
|
277
|
-
if (oldDependencies) {
|
|
278
|
-
for (const dep of oldDependencies) if (!atom._dependencies?.has(dep)) {
|
|
279
|
-
dep._children.delete(atom);
|
|
280
|
-
disableAtom(dep);
|
|
328
|
+
atom.state.error = atom._nextError = e;
|
|
329
|
+
if (atom._reject) {
|
|
330
|
+
atom._reject(e);
|
|
331
|
+
atom._resolve = atom._reject = atom.state.promise = void 0;
|
|
332
|
+
}
|
|
281
333
|
}
|
|
282
|
-
oldDependencies.clear();
|
|
283
334
|
}
|
|
284
|
-
atom._nextDependencies = oldDependencies;
|
|
285
335
|
};
|
|
286
336
|
let runningGc = false;
|
|
287
337
|
let gcCandidates = /* @__PURE__ */ new Set();
|
|
@@ -296,32 +346,23 @@ const disableAtom = (atom) => {
|
|
|
296
346
|
};
|
|
297
347
|
const gc = () => {
|
|
298
348
|
for (const atom of gcCandidates) if (!atom._source && !atom._persist && !atom._children?.size && !atom._watchers?.size && !atom._subscribers?.size) {
|
|
299
|
-
atom.
|
|
300
|
-
atom.
|
|
301
|
-
atom.
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
dep._children.delete(atom);
|
|
309
|
-
disableAtom(dep);
|
|
310
|
-
}
|
|
311
|
-
atom._dependencies.clear();
|
|
312
|
-
if (atom._nextDependencies) {
|
|
313
|
-
for (const dep of atom._nextDependencies) {
|
|
314
|
-
dep._children.delete(atom);
|
|
315
|
-
disableAtom(dep);
|
|
316
|
-
}
|
|
317
|
-
atom._nextDependencies.clear();
|
|
349
|
+
atom._ctrl?.abort();
|
|
350
|
+
++atom._counter;
|
|
351
|
+
atom._nextValue = atom._nextError = atom.state.error = atom.state.value = atom.state.promise = atom._resolve = atom._reject = atom._ctrl = void 0;
|
|
352
|
+
atom._needPropagate = atom._needExecute = atom._hasValue = atom.state.active = false;
|
|
353
|
+
atom._valueChanged = atom._source;
|
|
354
|
+
if (atom._allDependencies) {
|
|
355
|
+
if (atom._dependencies) {
|
|
356
|
+
for (const dep of atom._dependencies) dep._children.delete(atom);
|
|
357
|
+
atom._dependencies.clear();
|
|
318
358
|
}
|
|
359
|
+
for (const dep of atom._allDependencies) disableAtom(dep);
|
|
360
|
+
atom._allDependencies.clear();
|
|
319
361
|
}
|
|
320
362
|
}
|
|
321
363
|
gcCandidates.clear();
|
|
322
364
|
runningGc = false;
|
|
323
365
|
};
|
|
324
|
-
const equals = (value, prevValue, equalsFn) => Object.is(value, prevValue) || equalsFn !== void 0 && prevValue !== void 0 && equalsFn(value, prevValue);
|
|
325
366
|
const isPromiseLike = (x) => typeof x?.then === "function";
|
|
326
367
|
const createThenableSignal = () => {
|
|
327
368
|
const ctrl = new AbortController();
|
|
@@ -344,6 +385,6 @@ const logError = (e) => {
|
|
|
344
385
|
});
|
|
345
386
|
};
|
|
346
387
|
//#endregion
|
|
347
|
-
export { $, createScope,
|
|
388
|
+
export { $, createScope, isAtom, isPrimitiveAtom };
|
|
348
389
|
|
|
349
390
|
//# sourceMappingURL=atom.mjs.map
|
package/dist/atom.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"atom.mjs","names":[],"sources":["../src/atom.ts"],"sourcesContent":["export type Atom<Value> = PrimitiveAtom<Value> | DerivedAtom<Value>;\nexport type CommonAtom<Value> = {\n readonly get: () => Value;\n readonly watch: (watcher: AtomWatcher) => () => void;\n readonly subscribe: (subscriber: AtomSubscribe<Value>) => () => void;\n readonly state: AtomState<Value>;\n};\nexport type PrimitiveAtom<Value> = CommonAtom<Value> & {\n readonly set: (value: AtomUpdater<Value>) => void;\n readonly state: AtomSuccessState<Value>;\n};\nexport type DerivedAtom<Value> = CommonAtom<Value>;\n\nexport type AtomWatcher = () => void;\nexport type AtomSubscribe<Value> = (value: Value, options: AtomSubscriberOptions) => void;\nexport type AtomInit<Value> = Value | AtomGetter<Value>;\nexport type AtomUpdater<Value> = Value | AtomReducer<Value>;\n// TODO: readonly\nexport type AtomInactiveState<Value> = {\n promise: typeof inactive;\n error: any;\n value?: Value;\n};\nexport type AtomPromiseState<Value> = {\n promise: PromiseLike<Value>;\n error: any;\n value?: Value;\n};\nexport type AtomSuccessState<Value> = {\n promise: undefined;\n error: undefined;\n value: Value;\n};\nexport type AtomErrorState<Value> = {\n promise: undefined;\n error: any;\n value?: Value;\n};\nexport type AtomState<Value> =\n | AtomInactiveState<Value>\n | AtomPromiseState<Value>\n | AtomSuccessState<Value>\n | AtomErrorState<Value>;\n\nexport type AtomSubscriberOptions = { readonly signal: ThenableSignal };\nexport type AtomGetter<Value> = (\n get: GetAtom,\n options: AtomGetOptions,\n) => Value | PromiseLike<Value>;\nexport type AtomReducer<Value> = (value: Value) => Value;\n\nexport type AtomGetOptions = { readonly signal: ThenableSignal };\nexport type ThenableSignal = AbortSignal & { then: (f: () => void) => void };\ntype ThenableSignalController = {\n abort: () => void;\n signal: ThenableSignal;\n};\n\nexport type GetAtom = {\n <Value>(anotherAtom: Atom<Value>, unwrap?: true): Value;\n <Value>(anotherAtom: Atom<Value>, unwrap: false): AtomState<Value>;\n};\n\ntype CreateAtom = {\n <Value>(init: AtomGetter<Value>, options?: AtomOptions<Value>): DerivedAtom<Value>;\n <Value>(init: Value, options?: AtomOptions<Value>): PrimitiveAtom<Value>;\n <Value>(init: Value | AtomGetter<Value>, options?: AtomOptions<Value>): Atom<Value>;\n};\nexport type AtomOptions<Value> = {\n equals?: AtomEquals<Value>;\n persist?: boolean;\n eager?: boolean;\n};\n\nexport type AtomEquals<Value> = (value: Value, prevValue: Value) => boolean;\nexport type AtomScope = {\n <Value>(baseAtom: PrimitiveAtom<Value>): PrimitiveAtom<Value>;\n <Value>(baseAtom: DerivedAtom<Value>): DerivedAtom<Value>;\n <Value>(baseAtom: Atom<Value>): Atom<Value>;\n <Value>(baseAtom: PrimitiveAtom<Value>, strict: true): PrimitiveAtom<Value> | undefined;\n <Value>(baseAtom: DerivedAtom<Value>, strict: true): DerivedAtom<Value> | undefined;\n <Value>(baseAtom: Atom<Value>, strict: true): Atom<Value> | undefined;\n};\n\nexport type SetLike<Key> = Key[] | Set<Key> | (Key extends object ? WeakSet<Key> : never);\nexport type MapLike<Key, Value> =\n | Map<Key, Value>\n | (Key extends object ? WeakMap<Key, Value> : never)\n | (Key extends string | number | symbol ? Record<Key, Value> : never);\n\ntype GetAtomInternal = {\n <Value>(anotherAtom: AtomInternal<Value>, unwrap?: true): Value;\n <Value>(anotherAtom: AtomInternal<Value>, unwrap: false): AtomState<Value>;\n};\ntype AtomGetterInternal<Value> = (\n get: GetAtomInternal,\n options: AtomGetOptions,\n) => Value | PromiseLike<Value>;\ntype AtomSubscribeInternal<Value> = {\n _subscriber: AtomSubscribe<Value>;\n _options: AtomSubscriberOptions;\n _ctrl?: ThenableSignalController;\n};\n\ntype AtomInternal<Value> = PrimitiveAtomInternal<Value> | DerivedAtomInternal<Value>;\n\nabstract class CommonAtomInternal<Value> {\n _nextValue: Value | undefined;\n _nextError: any | undefined;\n _children: Set<DerivedAtomInternal<any>> | undefined;\n _watchers: Set<AtomWatcher> | undefined;\n _subscribers: Set<AtomSubscribeInternal<Value>> | undefined;\n\n abstract readonly _source: boolean;\n abstract _active: boolean;\n abstract _needExecute: boolean;\n abstract _needPropagate: boolean;\n abstract _marked: boolean;\n\n abstract readonly _init: Value | AtomGetterInternal<Value>;\n abstract readonly _equals: AtomEquals<Value> | undefined;\n\n abstract readonly state: AtomState<Value>;\n\n get(): Value {\n if (!this._active) {\n execute(this as unknown as DerivedAtomInternal<Value>);\n disableAtom(this as unknown as AtomInternal<Value>);\n }\n if (this.state.error) throw this.state.error;\n if (this.state.promise) throw this.state.promise;\n return this.state.value!;\n }\n\n watch(watcher: AtomWatcher): () => void {\n if (!this._active) {\n requestActivate(this as unknown as DerivedAtomInternal<Value>);\n }\n (this._watchers ||= new Set()).add(watcher);\n return () => {\n this._watchers!.delete(watcher);\n if (!this._watchers!.size) {\n disableAtom(this as unknown as AtomInternal<Value>);\n }\n };\n }\n\n subscribe(subscriber: AtomSubscribe<Value>): () => void {\n const atomSubscriber: AtomSubscribeInternal<Value> = {\n _subscriber: subscriber,\n _options: {\n get signal() {\n return (atomSubscriber._ctrl ||= createThenableSignal()).signal;\n },\n },\n };\n if (!this._active) {\n requestActivate(this as unknown as DerivedAtomInternal<Value>);\n } else if (!this.state.error && !this.state.promise) {\n try {\n subscriber(this.state.value!, atomSubscriber._options);\n } catch (e) {\n logError(e);\n }\n }\n (this._subscribers ||= new Set()).add(atomSubscriber);\n return () => {\n this._subscribers!.delete(atomSubscriber);\n if (atomSubscriber._ctrl) {\n atomSubscriber._ctrl.abort();\n atomSubscriber._ctrl = undefined;\n }\n if (!this._subscribers!.size) {\n disableAtom(this as unknown as AtomInternal<Value>);\n }\n };\n }\n\n [Symbol.toPrimitive](): Value | undefined {\n return this.state.value;\n }\n}\n\nclass PrimitiveAtomInternal<Value> extends CommonAtomInternal<Value> {\n declare readonly _source: true;\n declare readonly _active: true;\n declare readonly _needExecute: false;\n _needPropagate: boolean = false;\n _marked: boolean = false;\n\n declare readonly _init: Value;\n declare readonly _equals: AtomEquals<Value> | undefined;\n\n declare state: AtomSuccessState<Value>;\n\n constructor(init: Value, options?: AtomOptions<Value>) {\n super();\n this._init = init;\n this._equals = options?.equals;\n this._nextValue = init;\n this.state = {\n promise: undefined,\n error: undefined,\n value: init,\n };\n }\n\n set(this: PrimitiveAtomInternal<Value>, value: AtomUpdater<Value>) {\n const nextValue = value instanceof Function ? value(this._nextValue!) : value;\n if (!equals(nextValue, this.state.value, this._equals)) {\n this._nextValue = nextValue;\n requestPropagate(this);\n }\n }\n}\n// @ts-expect-error\nPrimitiveAtomInternal.prototype._source = true;\n// @ts-expect-error\nPrimitiveAtomInternal.prototype._active = true;\n// @ts-expect-error\nPrimitiveAtomInternal.prototype._needExecute = false;\n\nclass DerivedAtomInternal<Value> extends CommonAtomInternal<Value> {\n declare readonly _source: false;\n\n _active = false;\n _needExecute = false;\n _needPropagate = false;\n _marked = false;\n\n _counter = 0;\n _ctrl: ThenableSignalController | undefined;\n _dependencies: Set<AtomInternal<any>> | undefined;\n _nextDependencies: Set<AtomInternal<any>> | undefined;\n\n declare readonly _init: AtomGetterInternal<Value>;\n declare readonly _equals: AtomEquals<Value> | undefined;\n declare readonly _persist: boolean;\n declare readonly _options: AtomGetOptions;\n\n declare state: AtomState<Value>;\n\n constructor(init: AtomGetter<Value>, options?: AtomOptions<Value>) {\n super();\n this._init = init as AtomGetterInternal<Value>;\n this._equals = options?.equals;\n this._persist = !!options?.persist;\n\n const self = this;\n this._options = {\n get signal() {\n return (self._ctrl ||= createThenableSignal()).signal;\n },\n };\n\n this.state = {\n promise: inactive,\n error: undefined,\n value: undefined,\n };\n }\n}\n// @ts-expect-error\nDerivedAtomInternal.prototype._source = false;\n\nexport const inactive = Promise.reject();\ninactive.catch(() => {});\n\nexport const $: CreateAtom = <Value>(\n init: Value | AtomGetter<Value>,\n options?: AtomOptions<Value>,\n) => {\n if (init instanceof Function) return new DerivedAtomInternal(init, options);\n return new PrimitiveAtomInternal(init, options) as any;\n};\n\nexport const isAtom = (x: unknown): x is Atom<unknown> => x instanceof CommonAtomInternal;\n\nexport const isPrimitiveAtom = (x: unknown): x is PrimitiveAtom<unknown> =>\n x instanceof PrimitiveAtomInternal;\n\nexport type AtomValuePair<Value> =\n | [Atom<Value>, Value | PrimitiveAtom<Value>]\n | [DerivedAtom<Value>, Value | Atom<Value>];\nexport const createScope = <T extends AtomValuePair<unknown>[]>(\n parentScope?: AtomScope | null,\n atomValuePairs?: T,\n): AtomScope => {\n const scopeMap = new WeakMap<Atom<any>, Atom<any>>();\n const atomMap = parentScope ? new WeakMap<Atom<any>, Atom<any>>() : scopeMap;\n const scope = (<T extends Atom<unknown>>(baseAtom: T, strict = false) => {\n let scopedAtom = scopeMap.get(baseAtom);\n if (!strict) scopedAtom ||= atomMap.get(baseAtom);\n // TODO: 현재 스코프마다 사용되는 모든 아톰을 저장해서 메모리 사용이 비효율적인데 해결할 수 있을까?\n // 의존성이 동적이라 많이 어렵다\n if (!scopedAtom) {\n const parentAtom = parentScope?.(baseAtom, true);\n if (strict) return parentAtom;\n const realBaseAtom = parentAtom || baseAtom;\n atomMap.set(\n baseAtom,\n (scopedAtom = (\n (realBaseAtom as AtomInternal<never>)._init instanceof Function\n ? $(\n (get, options) =>\n (realBaseAtom as AtomInternal<never>)._init(\n (atom, unwrap) => get(scope(atom), unwrap as any),\n options,\n ),\n {\n equals: (realBaseAtom as AtomInternal<never>)._equals,\n persist: (realBaseAtom as DerivedAtomInternal<never>)._persist,\n },\n )\n : // baseAtom을 전달하지 않고 새로 생성하는 이유는 SSR 등에서 사용자 간 상태 공유를 막기 위함\n parentAtom || $((realBaseAtom as AtomInternal<any>)._init)\n ) as T),\n );\n }\n return scopedAtom;\n }) as AtomScope;\n if (atomValuePairs) {\n for (const [atom, value] of atomValuePairs) {\n scopeMap.set(atom, isAtom(value) ? (parentScope || scope)(value) : $(value));\n }\n }\n return scope;\n};\n\nlet pendingUpdateAtoms = false;\nlet updateQueue: AtomInternal<any>[] = [];\nlet stack: AtomInternal<any>[] = [];\nconst requestActivate = <Value>(atom: DerivedAtomInternal<Value>) => {\n if (!atom._needExecute) {\n atom._needExecute = true;\n requestPropagate(atom);\n }\n};\nconst requestPropagate = <Value>(atom: AtomInternal<Value>) => {\n if (!atom._needPropagate) {\n atom._needPropagate = true;\n updateQueue.push(atom);\n if (!pendingUpdateAtoms) {\n pendingUpdateAtoms = true;\n queueMicrotask(updateAtoms);\n }\n }\n};\nconst updateAtoms = () => {\n pendingUpdateAtoms = false;\n {\n const updatedAtoms = updateQueue;\n updateQueue = [];\n for (const atom of updatedAtoms) {\n atom.state.promise = undefined;\n atom.state.error = atom._nextError;\n atom.state.value = atom._nextValue;\n mark(atom);\n }\n }\n const markedAtoms = stack;\n stack = [];\n for (let i = markedAtoms.length; i--; ) {\n const atom = markedAtoms[i]!;\n atom._marked = false;\n if (atom._needExecute) {\n atom._needPropagate = true;\n execute(atom);\n }\n if (atom._needPropagate) {\n propagate(atom);\n }\n }\n};\nconst propagate = <Value>(atom: AtomInternal<Value>) => {\n atom._needPropagate = false;\n if (atom._watchers) {\n for (const watcher of atom._watchers) {\n try {\n watcher();\n } catch (e) {\n logError(e);\n }\n }\n }\n if (!atom.state.error && !atom.state.promise) {\n if (atom._subscribers) {\n for (const subscriber of atom._subscribers) {\n if (subscriber._ctrl) {\n subscriber._ctrl.abort();\n subscriber._ctrl = undefined;\n }\n try {\n subscriber._subscriber(atom.state.value!, subscriber._options);\n } catch (e) {\n logError(e);\n }\n }\n }\n if (atom._children) {\n for (const child of atom._children) {\n child._needExecute = true;\n }\n }\n }\n};\nconst mark = (atom: AtomInternal<any>) => {\n if (!atom._marked) {\n atom._marked = true;\n if (atom._children) {\n for (const child of atom._children) {\n mark(child);\n }\n }\n stack.push(atom);\n }\n};\n\nclass Wrapped {\n e: unknown;\n constructor(e: unknown) {\n this.e = e;\n }\n}\nconst expired = Symbol();\nconst execute = <Value>(atom: DerivedAtomInternal<Value>) => {\n const counter = ++atom._counter;\n const prevActive = atom._active;\n atom._active = true;\n atom._needExecute = false;\n atom.state.promise = undefined;\n\n if (atom._ctrl) {\n atom._ctrl.abort();\n atom._ctrl = undefined;\n }\n\n try {\n const value = atom._init(<V>(anotherAtom: AtomInternal<V>, unwrap = true) => {\n if (counter !== atom._counter) throw expired;\n if ((atom as unknown) !== anotherAtom) {\n if (!anotherAtom._active) {\n execute(anotherAtom);\n if (anotherAtom._needPropagate) {\n propagate(anotherAtom);\n }\n }\n (atom._nextDependencies ||= new Set()).add(anotherAtom);\n (anotherAtom._children ||= new Set()).add(atom);\n }\n if (!unwrap) return anotherAtom.state;\n if (anotherAtom.state.error) throw new Wrapped(anotherAtom.state.error);\n if (anotherAtom.state.promise) throw new Wrapped(anotherAtom.state.promise);\n return anotherAtom.state.value as V;\n }, atom._options);\n\n if (isPromiseLike(value)) {\n atom.state.promise = value;\n value.then(\n (value) => {\n if (counter === atom._counter) {\n finalizeExecution(atom);\n if (prevActive && equals(value, atom.state.value, atom._equals)) {\n atom.state.promise = undefined;\n // 동일한 값인데 propagate해줘야 되는 거 마음에 안 든다\n // watchers만 호출할까?\n } else {\n atom._nextValue = value;\n atom._nextError = undefined;\n }\n requestPropagate(atom);\n }\n },\n (e) => {\n if (counter === atom._counter) {\n finalizeExecution(atom);\n if (e instanceof Wrapped) {\n e = e.e;\n } else {\n logError(e);\n }\n atom._nextError = e;\n requestPropagate(atom);\n }\n },\n );\n } else {\n finalizeExecution(atom);\n atom.state.error = undefined;\n if (prevActive && equals(value, atom.state.value, atom._equals)) {\n atom._needPropagate = false;\n } else {\n atom.state.value = atom._nextValue = value;\n }\n }\n } catch (e) {\n finalizeExecution(atom);\n if (e === expired) {\n atom._needPropagate = false;\n } else {\n if (e instanceof Wrapped) {\n e = e.e;\n } else {\n logError(e);\n }\n atom.state.error = e;\n }\n }\n};\n\nconst finalizeExecution = <Value>(atom: DerivedAtomInternal<Value>) => {\n ++atom._counter;\n\n const oldDependencies = atom._dependencies;\n atom._dependencies = atom._nextDependencies;\n if (oldDependencies) {\n for (const dep of oldDependencies) {\n if (!atom._dependencies?.has(dep)) {\n dep._children!.delete(atom);\n disableAtom(dep);\n }\n }\n oldDependencies.clear();\n }\n atom._nextDependencies = oldDependencies;\n};\n\nlet runningGc = false;\nlet gcCandidates: Set<DerivedAtomInternal<any>> = new Set();\nconst disableAtom = <Value>(atom: AtomInternal<Value>) => {\n if (\n !atom._source &&\n !atom._persist &&\n !atom._children?.size &&\n !atom._watchers?.size &&\n !atom._subscribers?.size\n ) {\n gcCandidates.add(atom);\n if (!runningGc) {\n runningGc = true;\n setTimeout(gc, 0);\n }\n }\n};\nconst gc = () => {\n for (const atom of gcCandidates) {\n if (\n !atom._source &&\n !atom._persist &&\n !atom._children?.size &&\n !atom._watchers?.size &&\n !atom._subscribers?.size\n ) {\n atom.state.promise = inactive;\n atom._nextValue = atom._nextError = atom.state.error = atom.state.value = undefined;\n atom._needPropagate = atom._needExecute = atom._active = false;\n if (atom._ctrl) {\n atom._ctrl.abort();\n atom._ctrl = undefined;\n }\n if (atom._dependencies) {\n for (const dep of atom._dependencies) {\n dep._children!.delete(atom);\n disableAtom(dep);\n }\n atom._dependencies.clear();\n\n if (atom._nextDependencies) {\n for (const dep of atom._nextDependencies) {\n dep._children!.delete(atom);\n disableAtom(dep);\n }\n atom._nextDependencies.clear();\n }\n }\n }\n }\n gcCandidates.clear();\n runningGc = false;\n};\n\nconst equals = <Value>(\n value: Value,\n prevValue?: Value,\n equalsFn?: (value: Value, prevValue: Value) => boolean,\n) =>\n Object.is(value, prevValue) ||\n (equalsFn !== undefined && prevValue !== undefined && equalsFn(value, prevValue));\n\nconst isPromiseLike = (x: unknown): x is PromiseLike<unknown> =>\n typeof (x as PromiseLike<unknown>)?.then === \"function\";\n\nconst createThenableSignal = () => {\n const ctrl = new AbortController();\n const signal = ctrl.signal as ThenableSignal;\n const promise = new Promise((resolve) => {\n signal.then = (f: () => void) => promise.then(f);\n signal.addEventListener(\"abort\", resolve, {\n once: true,\n passive: true,\n });\n });\n return {\n abort: () => ctrl.abort(),\n signal,\n };\n};\n\nconst logError = (e: unknown) => {\n // Chrome's console.error doesn't follow the stack trace of the given Error\n queueMicrotask(() => {\n throw e;\n });\n};\n"],"mappings":";AA0GA,IAAe,qBAAf,MAAyC;CACvC;CACA;CACA;CACA;CACA;CAaA,MAAa;AACX,MAAI,CAAC,KAAK,SAAS;AACjB,WAAQ,KAA8C;AACtD,eAAY,KAAuC;;AAErD,MAAI,KAAK,MAAM,MAAO,OAAM,KAAK,MAAM;AACvC,MAAI,KAAK,MAAM,QAAS,OAAM,KAAK,MAAM;AACzC,SAAO,KAAK,MAAM;;CAGpB,MAAM,SAAkC;AACtC,MAAI,CAAC,KAAK,QACR,iBAAgB,KAA8C;AAEhE,GAAC,KAAK,8BAAc,IAAI,KAAK,EAAE,IAAI,QAAQ;AAC3C,eAAa;AACX,QAAK,UAAW,OAAO,QAAQ;AAC/B,OAAI,CAAC,KAAK,UAAW,KACnB,aAAY,KAAuC;;;CAKzD,UAAU,YAA8C;EACtD,MAAM,iBAA+C;GACnD,aAAa;GACb,UAAU,EACR,IAAI,SAAS;AACX,YAAQ,eAAe,UAAU,sBAAsB,EAAE;MAE5D;GACF;AACD,MAAI,CAAC,KAAK,QACR,iBAAgB,KAA8C;WACrD,CAAC,KAAK,MAAM,SAAS,CAAC,KAAK,MAAM,QAC1C,KAAI;AACF,cAAW,KAAK,MAAM,OAAQ,eAAe,SAAS;WAC/C,GAAG;AACV,YAAS,EAAE;;AAGf,GAAC,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;AACd,QAAK,MAAM,OAAO;AAClB,QAAK,QAAQ,KAAA;;AAEf,MAAI,KAAK,eAAe;AACtB,QAAK,MAAM,OAAO,KAAK,eAAe;AACpC,QAAI,UAAW,OAAO,KAAK;AAC3B,gBAAY,IAAI;;AAElB,QAAK,cAAc,OAAO;AAE1B,OAAI,KAAK,mBAAmB;AAC1B,SAAK,MAAM,OAAO,KAAK,mBAAmB;AACxC,SAAI,UAAW,OAAO,KAAK;AAC3B,iBAAY,IAAI;;AAElB,SAAK,kBAAkB,OAAO;;;;AAKtC,cAAa,OAAO;AACpB,aAAY;;AAGd,MAAM,UACJ,OACA,WACA,aAEA,OAAO,GAAG,OAAO,UAAU,IAC1B,aAAa,KAAA,KAAa,cAAc,KAAA,KAAa,SAAS,OAAO,UAAU;AAElF,MAAM,iBAAiB,MACrB,OAAQ,GAA4B,SAAS;AAE/C,MAAM,6BAA6B;CACjC,MAAM,OAAO,IAAI,iBAAiB;CAClC,MAAM,SAAS,KAAK;CACpB,MAAM,UAAU,IAAI,SAAS,YAAY;AACvC,SAAO,QAAQ,MAAkB,QAAQ,KAAK,EAAE;AAChD,SAAO,iBAAiB,SAAS,SAAS;GACxC,MAAM;GACN,SAAS;GACV,CAAC;GACF;AACF,QAAO;EACL,aAAa,KAAK,OAAO;EACzB;EACD;;AAGH,MAAM,YAAY,MAAe;AAE/B,sBAAqB;AACnB,QAAM;GACN"}
|
|
1
|
+
{"version":3,"file":"atom.mjs","names":[],"sources":["../src/atom.ts"],"sourcesContent":["export type Atom<Value> = PrimitiveAtom<Value> | DerivedAtom<Value>;\nexport type CommonAtom<Value> = {\n readonly get: () => Value;\n readonly watch: (watcher: AtomWatcher) => () => void;\n readonly subscribe: (subscriber: AtomSubscribe<Value>) => () => void;\n readonly state: AtomState<Value>;\n};\nexport type PrimitiveAtom<Value> = CommonAtom<Value> & {\n readonly set: (value: AtomUpdater<Value>) => void;\n readonly state: AtomSuccessState<Value>;\n};\nexport type DerivedAtom<Value> = CommonAtom<Value>;\n\nexport type AtomWatcher = () => void;\nexport type AtomSubscribe<Value> = (value: Value, options: AtomSubscriberOptions) => void;\nexport type AtomInit<Value> = Value | AtomGetter<Value>;\nexport type AtomUpdater<Value> = Value | AtomReducer<Value>;\n// TODO: readonly\nexport type AtomInactiveState<Value> = {\n active: false;\n error: any;\n promise: undefined;\n value?: Value;\n};\nexport type AtomPromiseState<Value> = {\n active: true;\n error: any;\n promise: PromiseLike<Value>;\n value?: Value;\n};\nexport type AtomSuccessState<Value> = {\n active: true;\n error: undefined;\n promise: undefined;\n value: Value;\n};\nexport type AtomErrorState<Value> = {\n active: true;\n error: any;\n promise: undefined;\n value?: Value;\n};\nexport type AtomState<Value> =\n | AtomInactiveState<Value>\n | AtomPromiseState<Value>\n | AtomSuccessState<Value>\n | AtomErrorState<Value>;\n\nexport type AtomSubscriberOptions = { readonly signal: ThenableSignal };\nexport type AtomGetter<Value> = (\n get: GetAtom,\n options: AtomGetOptions,\n) => Value | PromiseLike<Value>;\nexport type AtomReducer<Value> = (value: Value) => Value;\n\nexport type AtomGetOptions = { readonly signal: ThenableSignal };\nexport type ThenableSignal = AbortSignal & { then: (f: () => void) => void };\ntype ThenableSignalController = {\n abort: () => void;\n signal: ThenableSignal;\n};\n\nexport type GetAtom = <Value>(anotherAtom: Atom<Value>) => Value;\n\ntype CreateAtom = {\n <Value>(init: AtomGetter<Value>, options?: AtomOptions<Value>): DerivedAtom<Value>;\n <Value>(init: Value, options?: AtomOptions<Value>): PrimitiveAtom<Value>;\n <Value>(init: Value | AtomGetter<Value>, options?: AtomOptions<Value>): Atom<Value>;\n};\nexport type AtomOptions<Value> = {\n equals?: AtomEquals<Value>;\n persist?: boolean;\n eager?: boolean;\n};\n\nexport type AtomEquals<Value> = (value: Value, prevValue: Value) => boolean;\nexport type AtomScope = {\n <Value>(baseAtom: PrimitiveAtom<Value>): PrimitiveAtom<Value>;\n <Value>(baseAtom: DerivedAtom<Value>): DerivedAtom<Value>;\n <Value>(baseAtom: Atom<Value>): Atom<Value>;\n <Value>(baseAtom: PrimitiveAtom<Value>, strict: true): PrimitiveAtom<Value> | undefined;\n <Value>(baseAtom: DerivedAtom<Value>, strict: true): DerivedAtom<Value> | undefined;\n <Value>(baseAtom: Atom<Value>, strict: true): Atom<Value> | undefined;\n};\n\nexport type SetLike<Key> = Key[] | Set<Key> | (Key extends object ? WeakSet<Key> : never);\nexport type MapLike<Key, Value> =\n | Map<Key, Value>\n | (Key extends object ? WeakMap<Key, Value> : never)\n | (Key extends string | number | symbol ? Record<Key, Value> : never);\n\ntype GetAtomInternal = <Value>(anotherAtom: AtomInternal<Value>) => Value;\ntype AtomGetterInternal<Value> = (\n get: GetAtomInternal,\n options: AtomGetOptions,\n) => Value | PromiseLike<Value>;\ntype AtomSubscribeInternal<Value> = {\n _subscriber: AtomSubscribe<Value>;\n _options: AtomSubscriberOptions;\n _ctrl?: ThenableSignalController;\n};\n\ntype AtomInternal<Value> = PrimitiveAtomInternal<Value> | DerivedAtomInternal<Value>;\n\nabstract class CommonAtomInternal<Value> {\n _nextValue: Value | undefined;\n _nextError: any | undefined;\n _children: Set<DerivedAtomInternal<any>> | undefined;\n _watchers: Set<AtomWatcher> | undefined;\n _subscribers: Set<AtomSubscribeInternal<Value>> | undefined;\n _valueChanged = true;\n\n abstract readonly _source: boolean;\n abstract _needExecute: boolean;\n abstract _needPropagate: boolean;\n abstract _marked: boolean;\n abstract _resolve: ((value: Value) => void) | undefined;\n abstract _reject: ((reason: any) => void) | undefined;\n\n abstract readonly _init: Value | AtomGetterInternal<Value>;\n abstract readonly _equals: AtomEquals<Value> | undefined;\n\n abstract readonly state: AtomState<Value>;\n\n get(): Value {\n if (!this.state.active) {\n execute(this as unknown as DerivedAtomInternal<Value>);\n disableAtom(this as unknown as AtomInternal<Value>);\n }\n if (this.state.promise) throw this.state.promise;\n if (this.state.error) throw this.state.error;\n return this.state.value!;\n }\n\n watch(watcher: AtomWatcher): () => void {\n if (!this.state.active) {\n requestActivate(this as unknown as DerivedAtomInternal<Value>);\n }\n (this._watchers ||= new Set()).add(watcher);\n return () => {\n this._watchers!.delete(watcher);\n if (!this._watchers!.size) {\n disableAtom(this as unknown as AtomInternal<Value>);\n }\n };\n }\n\n subscribe(subscriber: AtomSubscribe<Value>): () => void {\n const atomSubscriber: AtomSubscribeInternal<Value> = {\n _subscriber: subscriber,\n _options: {\n get signal() {\n return (atomSubscriber._ctrl ||= createThenableSignal()).signal;\n },\n },\n };\n if (!this.state.active) {\n requestActivate(this as unknown as DerivedAtomInternal<Value>);\n } else if (!this.state.error && !this.state.promise) {\n try {\n subscriber(this.state.value!, atomSubscriber._options);\n } catch (e) {\n logError(e);\n }\n }\n (this._subscribers ||= new Set()).add(atomSubscriber);\n return () => {\n this._subscribers!.delete(atomSubscriber);\n if (atomSubscriber._ctrl) {\n atomSubscriber._ctrl.abort();\n atomSubscriber._ctrl = undefined;\n }\n if (!this._subscribers!.size) {\n disableAtom(this as unknown as AtomInternal<Value>);\n }\n };\n }\n\n [Symbol.toPrimitive](): Value | undefined {\n return this.state.value;\n }\n}\n\nclass PrimitiveAtomInternal<Value> extends CommonAtomInternal<Value> {\n declare readonly _source: true;\n declare readonly _needExecute: false;\n _needPropagate: boolean = false;\n _marked: boolean = false;\n\n declare readonly _init: Value;\n declare readonly _equals: AtomEquals<Value> | undefined;\n\n declare state: AtomSuccessState<Value>;\n declare _hasValue: true;\n declare _nextValue: Value;\n declare _nextError: undefined;\n declare _resolve: undefined;\n declare _reject: undefined;\n\n constructor(init: Value, options?: AtomOptions<Value>) {\n super();\n this._nextValue = this._init = init;\n this._equals = options?.equals;\n this.state = {\n active: true,\n promise: undefined,\n error: undefined,\n value: init,\n };\n }\n\n set(this: PrimitiveAtomInternal<Value>, value: AtomUpdater<Value>) {\n const nextValue = value instanceof Function ? value(this._nextValue!) : value;\n if (!Object.is(nextValue, this._nextValue)) {\n this._nextValue = nextValue;\n requestPropagate(this);\n }\n }\n}\n// @ts-expect-error\nPrimitiveAtomInternal.prototype._source = true;\nPrimitiveAtomInternal.prototype._hasValue = true;\n// @ts-expect-error\nPrimitiveAtomInternal.prototype._needExecute = false;\n\nclass DerivedAtomInternal<Value> extends CommonAtomInternal<Value> {\n declare readonly _source: false;\n\n _hasValue = false;\n _needExecute = false;\n _needPropagate = false;\n _marked = false;\n\n _counter = 0;\n _resolve: ((value: Value) => void) | undefined;\n _reject: ((reason: any) => void) | undefined;\n _ctrl: ThenableSignalController | undefined;\n _dependencies: Set<AtomInternal<any>> | undefined;\n _allDependencies: Set<AtomInternal<any>> | undefined;\n\n declare readonly _init: AtomGetterInternal<Value>;\n declare readonly _equals: AtomEquals<Value> | undefined;\n declare readonly _persist: boolean;\n declare readonly _options: AtomGetOptions;\n\n declare state: AtomState<Value>;\n\n constructor(init: AtomGetter<Value>, options?: AtomOptions<Value>) {\n super();\n this._init = init as AtomGetterInternal<Value>;\n this._equals = options?.equals;\n this._persist = !!options?.persist;\n\n const self = this;\n this._options = {\n get signal() {\n return (self._ctrl ||= createThenableSignal()).signal;\n },\n };\n\n this.state = {\n active: false,\n promise: undefined,\n error: undefined,\n value: undefined,\n };\n }\n}\n// @ts-expect-error\nDerivedAtomInternal.prototype._source = false;\n\nexport const $: CreateAtom = <Value>(\n init: Value | AtomGetter<Value>,\n options?: AtomOptions<Value>,\n) => {\n if (init instanceof Function) return new DerivedAtomInternal(init, options);\n return new PrimitiveAtomInternal(init, options) as any;\n};\n\nexport const isAtom = (x: unknown): x is Atom<unknown> => x instanceof CommonAtomInternal;\n\nexport const isPrimitiveAtom = (x: unknown): x is PrimitiveAtom<unknown> =>\n x instanceof PrimitiveAtomInternal;\n\nexport type AtomValuePair<Value> =\n | [Atom<Value>, Value | PrimitiveAtom<Value>]\n | [DerivedAtom<Value>, Value | Atom<Value>];\nexport const createScope = <T extends AtomValuePair<unknown>[]>(\n parentScope?: AtomScope | null,\n atomValuePairs?: T,\n): AtomScope => {\n const scopeMap = new WeakMap<Atom<any>, Atom<any>>();\n const atomMap = parentScope ? new WeakMap<Atom<any>, Atom<any>>() : scopeMap;\n const scope = (<T extends Atom<unknown>>(baseAtom: T, strict = false) => {\n let scopedAtom = scopeMap.get(baseAtom);\n if (!strict) scopedAtom ||= atomMap.get(baseAtom);\n // TODO: 현재 스코프마다 사용되는 모든 아톰을 저장해서 메모리 사용이 비효율적인데 해결할 수 있을까?\n // 의존성이 동적이라 많이 어렵다\n if (!scopedAtom) {\n const parentAtom = parentScope?.(baseAtom, true);\n if (strict) return parentAtom;\n const realBaseAtom = parentAtom || baseAtom;\n atomMap.set(\n baseAtom,\n (scopedAtom = (\n (realBaseAtom as AtomInternal<never>)._init instanceof Function\n ? $(\n (get, options) =>\n (realBaseAtom as AtomInternal<never>)._init((atom) => get(scope(atom)), options),\n {\n equals: (realBaseAtom as AtomInternal<never>)._equals,\n persist: (realBaseAtom as DerivedAtomInternal<never>)._persist,\n },\n )\n : // baseAtom을 전달하지 않고 새로 생성하는 이유는 SSR 등에서 사용자 간 상태 공유를 막기 위함\n parentAtom || $((realBaseAtom as AtomInternal<any>)._init)\n ) as T),\n );\n }\n return scopedAtom;\n }) as AtomScope;\n if (atomValuePairs) {\n for (const [atom, value] of atomValuePairs) {\n scopeMap.set(atom, isAtom(value) ? (parentScope || scope)(value) : $(value));\n }\n }\n return scope;\n};\n\nlet pendingUpdateAtoms = false;\nlet updateQueue: AtomInternal<any>[] = [];\nlet stack: AtomInternal<any>[] = [];\nconst requestActivate = <Value>(atom: DerivedAtomInternal<Value>) => {\n if (!atom._needExecute) {\n atom._needExecute = true;\n requestPropagate(atom);\n }\n};\nconst requestPropagate = <Value>(atom: AtomInternal<Value>) => {\n if (!atom._needPropagate) {\n atom._needPropagate = true;\n updateQueue.push(atom);\n if (!pendingUpdateAtoms) {\n pendingUpdateAtoms = true;\n queueMicrotask(updateAtoms);\n }\n }\n};\nconst updateAtoms = () => {\n pendingUpdateAtoms = false;\n {\n const updatedAtoms = updateQueue;\n updateQueue = [];\n for (const atom of updatedAtoms) {\n if (atom.state.active) {\n const prevSuccess = atom._hasValue && !atom.state.promise && !atom.state.error;\n if ((atom.state.error = atom._nextError)) {\n atom._nextValue = atom.state.value;\n if (atom._reject) {\n atom._reject(atom._nextError);\n atom._resolve = atom._reject = atom.state.promise = undefined;\n }\n } else {\n if (\n !atom._hasValue ||\n (!Object.is(atom._nextValue, atom.state.value) &&\n !atom._equals?.(atom._nextValue, atom.state.value!))\n ) {\n atom.state.value = atom._nextValue;\n atom._valueChanged = atom._hasValue = true;\n } else {\n atom._nextValue = atom.state.value;\n if (prevSuccess) {\n atom._needPropagate = false;\n continue;\n }\n }\n if (atom._resolve) {\n atom._resolve(atom._nextValue!);\n atom._resolve = atom._reject = atom.state.promise = undefined;\n }\n }\n }\n mark(atom);\n }\n }\n const markedAtoms = stack;\n stack = [];\n for (let i = markedAtoms.length; i--; ) {\n const atom = markedAtoms[i]!;\n atom._marked = false;\n if (atom._needExecute) {\n atom._needPropagate = true;\n execute(atom);\n }\n if (atom._needPropagate) {\n propagate(atom);\n }\n }\n};\nconst propagate = <Value>(atom: AtomInternal<Value>) => {\n atom._needPropagate = false;\n if (atom._watchers) {\n for (const watcher of atom._watchers) {\n try {\n watcher();\n } catch (e) {\n logError(e);\n }\n }\n }\n if (atom.state.promise) {\n if (atom._children) {\n for (const child of atom._children) {\n child.state.promise ??= new Promise((resolve, reject) => {\n child._resolve = resolve;\n child._reject = reject;\n });\n child._needPropagate = true;\n }\n }\n } else if (atom.state.error) {\n if (atom._children) {\n for (const child of atom._children) {\n child.state.error = child._nextError = atom.state.error;\n if (child._reject) {\n child._reject(child._nextError);\n child._resolve = child._reject = child.state.promise = undefined;\n }\n child._needPropagate = true;\n }\n }\n } else {\n if (atom._valueChanged && atom._subscribers) {\n for (const subscriber of atom._subscribers) {\n if (subscriber._ctrl) {\n subscriber._ctrl.abort();\n subscriber._ctrl = undefined;\n }\n try {\n subscriber._subscriber(atom.state.value!, subscriber._options);\n } catch (e) {\n logError(e);\n }\n }\n }\n if (atom._children) {\n for (const child of atom._children) {\n child._needExecute = true;\n }\n }\n }\n atom._valueChanged = false;\n};\nconst mark = (atom: AtomInternal<any>) => {\n if (!atom._marked) {\n atom._marked = true;\n if (atom._children) {\n for (const child of atom._children) {\n mark(child);\n }\n }\n stack.push(atom);\n }\n};\n\nclass Wrapped {\n e: unknown;\n constructor(e: unknown) {\n this.e = e;\n }\n}\nconst expired = Symbol();\nconst loading = Symbol();\nconst execute = <Value>(atom: DerivedAtomInternal<Value>) => {\n const counter = ++atom._counter;\n const prevSuccess = atom._hasValue && !atom.state.promise && !atom.state.error;\n\n atom.state.active = true;\n atom._needExecute = false;\n\n if (atom._dependencies) {\n for (const dep of atom._dependencies) {\n dep._children!.delete(atom);\n // TODO?: if (dep.aggressiveGc) disableAtom(dep);\n }\n atom._dependencies.clear();\n }\n if (atom._ctrl) {\n atom._ctrl.abort();\n atom._ctrl = undefined;\n }\n\n try {\n const value = atom._init(<V>(anotherAtom: AtomInternal<V>) => {\n if (counter !== atom._counter) throw expired;\n\n if ((atom as unknown) !== anotherAtom) {\n if (!anotherAtom.state.active) {\n execute(anotherAtom as DerivedAtomInternal<V>);\n }\n if (!atom._allDependencies) {\n atom._allDependencies = new Set();\n atom._dependencies = new Set();\n }\n atom._dependencies!.add(anotherAtom);\n atom._allDependencies.add(anotherAtom);\n (anotherAtom._children ||= new Set()).add(atom);\n }\n\n const { state } = anotherAtom;\n if (state.promise) throw loading;\n if (state.error) throw new Wrapped(state.error);\n return state.value as V;\n }, atom._options);\n\n if (isPromiseLike(value)) {\n atom.state.promise ??= new Promise((resolve, reject) => {\n atom._resolve = resolve;\n atom._reject = reject;\n });\n value.then(\n (value) => {\n if (counter === atom._counter) {\n ++atom._counter;\n if (!atom._hasValue || !Object.is(value, atom._nextValue!)) atom._nextValue = value;\n atom._nextError = undefined;\n requestPropagate(atom);\n }\n },\n (e) => {\n if (counter === atom._counter && e !== expired) {\n ++atom._counter;\n if (e !== loading) {\n if (e instanceof Wrapped) {\n e = e.e;\n } else {\n logError(e);\n }\n atom._nextError = e;\n requestPropagate(atom);\n }\n }\n },\n );\n } else {\n ++atom._counter;\n if (\n !atom._hasValue ||\n (!Object.is(value, atom._nextValue) && !atom._equals?.(value, atom._nextValue!))\n ) {\n atom.state.value = atom._nextValue = value;\n atom._valueChanged = atom._hasValue = true;\n } else if (prevSuccess) {\n atom._needPropagate = false;\n }\n atom.state.error = atom._nextError = undefined;\n if (atom._resolve) {\n atom._resolve(atom._nextValue!);\n atom._resolve = atom._reject = atom.state.promise = undefined;\n }\n }\n } catch (e) {\n // assert(e !== expired);\n ++atom._counter;\n if (e === loading) {\n atom.state.promise ??= new Promise((resolve, reject) => {\n atom._resolve = resolve;\n atom._reject = reject;\n });\n } else {\n if (e instanceof Wrapped) {\n e = e.e;\n } else {\n logError(e);\n }\n atom.state.error = atom._nextError = e;\n if (atom._reject) {\n atom._reject(e);\n atom._resolve = atom._reject = atom.state.promise = undefined;\n }\n }\n }\n};\n\nlet runningGc = false;\nlet gcCandidates: Set<DerivedAtomInternal<any>> = new Set();\nconst disableAtom = <Value>(atom: AtomInternal<Value>) => {\n if (\n !atom._source &&\n !atom._persist &&\n !atom._children?.size &&\n !atom._watchers?.size &&\n !atom._subscribers?.size\n ) {\n gcCandidates.add(atom);\n if (!runningGc) {\n runningGc = true;\n setTimeout(gc, 0);\n }\n }\n};\nconst gc = () => {\n for (const atom of gcCandidates) {\n if (\n !atom._source &&\n !atom._persist &&\n !atom._children?.size &&\n !atom._watchers?.size &&\n !atom._subscribers?.size\n ) {\n atom._ctrl?.abort();\n // atom._reject?.(null);\n ++atom._counter;\n atom._nextValue =\n atom._nextError =\n atom.state.error =\n atom.state.value =\n atom.state.promise =\n atom._resolve =\n atom._reject =\n atom._ctrl =\n undefined;\n atom._needPropagate = atom._needExecute = atom._hasValue = atom.state.active = false;\n atom._valueChanged = atom._source;\n if (atom._allDependencies) {\n if (atom._dependencies) {\n for (const dep of atom._dependencies) {\n dep._children!.delete(atom);\n }\n atom._dependencies.clear();\n }\n for (const dep of atom._allDependencies) {\n disableAtom(dep);\n }\n atom._allDependencies.clear();\n }\n }\n }\n gcCandidates.clear();\n runningGc = false;\n};\n\nconst isPromiseLike = (x: unknown): x is PromiseLike<unknown> =>\n typeof (x as PromiseLike<unknown>)?.then === \"function\";\n\nconst createThenableSignal = () => {\n const ctrl = new AbortController();\n const signal = ctrl.signal as ThenableSignal;\n const promise = new Promise((resolve) => {\n signal.then = (f: () => void) => promise.then(f);\n signal.addEventListener(\"abort\", resolve, {\n once: true,\n passive: true,\n });\n });\n return {\n abort: () => ctrl.abort(),\n signal,\n };\n};\n\nconst logError = (e: unknown) => {\n // Chrome's console.error doesn't follow the stack trace of the given Error\n queueMicrotask(() => {\n throw e;\n });\n};\n"],"mappings":";AAwGA,IAAe,qBAAf,MAAyC;CACvC;CACA;CACA;CACA;CACA;CACA,gBAAgB;CAchB,MAAa;AACX,MAAI,CAAC,KAAK,MAAM,QAAQ;AACtB,WAAQ,KAA8C;AACtD,eAAY,KAAuC;;AAErD,MAAI,KAAK,MAAM,QAAS,OAAM,KAAK,MAAM;AACzC,MAAI,KAAK,MAAM,MAAO,OAAM,KAAK,MAAM;AACvC,SAAO,KAAK,MAAM;;CAGpB,MAAM,SAAkC;AACtC,MAAI,CAAC,KAAK,MAAM,OACd,iBAAgB,KAA8C;AAEhE,GAAC,KAAK,8BAAc,IAAI,KAAK,EAAE,IAAI,QAAQ;AAC3C,eAAa;AACX,QAAK,UAAW,OAAO,QAAQ;AAC/B,OAAI,CAAC,KAAK,UAAW,KACnB,aAAY,KAAuC;;;CAKzD,UAAU,YAA8C;EACtD,MAAM,iBAA+C;GACnD,aAAa;GACb,UAAU,EACR,IAAI,SAAS;AACX,YAAQ,eAAe,UAAU,sBAAsB,EAAE;MAE5D;GACF;AACD,MAAI,CAAC,KAAK,MAAM,OACd,iBAAgB,KAA8C;WACrD,CAAC,KAAK,MAAM,SAAS,CAAC,KAAK,MAAM,QAC1C,KAAI;A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B,IAAE,KAAK;AACP,OAAK,aACH,KAAK,aACL,KAAK,MAAM,QACX,KAAK,MAAM,QACX,KAAK,MAAM,UACX,KAAK,WACL,KAAK,UACL,KAAK,QACH,KAAA;AACJ,OAAK,iBAAiB,KAAK,eAAe,KAAK,YAAY,KAAK,MAAM,SAAS;AAC/E,OAAK,gBAAgB,KAAK;AAC1B,MAAI,KAAK,kBAAkB;AACzB,OAAI,KAAK,eAAe;AACtB,SAAK,MAAM,OAAO,KAAK,cACrB,KAAI,UAAW,OAAO,KAAK;AAE7B,SAAK,cAAc,OAAO;;AAE5B,QAAK,MAAM,OAAO,KAAK,iBACrB,aAAY,IAAI;AAElB,QAAK,iBAAiB,OAAO;;;AAInC,cAAa,OAAO;AACpB,aAAY;;AAGd,MAAM,iBAAiB,MACrB,OAAQ,GAA4B,SAAS;AAE/C,MAAM,6BAA6B;CACjC,MAAM,OAAO,IAAI,iBAAiB;CAClC,MAAM,SAAS,KAAK;CACpB,MAAM,UAAU,IAAI,SAAS,YAAY;AACvC,SAAO,QAAQ,MAAkB,QAAQ,KAAK,EAAE;AAChD,SAAO,iBAAiB,SAAS,SAAS;GACxC,MAAM;GACN,SAAS;GACV,CAAC;GACF;AACF,QAAO;EACL,aAAa,KAAK,OAAO;EACzB;EACD;;AAGH,MAAM,YAAY,MAAe;AAE/B,sBAAqB;AACnB,QAAM;GACN"}
|
package/dist/browser/index.d.ts
CHANGED
|
@@ -16,23 +16,27 @@ type AtomSubscribe<Value> = (value: Value, options: AtomSubscriberOptions) => vo
|
|
|
16
16
|
type AtomInit<Value> = Value | AtomGetter<Value>;
|
|
17
17
|
type AtomUpdater<Value> = Value | AtomReducer<Value>;
|
|
18
18
|
type AtomInactiveState<Value> = {
|
|
19
|
-
|
|
19
|
+
active: false;
|
|
20
20
|
error: any;
|
|
21
|
+
promise: undefined;
|
|
21
22
|
value?: Value;
|
|
22
23
|
};
|
|
23
24
|
type AtomPromiseState<Value> = {
|
|
24
|
-
|
|
25
|
+
active: true;
|
|
25
26
|
error: any;
|
|
27
|
+
promise: PromiseLike<Value>;
|
|
26
28
|
value?: Value;
|
|
27
29
|
};
|
|
28
30
|
type AtomSuccessState<Value> = {
|
|
29
|
-
|
|
31
|
+
active: true;
|
|
30
32
|
error: undefined;
|
|
33
|
+
promise: undefined;
|
|
31
34
|
value: Value;
|
|
32
35
|
};
|
|
33
36
|
type AtomErrorState<Value> = {
|
|
34
|
-
|
|
37
|
+
active: true;
|
|
35
38
|
error: any;
|
|
39
|
+
promise: undefined;
|
|
36
40
|
value?: Value;
|
|
37
41
|
};
|
|
38
42
|
type AtomState<Value> = AtomInactiveState<Value> | AtomPromiseState<Value> | AtomSuccessState<Value> | AtomErrorState<Value>;
|
|
@@ -47,10 +51,7 @@ type AtomGetOptions = {
|
|
|
47
51
|
type ThenableSignal = AbortSignal & {
|
|
48
52
|
then: (f: () => void) => void;
|
|
49
53
|
};
|
|
50
|
-
type GetAtom =
|
|
51
|
-
<Value>(anotherAtom: Atom<Value>, unwrap?: true): Value;
|
|
52
|
-
<Value>(anotherAtom: Atom<Value>, unwrap: false): AtomState<Value>;
|
|
53
|
-
};
|
|
54
|
+
type GetAtom = <Value>(anotherAtom: Atom<Value>) => Value;
|
|
54
55
|
type CreateAtom = {
|
|
55
56
|
<Value>(init: AtomGetter<Value>, options?: AtomOptions<Value>): DerivedAtom<Value>;
|
|
56
57
|
<Value>(init: Value, options?: AtomOptions<Value>): PrimitiveAtom<Value>;
|
|
@@ -72,7 +73,6 @@ type AtomScope = {
|
|
|
72
73
|
};
|
|
73
74
|
type SetLike<Key> = Key[] | Set<Key> | (Key extends object ? WeakSet<Key> : never);
|
|
74
75
|
type MapLike<Key, Value> = Map<Key, Value> | (Key extends object ? WeakMap<Key, Value> : never) | (Key extends string | number | symbol ? Record<Key, Value> : never);
|
|
75
|
-
declare const inactive: Promise<never>;
|
|
76
76
|
declare const $: CreateAtom;
|
|
77
77
|
declare const isAtom: (x: unknown) => x is Atom<unknown>;
|
|
78
78
|
declare const isPrimitiveAtom: (x: unknown) => x is PrimitiveAtom<unknown>;
|
|
@@ -92,5 +92,5 @@ declare const collectAtoms: <T>(tree: T, get?: <T_1>(atom: Atom<T_1>) => T_1) =>
|
|
|
92
92
|
declare const setAtoms: <T>(tree: T, values: RecursiveOptional<CollectedAtoms<T>>) => void;
|
|
93
93
|
declare const $$: CollectAtom;
|
|
94
94
|
//#endregion
|
|
95
|
-
export { $, $$, Atom, AtomEquals, AtomErrorState, AtomGetOptions, AtomGetter, AtomInactiveState, AtomInit, AtomOptions, AtomPromiseState, AtomReducer, AtomScope, AtomState, AtomSubscribe, AtomSubscriberOptions, AtomSuccessState, AtomUpdater, AtomValuePair, AtomWatcher, Atomized, CollectedAtoms, CommonAtom, DerivedAtom, GetAtom, MapLike, PrimitiveAtom, RecursiveOptional, SetLike, ThenableSignal, atomize, collectAtoms, createScope,
|
|
95
|
+
export { $, $$, Atom, AtomEquals, AtomErrorState, AtomGetOptions, AtomGetter, AtomInactiveState, AtomInit, AtomOptions, AtomPromiseState, AtomReducer, AtomScope, AtomState, AtomSubscribe, AtomSubscriberOptions, AtomSuccessState, AtomUpdater, AtomValuePair, AtomWatcher, Atomized, CollectedAtoms, CommonAtom, DerivedAtom, GetAtom, MapLike, PrimitiveAtom, RecursiveOptional, SetLike, ThenableSignal, atomize, collectAtoms, createScope, isAtom, isPrimitiveAtom, setAtoms };
|
|
96
96
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/atom.ts","../../src/utils.ts"],"mappings":";KAAY,IAAA,UAAc,aAAA,CAAc,KAAA,IAAS,WAAA,CAAY,KAAA;AAAA,KACjD,UAAA;EAAA,SACD,GAAA,QAAW,KAAA;EAAA,SACX,KAAA,GAAQ,OAAA,EAAS,WAAA;EAAA,SACjB,SAAA,GAAY,UAAA,EAAY,aAAA,CAAc,KAAA;EAAA,SACtC,KAAA,EAAO,SAAA,C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Q,aAAA,CAAc,CAAA;AAAA,KAEpF,cAAA,MACV,CAAA,SAAU,IAAA,YAAgB,CAAA,GAAI,CAAA,gCAAiC,CAAA,GAAI,cAAA,CAAe,CAAA,CAAE,CAAA,OAAQ,CAAA;AAAA,KAEzF,WAAA;EAAA,QACK,IAAA,EAAM,UAAA,CAAW,KAAA,IAAS,WAAA,CAAY,KAAA;EAAA,QACtC,IAAA,EAAM,KAAA,GAAQ,WAAA,CAAY,cAAA,CAAe,KAAA;AAAA;AAAA,KAGvC,iBAAA,MACR,CAAA,IACC,CAAA,gCAEiB,CAAA,GAAI,iBAAA,CAAkB,CAAA,CAAE,CAAA;AAAA,cAajC,OAAA,MAAc,IAAA,EAAM,CAAA,KAAI,QAAA,CAAS,CAAA;AAAA,cASjC,YAAA,MAAmB,IAAA,EAAM,CAAA,EAAG,GAAA,SAAA,IAAA,EADf,IAAA,CAAK,GAAA,MAAK,GAAA,KACqB,cAAA,CAAe,CAAA;AAAA,cAY3D,QAAA,MAAe,IAAA,EAAM,CAAA,EAAG,MAAA,EAAQ,iBAAA,CAAkB,cAAA,CAAe,CAAA;AAAA,cAajE,EAAA,EAAI,WAAA"}
|
package/dist/browser/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var e=class{_nextValue;_nextError;_children;_watchers;_subscribers;get(){if(this.
|
|
1
|
+
var e=class{_nextValue;_nextError;_children;_watchers;_subscribers;_valueChanged=!0;get(){if(this.state.active||(v(this),x(this)),this.state.promise)throw this.state.promise;if(this.state.error)throw this.state.error;return this.state.value}watch(e){return this.state.active||u(this),(this._watchers||=new Set).add(e),()=>{this._watchers.delete(e),this._watchers.size||x(this)}}subscribe(e){let t={_subscriber:e,_options:{get signal(){return(t._ctrl||=w()).signal}}};if(!this.state.active)u(this);else if(!this.state.error&&!this.state.promise)try{e(this.state.value,t._options)}catch(e){T(e)}return(this._subscribers||=new Set).add(t),()=>{this._subscribers.delete(t),t._ctrl&&=(t._ctrl.abort(),void 0),this._subscribers.size||x(this)}}[Symbol.toPrimitive](){return this.state.value}},t=class extends e{_needPropagate=!1;_marked=!1;constructor(e,t){super(),this._nextValue=this._init=e,this._equals=t?.equals,this.state={active:!0,promise:void 0,error:void 0,value:e}}set(e){let t=e instanceof Function?e(this._nextValue):e;Object.is(t,this._nextValue)||(this._nextValue=t,d(this))}};t.prototype._source=!0,t.prototype._hasValue=!0,t.prototype._needExecute=!1;var n=class extends e{_hasValue=!1;_needExecute=!1;_needPropagate=!1;_marked=!1;_counter=0;_resolve;_reject;_ctrl;_dependencies;_allDependencies;constructor(e,t){super(),this._init=e,this._equals=t?.equals,this._persist=!!t?.persist;let n=this;this._options={get signal(){return(n._ctrl||=w()).signal}},this.state={active:!1,promise:void 0,error:void 0,value:void 0}}};n.prototype._source=!1;const r=(e,r)=>e instanceof Function?new n(e,r):new t(e,r),i=t=>t instanceof e,a=e=>e instanceof t,o=(e,t)=>{let n=new WeakMap,a=e?new WeakMap:n,o=((t,i=!1)=>{let s=n.get(t);if(i||(s||=a.get(t)),!s){let n=e?.(t,!0);if(i)return n;let c=n||t;a.set(t,s=c._init instanceof Function?r((e,t)=>c._init(t=>e(o(t)),t),{equals:c._equals,persist:c._persist}):n||r(c._init))}return s});if(t)for(let[a,s]of t)n.set(a,i(s)?(e||o)(s):r(s));return o};let s=!1,c=[],l=[];const u=e=>{e._needExecute||(e._needExecute=!0,d(e))},d=e=>{e._needPropagate||(e._needPropagate=!0,c.push(e),s||(s=!0,queueMicrotask(f)))},f=()=>{s=!1;{let e=c;c=[];for(let t of e){if(t.state.active){let e=t._hasValue&&!t.state.promise&&!t.state.error;if(t.state.error=t._nextError)t._nextValue=t.state.value,t._reject&&(t._reject(t._nextError),t._resolve=t._reject=t.state.promise=void 0);else{if(!t._hasValue||!Object.is(t._nextValue,t.state.value)&&!t._equals?.(t._nextValue,t.state.value))t.state.value=t._nextValue,t._valueChanged=t._hasValue=!0;else if(t._nextValue=t.state.value,e){t._needPropagate=!1;continue}t._resolve&&=(t._resolve(t._nextValue),t._reject=t.state.promise=void 0)}}m(t)}}let e=l;l=[];for(let t=e.length;t--;){let n=e[t];n._marked=!1,n._needExecute&&(n._needPropagate=!0,v(n)),n._needPropagate&&p(n)}},p=e=>{if(e._needPropagate=!1,e._watchers)for(let t of e._watchers)try{t()}catch(e){T(e)}if(e.state.promise){if(e._children)for(let t of e._children)t.state.promise??=new Promise((e,n)=>{t._resolve=e,t._reject=n}),t._needPropagate=!0}else if(e.state.error){if(e._children)for(let t of e._children)t.state.error=t._nextError=e.state.error,t._reject&&(t._reject(t._nextError),t._resolve=t._reject=t.state.promise=void 0),t._needPropagate=!0}else{if(e._valueChanged&&e._subscribers)for(let t of e._subscribers){t._ctrl&&=(t._ctrl.abort(),void 0);try{t._subscriber(e.state.value,t._options)}catch(e){T(e)}}if(e._children)for(let t of e._children)t._needExecute=!0}e._valueChanged=!1},m=e=>{if(!e._marked){if(e._marked=!0,e._children)for(let t of e._children)m(t);l.push(e)}};var h=class{e;constructor(e){this.e=e}};const g=Symbol(),_=Symbol(),v=e=>{let t=++e._counter,n=e._hasValue&&!e.state.promise&&!e.state.error;if(e.state.active=!0,e._needExecute=!1,e._dependencies){for(let t of e._dependencies)t._children.delete(e);e._dependencies.clear()}e._ctrl&&=(e._ctrl.abort(),void 0);try{let r=e._init(n=>{if(t!==e._counter)throw g;e!==n&&(n.state.active||v(n),e._allDependencies||(e._allDependencies=new Set,e._dependencies=new Set),e._dependencies.add(n),e._allDependencies.add(n),(n._children||=new Set).add(e));let{state:r}=n;if(r.promise)throw _;if(r.error)throw new h(r.error);return r.value},e._options);C(r)?(e.state.promise??=new Promise((t,n)=>{e._resolve=t,e._reject=n}),r.then(n=>{t===e._counter&&(++e._counter,(!e._hasValue||!Object.is(n,e._nextValue))&&(e._nextValue=n),e._nextError=void 0,d(e))},n=>{t===e._counter&&n!==g&&(++e._counter,n!==_&&(n instanceof h?n=n.e:T(n),e._nextError=n,d(e)))})):(++e._counter,!e._hasValue||!Object.is(r,e._nextValue)&&!e._equals?.(r,e._nextValue)?(e.state.value=e._nextValue=r,e._valueChanged=e._hasValue=!0):n&&(e._needPropagate=!1),e.state.error=e._nextError=void 0,e._resolve&&=(e._resolve(e._nextValue),e._reject=e.state.promise=void 0))}catch(t){++e._counter,t===_?e.state.promise??=new Promise((t,n)=>{e._resolve=t,e._reject=n}):(t instanceof h?t=t.e:T(t),e.state.error=e._nextError=t,e._reject&&(e._reject(t),e._resolve=e._reject=e.state.promise=void 0))}};let y=!1,b=new Set;const x=e=>{!e._source&&!e._persist&&!e._children?.size&&!e._watchers?.size&&!e._subscribers?.size&&(b.add(e),y||(y=!0,setTimeout(S,0)))},S=()=>{for(let e of b)if(!e._source&&!e._persist&&!e._children?.size&&!e._watchers?.size&&!e._subscribers?.size&&(e._ctrl?.abort(),++e._counter,e._nextValue=e._nextError=e.state.error=e.state.value=e.state.promise=e._resolve=e._reject=e._ctrl=void 0,e._needPropagate=e._needExecute=e._hasValue=e.state.active=!1,e._valueChanged=e._source,e._allDependencies)){if(e._dependencies){for(let t of e._dependencies)t._children.delete(e);e._dependencies.clear()}for(let t of e._allDependencies)x(t);e._allDependencies.clear()}b.clear(),y=!1},C=e=>typeof e?.then==`function`,w=()=>{let e=new AbortController,t=e.signal,n=new Promise(e=>{t.then=e=>n.then(e),t.addEventListener(`abort`,e,{once:!0,passive:!0})});return{abort:()=>e.abort(),signal:t}},T=e=>{queueMicrotask(()=>{throw e})},E=()=>E,D=()=>void 0;Object.setPrototypeOf(E,new Proxy(E,{get:(e,t)=>t===Symbol.toPrimitive?D:E}));const O=e=>{if(typeof e!=`object`||!e)return r(e);if(Array.isArray(e))return e.map(O);let t=Object.create(null);for(let n in e)t[n]=O(e[n]);return t},k=e=>e.get(),A=(e,t=k)=>{let n=e=>{if(typeof e!=`object`||!e)return e;if(i(e))return t(e);if(Array.isArray(e))return e.map(n);let r=Object.create(null);for(let t in e)r[t]=n(e[t]);return r};return n(e)},j=(e,t)=>{let n=(e,t)=>{if(typeof e==`object`&&e)if(i(e))a(e)&&e.set(t);else for(let r in t)n(e[r],t[r])};n(e,t)},M=e=>e instanceof Function?r((t,n)=>{let r,i,a=e(e=>{try{t(e)}catch{}let n=e.state;if(n.error)i=n.error;else if(n.promise)(r||=[]).push(n.promise);else return n.value;return E},n);if(i)throw i;return r?Promise.all(r):a},{equals:N}):M(t=>A(e,t)),N=(e,t)=>{if(typeof e!=`object`||typeof t!=`object`||!e||!t)return!1;let n=e.constructor;if(n!==t.constructor)return!1;if(n===Array){let n=e.length;if(n!==t.length)return!1;for(;(n=n-1|0)>=0;)if(!Object.is(e[n],t[n]))return!1;return!0}let r=0;for(let n in e){if(!(n in t&&Object.is(e[n],t[n])))return!1;r=r+1|0}for(let e in t)if((r=r-1|0)<0)return!1;return!0};export{r as $,M as $$,O as atomize,A as collectAtoms,o as createScope,i as isAtom,a as isPrimitiveAtom,j as setAtoms};
|
|
2
2
|
//# sourceMappingURL=index.js.map
|