bansa 0.0.22 → 0.0.24
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/dist/atom.js +33 -32
- package/dist/index.browser.js +1 -1
- package/package.json +1 -1
- package/tests/bansa.test.ts +21 -0
package/dist/atom.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
class CommonAtomInternal {
|
|
2
|
-
|
|
2
|
+
f;
|
|
3
3
|
o;
|
|
4
4
|
b;
|
|
5
|
-
f;
|
|
6
5
|
g;
|
|
6
|
+
h;
|
|
7
7
|
get() {
|
|
8
|
-
if (!this.
|
|
8
|
+
if (!this.d) {
|
|
9
9
|
execute(this);
|
|
10
10
|
disableAtom(this);
|
|
11
11
|
}
|
|
@@ -14,13 +14,13 @@ class CommonAtomInternal {
|
|
|
14
14
|
return this.state.value;
|
|
15
15
|
}
|
|
16
16
|
watch(watcher) {
|
|
17
|
-
if (!this.
|
|
17
|
+
if (!this.d) {
|
|
18
18
|
requestActivate(this);
|
|
19
19
|
}
|
|
20
|
-
(this.
|
|
20
|
+
(this.g ||= /* @__PURE__ */ new Set()).add(watcher);
|
|
21
21
|
return () => {
|
|
22
|
-
this.
|
|
23
|
-
if (!this.
|
|
22
|
+
this.g.delete(watcher);
|
|
23
|
+
if (!this.g.size) {
|
|
24
24
|
disableAtom(this);
|
|
25
25
|
}
|
|
26
26
|
};
|
|
@@ -34,7 +34,7 @@ class CommonAtomInternal {
|
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
36
|
};
|
|
37
|
-
if (!this.
|
|
37
|
+
if (!this.d) {
|
|
38
38
|
requestActivate(this);
|
|
39
39
|
} else if (!this.state.error && !this.state.promise) {
|
|
40
40
|
try {
|
|
@@ -43,14 +43,14 @@ class CommonAtomInternal {
|
|
|
43
43
|
logError(e);
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
|
-
(this.
|
|
46
|
+
(this.h ||= /* @__PURE__ */ new Set()).add(atomSubscriber);
|
|
47
47
|
return () => {
|
|
48
|
-
this.
|
|
48
|
+
this.h.delete(atomSubscriber);
|
|
49
49
|
if (atomSubscriber.a) {
|
|
50
50
|
atomSubscriber.a.abort();
|
|
51
51
|
atomSubscriber.a = void 0;
|
|
52
52
|
}
|
|
53
|
-
if (!this.
|
|
53
|
+
if (!this.h.size) {
|
|
54
54
|
disableAtom(this);
|
|
55
55
|
}
|
|
56
56
|
};
|
|
@@ -66,7 +66,7 @@ class PrimitiveAtomInternal extends CommonAtomInternal {
|
|
|
66
66
|
super();
|
|
67
67
|
this.l = init;
|
|
68
68
|
this.m = options?.equals;
|
|
69
|
-
this.
|
|
69
|
+
this.f = init;
|
|
70
70
|
this.state = {
|
|
71
71
|
promise: void 0,
|
|
72
72
|
error: void 0,
|
|
@@ -74,18 +74,18 @@ class PrimitiveAtomInternal extends CommonAtomInternal {
|
|
|
74
74
|
};
|
|
75
75
|
}
|
|
76
76
|
set(value) {
|
|
77
|
-
const nextValue = value instanceof Function ? value(this.
|
|
77
|
+
const nextValue = value instanceof Function ? value(this.f) : value;
|
|
78
78
|
if (!equals(nextValue, this.state.value, this.m)) {
|
|
79
|
-
this.
|
|
79
|
+
this.f = nextValue;
|
|
80
80
|
requestPropagate(this);
|
|
81
81
|
}
|
|
82
82
|
}
|
|
83
83
|
}
|
|
84
84
|
PrimitiveAtomInternal.prototype.r = true;
|
|
85
|
-
PrimitiveAtomInternal.prototype.
|
|
85
|
+
PrimitiveAtomInternal.prototype.d = true;
|
|
86
86
|
PrimitiveAtomInternal.prototype.i = false;
|
|
87
87
|
class DerivedAtomInternal extends CommonAtomInternal {
|
|
88
|
-
|
|
88
|
+
d = false;
|
|
89
89
|
i = false;
|
|
90
90
|
c = false;
|
|
91
91
|
q = false;
|
|
@@ -124,7 +124,7 @@ const isAtom = (x) => x instanceof CommonAtomInternal;
|
|
|
124
124
|
const isPrimitiveAtom = (x) => x instanceof PrimitiveAtomInternal;
|
|
125
125
|
const createScope = (parentScope, atomValuePairs) => {
|
|
126
126
|
const scopeMap = /* @__PURE__ */ new WeakMap();
|
|
127
|
-
const atomMap = /* @__PURE__ */ new WeakMap();
|
|
127
|
+
const atomMap = parentScope ? /* @__PURE__ */ new WeakMap() : scopeMap;
|
|
128
128
|
const scope = ((baseAtom, strict = false) => {
|
|
129
129
|
let scopedAtom = scopeMap.get(baseAtom);
|
|
130
130
|
if (!strict) scopedAtom ||= atomMap.get(baseAtom);
|
|
@@ -182,7 +182,7 @@ const updateAtoms = () => {
|
|
|
182
182
|
for (const atom of updatedAtoms) {
|
|
183
183
|
atom.state.promise = void 0;
|
|
184
184
|
atom.state.error = atom.o;
|
|
185
|
-
atom.state.value = atom.
|
|
185
|
+
atom.state.value = atom.f;
|
|
186
186
|
mark(atom);
|
|
187
187
|
}
|
|
188
188
|
}
|
|
@@ -202,8 +202,8 @@ const updateAtoms = () => {
|
|
|
202
202
|
};
|
|
203
203
|
const propagate = (atom) => {
|
|
204
204
|
atom.c = false;
|
|
205
|
-
if (atom.
|
|
206
|
-
for (const watcher of atom.
|
|
205
|
+
if (atom.g) {
|
|
206
|
+
for (const watcher of atom.g) {
|
|
207
207
|
try {
|
|
208
208
|
watcher();
|
|
209
209
|
} catch (e) {
|
|
@@ -212,8 +212,8 @@ const propagate = (atom) => {
|
|
|
212
212
|
}
|
|
213
213
|
}
|
|
214
214
|
if (!atom.state.error && !atom.state.promise) {
|
|
215
|
-
if (atom.
|
|
216
|
-
for (const subscriber of atom.
|
|
215
|
+
if (atom.h) {
|
|
216
|
+
for (const subscriber of atom.h) {
|
|
217
217
|
if (subscriber.a) {
|
|
218
218
|
subscriber.a.abort();
|
|
219
219
|
subscriber.a = void 0;
|
|
@@ -252,7 +252,8 @@ class Wrapped {
|
|
|
252
252
|
const expired = Symbol();
|
|
253
253
|
const execute = (atom) => {
|
|
254
254
|
const counter = ++atom.n;
|
|
255
|
-
|
|
255
|
+
const prevActive = atom.d;
|
|
256
|
+
atom.d = true;
|
|
256
257
|
atom.i = false;
|
|
257
258
|
atom.state.promise = void 0;
|
|
258
259
|
if (atom.a) {
|
|
@@ -264,7 +265,7 @@ const execute = (atom) => {
|
|
|
264
265
|
(anotherAtom, unwrap = true) => {
|
|
265
266
|
if (counter !== atom.n) throw expired;
|
|
266
267
|
if (atom !== anotherAtom) {
|
|
267
|
-
if (!anotherAtom.
|
|
268
|
+
if (!anotherAtom.d) {
|
|
268
269
|
execute(anotherAtom);
|
|
269
270
|
if (anotherAtom.c) {
|
|
270
271
|
propagate(anotherAtom);
|
|
@@ -288,10 +289,10 @@ const execute = (atom) => {
|
|
|
288
289
|
(value2) => {
|
|
289
290
|
if (counter === atom.n) {
|
|
290
291
|
finalizeExecution(atom);
|
|
291
|
-
if (equals(value2, atom.state.value, atom.m)) {
|
|
292
|
+
if (prevActive && equals(value2, atom.state.value, atom.m)) {
|
|
292
293
|
atom.state.promise = void 0;
|
|
293
294
|
} else {
|
|
294
|
-
atom.
|
|
295
|
+
atom.f = value2;
|
|
295
296
|
atom.o = void 0;
|
|
296
297
|
}
|
|
297
298
|
requestPropagate(atom);
|
|
@@ -313,10 +314,10 @@ const execute = (atom) => {
|
|
|
313
314
|
} else {
|
|
314
315
|
finalizeExecution(atom);
|
|
315
316
|
atom.state.error = void 0;
|
|
316
|
-
if (equals(value, atom.state.value, atom.m)) {
|
|
317
|
+
if (prevActive && equals(value, atom.state.value, atom.m)) {
|
|
317
318
|
atom.c = false;
|
|
318
319
|
} else {
|
|
319
|
-
atom.state.value = atom.
|
|
320
|
+
atom.state.value = atom.f = value;
|
|
320
321
|
}
|
|
321
322
|
}
|
|
322
323
|
} catch (e) {
|
|
@@ -351,7 +352,7 @@ const finalizeExecution = (atom) => {
|
|
|
351
352
|
let runningGc = false;
|
|
352
353
|
let gcCandidates = /* @__PURE__ */ new Set();
|
|
353
354
|
const disableAtom = (atom) => {
|
|
354
|
-
if (!atom.r && !atom.s && !atom.b?.size && !atom.
|
|
355
|
+
if (!atom.r && !atom.s && !atom.b?.size && !atom.g?.size && !atom.h?.size) {
|
|
355
356
|
gcCandidates.add(atom);
|
|
356
357
|
if (!runningGc) {
|
|
357
358
|
runningGc = true;
|
|
@@ -361,10 +362,10 @@ const disableAtom = (atom) => {
|
|
|
361
362
|
};
|
|
362
363
|
const gc = () => {
|
|
363
364
|
for (const atom of gcCandidates) {
|
|
364
|
-
if (!atom.r && !atom.s && !atom.b?.size && !atom.
|
|
365
|
+
if (!atom.r && !atom.s && !atom.b?.size && !atom.g?.size && !atom.h?.size) {
|
|
365
366
|
atom.state.promise = inactive;
|
|
366
|
-
atom.
|
|
367
|
-
atom.c = atom.i = atom.
|
|
367
|
+
atom.f = atom.o = atom.state.error = atom.state.value = void 0;
|
|
368
|
+
atom.c = atom.i = atom.d = false;
|
|
368
369
|
if (atom.a) {
|
|
369
370
|
atom.a.abort();
|
|
370
371
|
atom.a = void 0;
|
package/dist/index.browser.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var p=class{
|
|
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/package.json
CHANGED
package/tests/bansa.test.ts
CHANGED
|
@@ -144,6 +144,27 @@ describe('Atom Library - Basic Tests', () => {
|
|
|
144
144
|
expect(mockFn).not.toHaveBeenCalled();
|
|
145
145
|
});
|
|
146
146
|
|
|
147
|
+
it('derived atom undefined value', async () => {
|
|
148
|
+
const atom = $(0);
|
|
149
|
+
const derivedAtom = $((get) => get(atom) === 0 ? undefined : true);
|
|
150
|
+
|
|
151
|
+
const mockFn = vi.fn();
|
|
152
|
+
const unsub = derivedAtom.subscribe(mockFn);
|
|
153
|
+
await flushMicrotasks();
|
|
154
|
+
expect(mockFn).toHaveBeenCalledWith(undefined, expect.anything());
|
|
155
|
+
mockFn.mockClear();
|
|
156
|
+
|
|
157
|
+
atom.set(1);
|
|
158
|
+
await flushMicrotasks();
|
|
159
|
+
expect(mockFn).toHaveBeenCalledWith(true, expect.anything());
|
|
160
|
+
mockFn.mockClear();
|
|
161
|
+
|
|
162
|
+
unsub();
|
|
163
|
+
atom.set(0);
|
|
164
|
+
await flushMicrotasks();
|
|
165
|
+
expect(mockFn).not.toHaveBeenCalled();
|
|
166
|
+
});
|
|
167
|
+
|
|
147
168
|
it('atom watch', async () => {
|
|
148
169
|
const atom = $(42);
|
|
149
170
|
const derivedAtom = $((get) => Promise.resolve(get(atom) * 2));
|