bansa 0.0.24 → 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 +224 -193
- package/dist/atom.d.mts +83 -0
- package/dist/atom.d.mts.map +1 -0
- package/dist/atom.mjs +390 -0
- package/dist/atom.mjs.map +1 -0
- package/dist/browser/index.d.ts +96 -0
- package/dist/browser/index.d.ts.map +1 -0
- package/dist/browser/index.js +2 -0
- package/dist/browser/index.js.map +1 -0
- package/dist/index.d.mts +3 -0
- package/dist/index.mjs +3 -0
- package/dist/react.d.mts +45 -0
- package/dist/react.d.mts.map +1 -0
- package/dist/react.mjs +59 -0
- package/dist/react.mjs.map +1 -0
- package/dist/utils.d.mts +17 -0
- package/dist/utils.d.mts.map +1 -0
- package/dist/utils.mjs +70 -0
- package/dist/utils.mjs.map +1 -0
- package/package.json +45 -36
- package/tsconfig.json +29 -18
- package/README.ko.md +0 -486
- package/dist/atom.d.ts +0 -80
- package/dist/atom.js +0 -420
- package/dist/index.browser.js +0 -1
- package/dist/index.d.ts +0 -2
- package/dist/index.js +0 -2
- package/dist/react.d.ts +0 -15
- package/dist/react.js +0 -40
- package/dist/utils.d.ts +0 -19
- package/dist/utils.js +0 -80
- package/tests/bansa.test.ts +0 -1340
package/dist/atom.js
DELETED
|
@@ -1,420 +0,0 @@
|
|
|
1
|
-
class CommonAtomInternal {
|
|
2
|
-
f;
|
|
3
|
-
o;
|
|
4
|
-
b;
|
|
5
|
-
g;
|
|
6
|
-
h;
|
|
7
|
-
get() {
|
|
8
|
-
if (!this.d) {
|
|
9
|
-
execute(this);
|
|
10
|
-
disableAtom(this);
|
|
11
|
-
}
|
|
12
|
-
if (this.state.error) throw this.state.error;
|
|
13
|
-
if (this.state.promise) throw this.state.promise;
|
|
14
|
-
return this.state.value;
|
|
15
|
-
}
|
|
16
|
-
watch(watcher) {
|
|
17
|
-
if (!this.d) {
|
|
18
|
-
requestActivate(this);
|
|
19
|
-
}
|
|
20
|
-
(this.g ||= /* @__PURE__ */ new Set()).add(watcher);
|
|
21
|
-
return () => {
|
|
22
|
-
this.g.delete(watcher);
|
|
23
|
-
if (!this.g.size) {
|
|
24
|
-
disableAtom(this);
|
|
25
|
-
}
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
subscribe(subscriber) {
|
|
29
|
-
const atomSubscriber = {
|
|
30
|
-
t: subscriber,
|
|
31
|
-
p: {
|
|
32
|
-
get signal() {
|
|
33
|
-
return (atomSubscriber.a ||= createThenableSignal()).signal;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
if (!this.d) {
|
|
38
|
-
requestActivate(this);
|
|
39
|
-
} else if (!this.state.error && !this.state.promise) {
|
|
40
|
-
try {
|
|
41
|
-
subscriber(this.state.value, atomSubscriber.p);
|
|
42
|
-
} catch (e) {
|
|
43
|
-
logError(e);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
(this.h ||= /* @__PURE__ */ new Set()).add(atomSubscriber);
|
|
47
|
-
return () => {
|
|
48
|
-
this.h.delete(atomSubscriber);
|
|
49
|
-
if (atomSubscriber.a) {
|
|
50
|
-
atomSubscriber.a.abort();
|
|
51
|
-
atomSubscriber.a = void 0;
|
|
52
|
-
}
|
|
53
|
-
if (!this.h.size) {
|
|
54
|
-
disableAtom(this);
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
[Symbol.toPrimitive]() {
|
|
59
|
-
return this.state.value;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
class PrimitiveAtomInternal extends CommonAtomInternal {
|
|
63
|
-
c = false;
|
|
64
|
-
q = false;
|
|
65
|
-
constructor(init, options) {
|
|
66
|
-
super();
|
|
67
|
-
this.l = init;
|
|
68
|
-
this.m = options?.equals;
|
|
69
|
-
this.f = init;
|
|
70
|
-
this.state = {
|
|
71
|
-
promise: void 0,
|
|
72
|
-
error: void 0,
|
|
73
|
-
value: init
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
set(value) {
|
|
77
|
-
const nextValue = value instanceof Function ? value(this.f) : value;
|
|
78
|
-
if (!equals(nextValue, this.state.value, this.m)) {
|
|
79
|
-
this.f = nextValue;
|
|
80
|
-
requestPropagate(this);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
PrimitiveAtomInternal.prototype.r = true;
|
|
85
|
-
PrimitiveAtomInternal.prototype.d = true;
|
|
86
|
-
PrimitiveAtomInternal.prototype.i = false;
|
|
87
|
-
class DerivedAtomInternal extends CommonAtomInternal {
|
|
88
|
-
d = false;
|
|
89
|
-
i = false;
|
|
90
|
-
c = false;
|
|
91
|
-
q = false;
|
|
92
|
-
n = 0;
|
|
93
|
-
a;
|
|
94
|
-
j;
|
|
95
|
-
k;
|
|
96
|
-
constructor(init, options) {
|
|
97
|
-
super();
|
|
98
|
-
this.l = init;
|
|
99
|
-
this.m = options?.equals;
|
|
100
|
-
this.s = !!options?.persist;
|
|
101
|
-
const self = this;
|
|
102
|
-
this.p = {
|
|
103
|
-
get signal() {
|
|
104
|
-
return (self.a ||= createThenableSignal()).signal;
|
|
105
|
-
}
|
|
106
|
-
};
|
|
107
|
-
this.state = {
|
|
108
|
-
promise: inactive,
|
|
109
|
-
error: void 0,
|
|
110
|
-
value: void 0
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
DerivedAtomInternal.prototype.r = false;
|
|
115
|
-
const inactive = Promise.reject();
|
|
116
|
-
inactive.catch(() => {
|
|
117
|
-
});
|
|
118
|
-
const $ = (init, options) => {
|
|
119
|
-
if (init instanceof Function)
|
|
120
|
-
return new DerivedAtomInternal(init, options);
|
|
121
|
-
return new PrimitiveAtomInternal(init, options);
|
|
122
|
-
};
|
|
123
|
-
const isAtom = (x) => x instanceof CommonAtomInternal;
|
|
124
|
-
const isPrimitiveAtom = (x) => x instanceof PrimitiveAtomInternal;
|
|
125
|
-
const createScope = (parentScope, atomValuePairs) => {
|
|
126
|
-
const scopeMap = /* @__PURE__ */ new WeakMap();
|
|
127
|
-
const atomMap = parentScope ? /* @__PURE__ */ new WeakMap() : scopeMap;
|
|
128
|
-
const scope = ((baseAtom, strict = false) => {
|
|
129
|
-
let scopedAtom = scopeMap.get(baseAtom);
|
|
130
|
-
if (!strict) scopedAtom ||= atomMap.get(baseAtom);
|
|
131
|
-
if (!scopedAtom) {
|
|
132
|
-
const parentAtom = parentScope?.(baseAtom, true);
|
|
133
|
-
if (strict) return parentAtom;
|
|
134
|
-
const realBaseAtom = parentAtom || baseAtom;
|
|
135
|
-
atomMap.set(
|
|
136
|
-
baseAtom,
|
|
137
|
-
scopedAtom = realBaseAtom.l instanceof Function ? $((get, options) => realBaseAtom.l(
|
|
138
|
-
(atom, unwrap) => get(scope(atom), unwrap),
|
|
139
|
-
options
|
|
140
|
-
), {
|
|
141
|
-
equals: realBaseAtom.m,
|
|
142
|
-
persist: realBaseAtom.s
|
|
143
|
-
}) : parentAtom || $(realBaseAtom.l)
|
|
144
|
-
);
|
|
145
|
-
}
|
|
146
|
-
return scopedAtom;
|
|
147
|
-
});
|
|
148
|
-
if (atomValuePairs) {
|
|
149
|
-
for (const [atom, value] of atomValuePairs) {
|
|
150
|
-
scopeMap.set(
|
|
151
|
-
atom,
|
|
152
|
-
isAtom(value) ? (parentScope || scope)(value) : $(value)
|
|
153
|
-
);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
return scope;
|
|
157
|
-
};
|
|
158
|
-
let pendingUpdateAtoms = false;
|
|
159
|
-
let updateQueue = [];
|
|
160
|
-
let stack = [];
|
|
161
|
-
const requestActivate = (atom) => {
|
|
162
|
-
if (!atom.i) {
|
|
163
|
-
atom.i = true;
|
|
164
|
-
requestPropagate(atom);
|
|
165
|
-
}
|
|
166
|
-
};
|
|
167
|
-
const requestPropagate = (atom) => {
|
|
168
|
-
if (!atom.c) {
|
|
169
|
-
atom.c = true;
|
|
170
|
-
updateQueue.push(atom);
|
|
171
|
-
if (!pendingUpdateAtoms) {
|
|
172
|
-
pendingUpdateAtoms = true;
|
|
173
|
-
queueMicrotask(updateAtoms);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
};
|
|
177
|
-
const updateAtoms = () => {
|
|
178
|
-
pendingUpdateAtoms = false;
|
|
179
|
-
{
|
|
180
|
-
const updatedAtoms = updateQueue;
|
|
181
|
-
updateQueue = [];
|
|
182
|
-
for (const atom of updatedAtoms) {
|
|
183
|
-
atom.state.promise = void 0;
|
|
184
|
-
atom.state.error = atom.o;
|
|
185
|
-
atom.state.value = atom.f;
|
|
186
|
-
mark(atom);
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
const markedAtoms = stack;
|
|
190
|
-
stack = [];
|
|
191
|
-
for (let i = markedAtoms.length; i--; ) {
|
|
192
|
-
const atom = markedAtoms[i];
|
|
193
|
-
atom.q = false;
|
|
194
|
-
if (atom.i) {
|
|
195
|
-
atom.c = true;
|
|
196
|
-
execute(atom);
|
|
197
|
-
}
|
|
198
|
-
if (atom.c) {
|
|
199
|
-
propagate(atom);
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
};
|
|
203
|
-
const propagate = (atom) => {
|
|
204
|
-
atom.c = false;
|
|
205
|
-
if (atom.g) {
|
|
206
|
-
for (const watcher of atom.g) {
|
|
207
|
-
try {
|
|
208
|
-
watcher();
|
|
209
|
-
} catch (e) {
|
|
210
|
-
logError(e);
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
if (!atom.state.error && !atom.state.promise) {
|
|
215
|
-
if (atom.h) {
|
|
216
|
-
for (const subscriber of atom.h) {
|
|
217
|
-
if (subscriber.a) {
|
|
218
|
-
subscriber.a.abort();
|
|
219
|
-
subscriber.a = void 0;
|
|
220
|
-
}
|
|
221
|
-
try {
|
|
222
|
-
subscriber.t(atom.state.value, subscriber.p);
|
|
223
|
-
} catch (e) {
|
|
224
|
-
logError(e);
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
if (atom.b) {
|
|
229
|
-
for (const child of atom.b) {
|
|
230
|
-
child.i = true;
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
};
|
|
235
|
-
const mark = (atom) => {
|
|
236
|
-
if (!atom.q) {
|
|
237
|
-
atom.q = true;
|
|
238
|
-
if (atom.b) {
|
|
239
|
-
for (const child of atom.b) {
|
|
240
|
-
mark(child);
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
stack.push(atom);
|
|
244
|
-
}
|
|
245
|
-
};
|
|
246
|
-
class Wrapped {
|
|
247
|
-
e;
|
|
248
|
-
constructor(e) {
|
|
249
|
-
this.e = e;
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
const expired = Symbol();
|
|
253
|
-
const execute = (atom) => {
|
|
254
|
-
const counter = ++atom.n;
|
|
255
|
-
const prevActive = atom.d;
|
|
256
|
-
atom.d = true;
|
|
257
|
-
atom.i = false;
|
|
258
|
-
atom.state.promise = void 0;
|
|
259
|
-
if (atom.a) {
|
|
260
|
-
atom.a.abort();
|
|
261
|
-
atom.a = void 0;
|
|
262
|
-
}
|
|
263
|
-
try {
|
|
264
|
-
const value = atom.l(
|
|
265
|
-
(anotherAtom, unwrap = true) => {
|
|
266
|
-
if (counter !== atom.n) throw expired;
|
|
267
|
-
if (atom !== anotherAtom) {
|
|
268
|
-
if (!anotherAtom.d) {
|
|
269
|
-
execute(anotherAtom);
|
|
270
|
-
if (anotherAtom.c) {
|
|
271
|
-
propagate(anotherAtom);
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
(atom.k ||= /* @__PURE__ */ new Set()).add(anotherAtom);
|
|
275
|
-
(anotherAtom.b ||= /* @__PURE__ */ new Set()).add(atom);
|
|
276
|
-
}
|
|
277
|
-
if (!unwrap) return anotherAtom.state;
|
|
278
|
-
if (anotherAtom.state.error)
|
|
279
|
-
throw new Wrapped(anotherAtom.state.error);
|
|
280
|
-
if (anotherAtom.state.promise)
|
|
281
|
-
throw new Wrapped(anotherAtom.state.promise);
|
|
282
|
-
return anotherAtom.state.value;
|
|
283
|
-
},
|
|
284
|
-
atom.p
|
|
285
|
-
);
|
|
286
|
-
if (isPromiseLike(value)) {
|
|
287
|
-
atom.state.promise = value;
|
|
288
|
-
value.then(
|
|
289
|
-
(value2) => {
|
|
290
|
-
if (counter === atom.n) {
|
|
291
|
-
finalizeExecution(atom);
|
|
292
|
-
if (prevActive && equals(value2, atom.state.value, atom.m)) {
|
|
293
|
-
atom.state.promise = void 0;
|
|
294
|
-
} else {
|
|
295
|
-
atom.f = value2;
|
|
296
|
-
atom.o = void 0;
|
|
297
|
-
}
|
|
298
|
-
requestPropagate(atom);
|
|
299
|
-
}
|
|
300
|
-
},
|
|
301
|
-
(e) => {
|
|
302
|
-
if (counter === atom.n) {
|
|
303
|
-
finalizeExecution(atom);
|
|
304
|
-
if (e instanceof Wrapped) {
|
|
305
|
-
e = e.e;
|
|
306
|
-
} else {
|
|
307
|
-
logError(e);
|
|
308
|
-
}
|
|
309
|
-
atom.o = e;
|
|
310
|
-
requestPropagate(atom);
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
);
|
|
314
|
-
} else {
|
|
315
|
-
finalizeExecution(atom);
|
|
316
|
-
atom.state.error = void 0;
|
|
317
|
-
if (prevActive && equals(value, atom.state.value, atom.m)) {
|
|
318
|
-
atom.c = false;
|
|
319
|
-
} else {
|
|
320
|
-
atom.state.value = atom.f = value;
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
} catch (e) {
|
|
324
|
-
finalizeExecution(atom);
|
|
325
|
-
if (e === expired) {
|
|
326
|
-
atom.c = false;
|
|
327
|
-
} else {
|
|
328
|
-
if (e instanceof Wrapped) {
|
|
329
|
-
e = e.e;
|
|
330
|
-
} else {
|
|
331
|
-
logError(e);
|
|
332
|
-
}
|
|
333
|
-
atom.state.error = e;
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
};
|
|
337
|
-
const finalizeExecution = (atom) => {
|
|
338
|
-
++atom.n;
|
|
339
|
-
const oldDependencies = atom.j;
|
|
340
|
-
atom.j = atom.k;
|
|
341
|
-
if (oldDependencies) {
|
|
342
|
-
for (const dep of oldDependencies) {
|
|
343
|
-
if (!atom.j?.has(dep)) {
|
|
344
|
-
dep.b.delete(atom);
|
|
345
|
-
disableAtom(dep);
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
oldDependencies.clear();
|
|
349
|
-
}
|
|
350
|
-
atom.k = oldDependencies;
|
|
351
|
-
};
|
|
352
|
-
let runningGc = false;
|
|
353
|
-
let gcCandidates = /* @__PURE__ */ new Set();
|
|
354
|
-
const disableAtom = (atom) => {
|
|
355
|
-
if (!atom.r && !atom.s && !atom.b?.size && !atom.g?.size && !atom.h?.size) {
|
|
356
|
-
gcCandidates.add(atom);
|
|
357
|
-
if (!runningGc) {
|
|
358
|
-
runningGc = true;
|
|
359
|
-
setTimeout(gc, 0);
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
};
|
|
363
|
-
const gc = () => {
|
|
364
|
-
for (const atom of gcCandidates) {
|
|
365
|
-
if (!atom.r && !atom.s && !atom.b?.size && !atom.g?.size && !atom.h?.size) {
|
|
366
|
-
atom.state.promise = inactive;
|
|
367
|
-
atom.f = atom.o = atom.state.error = atom.state.value = void 0;
|
|
368
|
-
atom.c = atom.i = atom.d = false;
|
|
369
|
-
if (atom.a) {
|
|
370
|
-
atom.a.abort();
|
|
371
|
-
atom.a = void 0;
|
|
372
|
-
}
|
|
373
|
-
if (atom.j) {
|
|
374
|
-
for (const dep of atom.j) {
|
|
375
|
-
dep.b.delete(atom);
|
|
376
|
-
disableAtom(dep);
|
|
377
|
-
}
|
|
378
|
-
atom.j.clear();
|
|
379
|
-
if (atom.k) {
|
|
380
|
-
for (const dep of atom.k) {
|
|
381
|
-
dep.b.delete(atom);
|
|
382
|
-
disableAtom(dep);
|
|
383
|
-
}
|
|
384
|
-
atom.k.clear();
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
gcCandidates.clear();
|
|
390
|
-
runningGc = false;
|
|
391
|
-
};
|
|
392
|
-
const equals = (value, prevValue, equalsFn) => Object.is(value, prevValue) || equalsFn !== void 0 && prevValue !== void 0 && equalsFn(value, prevValue);
|
|
393
|
-
const isPromiseLike = (x) => typeof x?.then === "function";
|
|
394
|
-
const createThenableSignal = () => {
|
|
395
|
-
const ctrl = new AbortController();
|
|
396
|
-
const signal = ctrl.signal;
|
|
397
|
-
const promise = new Promise((resolve) => {
|
|
398
|
-
signal.then = (f) => promise.then(f);
|
|
399
|
-
signal.addEventListener("abort", resolve, {
|
|
400
|
-
once: true,
|
|
401
|
-
passive: true
|
|
402
|
-
});
|
|
403
|
-
});
|
|
404
|
-
return {
|
|
405
|
-
abort: () => ctrl.abort(),
|
|
406
|
-
signal
|
|
407
|
-
};
|
|
408
|
-
};
|
|
409
|
-
const logError = (e) => {
|
|
410
|
-
queueMicrotask(() => {
|
|
411
|
-
throw e;
|
|
412
|
-
});
|
|
413
|
-
};
|
|
414
|
-
export {
|
|
415
|
-
$,
|
|
416
|
-
createScope,
|
|
417
|
-
inactive,
|
|
418
|
-
isAtom,
|
|
419
|
-
isPrimitiveAtom
|
|
420
|
-
};
|
package/dist/index.browser.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
var p=class{r;m;n;s;l;get(){if(this.a||(P(this),d(this)),this.state.error)throw this.state.error;if(this.state.promise)throw this.state.promise;return this.state.value}watch(t){return this.a||O(this),(this.s||=new Set).add(t),()=>{this.s.delete(t),this.s.size||d(this)}}subscribe(t){let n={v:t,A:{get signal(){return(n.t||=j()).signal}}};if(!this.a)O(this);else if(!this.state.error&&!this.state.promise)try{t(this.state.value,n.A)}catch(a){m(a)}return(this.l||=new Set).add(n),()=>{this.l.delete(n),n.t&&(n.t.abort(),n.t=void 0),this.l.size||d(this)}}[Symbol.toPrimitive](){return this.state.value}},i=class extends p{o=!1;V=!1;constructor(t,n){super(),this.d=t,this.f=n?.equals,this.r=t,this.state={promise:void 0,error:void 0,value:t}}set(t){let n=t instanceof Function?t(this.r):t;g(n,this.state.value,this.f)||(this.r=n,b(this))}};i.prototype.y=!0;i.prototype.a=!0;i.prototype.i=!1;var y=class extends p{a=!1;i=!1;o=!1;V=!1;p=0;t;u;c;constructor(t,n){super(),this.d=t,this.f=n?.equals,this.b=!!n?.persist;let a=this;this.A={get signal(){return(a.t||=j()).signal}},this.state={promise:I,error:void 0,value:void 0}}};y.prototype.y=!1;var I=Promise.reject();I.catch(()=>{});var c=(e,t)=>e instanceof Function?new y(e,t):new i(e,t),v=e=>e instanceof p,_=e=>e instanceof i,H=(e,t)=>{let n=new WeakMap,a=e?new WeakMap:n,o=((r,l=!1)=>{let s=n.get(r);if(l||(s||=a.get(r)),!s){let h=e?.(r,!0);if(l)return h;let f=h||r;a.set(r,s=f.d instanceof Function?c((K,z)=>f.d((E,L)=>K(o(E),L),z),{equals:f.f,persist:f.b}):h||c(f.d))}return s});if(t)for(let[r,l]of t)n.set(r,v(l)?(e||o)(l):c(l));return o},S=!1,x=[],w=[],O=e=>{e.i||(e.i=!0,b(e))},b=e=>{e.o||(e.o=!0,x.push(e),S||(S=!0,queueMicrotask(M)))},M=()=>{S=!1;{let t=x;x=[];for(let n of t)n.state.promise=void 0,n.state.error=n.m,n.state.value=n.r,q(n)}let e=w;w=[];for(let t=e.length;t--;){let n=e[t];n.V=!1,n.i&&(n.o=!0,P(n)),n.o&&D(n)}},D=e=>{if(e.o=!1,e.s)for(let t of e.s)try{t()}catch(n){m(n)}if(!e.state.error&&!e.state.promise){if(e.l)for(let t of e.l){t.t&&(t.t.abort(),t.t=void 0);try{t.v(e.state.value,t.A)}catch(n){m(n)}}if(e.n)for(let t of e.n)t.i=!0}},q=e=>{if(!e.V){if(e.V=!0,e.n)for(let t of e.n)q(t);w.push(e)}},u=class{e;constructor(t){this.e=t}},G=Symbol(),P=e=>{let t=++e.p,n=e.a;e.a=!0,e.i=!1,e.state.promise=void 0,e.t&&(e.t.abort(),e.t=void 0);try{let a=e.d((o,r=!0)=>{if(t!==e.p)throw G;if(e!==o&&(o.a||(P(o),o.o&&D(o)),(e.c||=new Set).add(o),(o.n||=new Set).add(e)),!r)return o.state;if(o.state.error)throw new u(o.state.error);if(o.state.promise)throw new u(o.state.promise);return o.state.value},e.A);W(a)?(e.state.promise=a,a.then(o=>{t===e.p&&(V(e),n&&g(o,e.state.value,e.f)?e.state.promise=void 0:(e.r=o,e.m=void 0),b(e))},o=>{t===e.p&&(V(e),o instanceof u?o=o.e:m(o),e.m=o,b(e))})):(V(e),e.state.error=void 0,n&&g(a,e.state.value,e.f)?e.o=!1:e.state.value=e.r=a)}catch(a){V(e),a===G?e.o=!1:(a instanceof u?a=a.e:m(a),e.state.error=a)}},V=e=>{++e.p;let t=e.u;if(e.u=e.c,t){for(let n of t)e.u?.has(n)||(n.n.delete(e),d(n));t.clear()}e.c=t},k=!1,T=new Set,d=e=>{!e.y&&!e.b&&!e.n?.size&&!e.s?.size&&!e.l?.size&&(T.add(e),k||(k=!0,setTimeout(U,0)))},U=()=>{for(let e of T)if(!e.y&&!e.b&&!e.n?.size&&!e.s?.size&&!e.l?.size&&(e.state.promise=I,e.r=e.m=e.state.error=e.state.value=void 0,e.o=e.i=e.a=!1,e.t&&(e.t.abort(),e.t=void 0),e.u)){for(let t of e.u)t.n.delete(e),d(t);if(e.u.clear(),e.c){for(let t of e.c)t.n.delete(e),d(t);e.c.clear()}}T.clear(),k=!1},g=(e,t,n)=>Object.is(e,t)||n!==void 0&&t!==void 0&&n(e,t),W=e=>typeof e?.then=="function",j=()=>{let e=new AbortController,t=e.signal,n=new Promise(a=>{t.then=o=>n.then(o),t.addEventListener("abort",a,{once:!0,passive:!0})});return{abort:()=>e.abort(),signal:t}},m=e=>{queueMicrotask(()=>{throw e})};var A=()=>A,R=()=>{};Object.setPrototypeOf(A,new Proxy(A,{get:(e,t)=>t===Symbol.toPrimitive?R:A}));var C=e=>{if(typeof e!="object"||e===null)return c(e);if(Array.isArray(e))return e.map(C);let t=Object.create(null);for(let n in e)t[n]=C(e[n]);return t},$=e=>e.get(),F=(e,t=$)=>{let n=a=>{if(typeof a!="object"||a===null)return a;if(v(a))return t(a);if(Array.isArray(a))return a.map(n);let o=Object.create(null);for(let r in a)o[r]=n(a[r]);return o};return n(e)},X=(e,t)=>{let n=(a,o)=>{if(typeof a=="object"&&a!==null)if(v(a))_(a)&&a.set(o);else for(let r in o)n(a[r],o[r])};n(e,t)},B=e=>e instanceof Function?c((t,n)=>{let a,o,r=e(l=>{let s=t(l,!1);if(s.error)o=s.error;else if(s.promise)(a||=[]).push(s.promise);else return s.value;return A},n);if(o)throw o;if(a)throw Promise.all(a);return r},{equals:Q}):B(t=>F(e,t)),Q=(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 o=e.length;if(o!==t.length)return!1;for(;(o=o-1|0)>=0;)if(!Object.is(e[o],t[o]))return!1;return!0}let a=0;for(let o in e){if(!(o in t&&Object.is(e[o],t[o])))return!1;a=a+1|0}for(let o in t)if((a=a-1|0)<0)return!1;return!0};export{c as $,B as $$,C as atomize,F as collectAtoms,H as createScope,I as inactive,v as isAtom,_ as isPrimitiveAtom,X as setAtoms};
|
package/dist/index.d.ts
DELETED
package/dist/index.js
DELETED
package/dist/react.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { Atom, AtomScope, AtomValuePair, DerivedAtom, PrimitiveAtom } from '.';
|
|
2
|
-
export declare const ScopeContext: import("react").Context<AtomScope>;
|
|
3
|
-
export declare const ScopeProvider: ({ value, children }: {
|
|
4
|
-
value?: AtomValuePair<unknown>[];
|
|
5
|
-
children: React.ReactNode;
|
|
6
|
-
}) => import("react/jsx-runtime").JSX.Element;
|
|
7
|
-
export declare const useAtomValue: <Value>(atom: Atom<Value>) => Value;
|
|
8
|
-
export declare const useAtomState: <Value>(atom: DerivedAtom<Value>) => import("./atom").AtomState<Value>;
|
|
9
|
-
export declare const useScopedAtom: UseScopedAtom;
|
|
10
|
-
export declare const useAtom: <Value>(atom: PrimitiveAtom<Value>) => readonly [Value, (newState: Value) => void];
|
|
11
|
-
export type UseScopedAtom = {
|
|
12
|
-
<Value>(baseAtom: PrimitiveAtom<Value>): PrimitiveAtom<Value>;
|
|
13
|
-
<Value>(baseAtom: DerivedAtom<Value>): DerivedAtom<Value>;
|
|
14
|
-
<Value>(baseAtom: Atom<Value>): Atom<Value>;
|
|
15
|
-
};
|
package/dist/react.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { jsx } from "react/jsx-runtime";
|
|
2
|
-
import { createContext, use, useContext, useMemo, useSyncExternalStore } from "react";
|
|
3
|
-
import { createScope } from ".";
|
|
4
|
-
const ScopeContext = createContext((x) => x);
|
|
5
|
-
const ScopeProvider = ({ value, children }) => {
|
|
6
|
-
const parentScope = value && useContext(ScopeContext);
|
|
7
|
-
const scope = useMemo(() => createScope(parentScope, value), [parentScope]);
|
|
8
|
-
return /* @__PURE__ */ jsx(ScopeContext.Provider, { value: scope, children });
|
|
9
|
-
};
|
|
10
|
-
const useAtomValue = (atom) => (atom = useContext(ScopeContext)(atom), useSyncExternalStore(
|
|
11
|
-
(watcher) => atom.watch(watcher),
|
|
12
|
-
() => {
|
|
13
|
-
try {
|
|
14
|
-
return atom.get();
|
|
15
|
-
} catch (_) {
|
|
16
|
-
if (atom.state.promise) use(Promise.resolve(atom.state.promise));
|
|
17
|
-
throw atom.state.error;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
));
|
|
21
|
-
const useAtomState = (atom) => (atom = useContext(ScopeContext)(atom), useSyncExternalStore(
|
|
22
|
-
(watcher) => atom.watch(watcher),
|
|
23
|
-
() => {
|
|
24
|
-
try {
|
|
25
|
-
atom.get();
|
|
26
|
-
} catch (_) {
|
|
27
|
-
}
|
|
28
|
-
return atom.state;
|
|
29
|
-
}
|
|
30
|
-
));
|
|
31
|
-
const useScopedAtom = ((atom) => useContext(ScopeContext)(atom));
|
|
32
|
-
const useAtom = (atom) => (atom = useScopedAtom(atom), [useAtomValue(atom), (newState) => atom.set(newState)]);
|
|
33
|
-
export {
|
|
34
|
-
ScopeContext,
|
|
35
|
-
ScopeProvider,
|
|
36
|
-
useAtom,
|
|
37
|
-
useAtomState,
|
|
38
|
-
useAtomValue,
|
|
39
|
-
useScopedAtom
|
|
40
|
-
};
|
package/dist/utils.d.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { type PrimitiveAtom, type Atom, type AtomGetter, type DerivedAtom } from "./atom";
|
|
2
|
-
export type Atomized<T> = T extends object ? {
|
|
3
|
-
[K in keyof T]: Atomized<T[K]>;
|
|
4
|
-
} : PrimitiveAtom<T>;
|
|
5
|
-
export type CollectedAtoms<T> = T extends Atom<infer U> ? U : T extends object ? {
|
|
6
|
-
[K in keyof T]: CollectedAtoms<T[K]>;
|
|
7
|
-
} : T;
|
|
8
|
-
type CollectAtom = {
|
|
9
|
-
<Value>(init: AtomGetter<Value>): DerivedAtom<Value>;
|
|
10
|
-
<Value>(init: Value): DerivedAtom<CollectedAtoms<Value>>;
|
|
11
|
-
};
|
|
12
|
-
export type RecursiveOptional<T> = T | (T extends object ? {
|
|
13
|
-
[P in keyof T]: RecursiveOptional<T[P]>;
|
|
14
|
-
} : never);
|
|
15
|
-
export declare const atomize: <T>(tree: T) => Atomized<T>;
|
|
16
|
-
export declare const collectAtoms: <T>(tree: T, get?: <T_1>(atom: Atom<T_1>) => T_1) => CollectedAtoms<T>;
|
|
17
|
-
export declare const setAtoms: <T>(tree: T, values: RecursiveOptional<CollectedAtoms<T>>) => void;
|
|
18
|
-
export declare const $$: CollectAtom;
|
|
19
|
-
export {};
|
package/dist/utils.js
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import { $, isAtom, isPrimitiveAtom } from "./atom";
|
|
2
|
-
const ouroboros = () => ouroboros;
|
|
3
|
-
const toUndefined = () => void 0;
|
|
4
|
-
Object.setPrototypeOf(
|
|
5
|
-
ouroboros,
|
|
6
|
-
new Proxy(ouroboros, {
|
|
7
|
-
get: (_, k) => k === Symbol.toPrimitive ? toUndefined : ouroboros
|
|
8
|
-
})
|
|
9
|
-
);
|
|
10
|
-
const atomize = (tree) => {
|
|
11
|
-
if (typeof tree !== "object" || tree === null) return $(tree);
|
|
12
|
-
if (Array.isArray(tree)) return tree.map(atomize);
|
|
13
|
-
const result = /* @__PURE__ */ Object.create(null);
|
|
14
|
-
for (const k in tree) result[k] = atomize(tree[k]);
|
|
15
|
-
return result;
|
|
16
|
-
};
|
|
17
|
-
const getAtom = (atom) => atom.get();
|
|
18
|
-
const collectAtoms = (tree, get = getAtom) => {
|
|
19
|
-
const recurse = (t) => {
|
|
20
|
-
if (typeof t !== "object" || t === null) return t;
|
|
21
|
-
if (isAtom(t)) return get(t);
|
|
22
|
-
if (Array.isArray(t)) return t.map(recurse);
|
|
23
|
-
const result = /* @__PURE__ */ Object.create(null);
|
|
24
|
-
for (const k in t) result[k] = recurse(t[k]);
|
|
25
|
-
return result;
|
|
26
|
-
};
|
|
27
|
-
return recurse(tree);
|
|
28
|
-
};
|
|
29
|
-
const setAtoms = (tree, values) => {
|
|
30
|
-
const recurse = (t, v) => {
|
|
31
|
-
if (typeof t === "object" && t !== null) {
|
|
32
|
-
if (isAtom(t)) {
|
|
33
|
-
if (isPrimitiveAtom(t)) t.set(v);
|
|
34
|
-
} else {
|
|
35
|
-
for (const k in v) recurse(t[k], v[k]);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
};
|
|
39
|
-
recurse(tree, values);
|
|
40
|
-
};
|
|
41
|
-
const $$ = (init) => init instanceof Function ? $((get, options) => {
|
|
42
|
-
let promises;
|
|
43
|
-
let error;
|
|
44
|
-
const result = init((atom) => {
|
|
45
|
-
const state = get(atom, false);
|
|
46
|
-
if (state.error) error = state.error;
|
|
47
|
-
else if (state.promise) (promises ||= []).push(state.promise);
|
|
48
|
-
else return state.value;
|
|
49
|
-
return ouroboros;
|
|
50
|
-
}, options);
|
|
51
|
-
if (error) throw error;
|
|
52
|
-
if (promises) throw Promise.all(promises);
|
|
53
|
-
return result;
|
|
54
|
-
}, {
|
|
55
|
-
equals: shallowEquals
|
|
56
|
-
}) : $$((get) => collectAtoms(init, get));
|
|
57
|
-
const shallowEquals = (a, b) => {
|
|
58
|
-
if (typeof a !== "object" || typeof b !== "object" || !a || !b) return false;
|
|
59
|
-
const c = a.constructor;
|
|
60
|
-
if (c !== b.constructor) return false;
|
|
61
|
-
if (c === Array) {
|
|
62
|
-
let i = a.length;
|
|
63
|
-
if (i !== b.length) return false;
|
|
64
|
-
while ((i = i - 1 | 0) >= 0) if (!Object.is(a[i], b[i])) return false;
|
|
65
|
-
return true;
|
|
66
|
-
}
|
|
67
|
-
let n = 0;
|
|
68
|
-
for (const k in a) {
|
|
69
|
-
if (!(k in b && Object.is(a[k], b[k]))) return false;
|
|
70
|
-
n = n + 1 | 0;
|
|
71
|
-
}
|
|
72
|
-
for (const _ in b) if ((n = n - 1 | 0) < 0) return false;
|
|
73
|
-
return true;
|
|
74
|
-
};
|
|
75
|
-
export {
|
|
76
|
-
$$,
|
|
77
|
-
atomize,
|
|
78
|
-
collectAtoms,
|
|
79
|
-
setAtoms
|
|
80
|
-
};
|