bansa 0.0.14 → 0.0.15
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/index.browser.js +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.js +5 -3
- package/package.json +1 -1
- package/tests/bansa.test.ts +58 -2
package/dist/index.browser.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var f=class{r;p;a;o;l;get(){if(this.s||(k(this),i(this)),this.state.error)throw this.state.error;if(this.state.promise)throw this.state.promise;return this.state.value}watch(t){return this.s||_(this),(this.o||=new Set).add(t),()=>{this.o.delete(t),this.o.size||i(this)}}subscribe(t){let a={h:t,A:{get signal(){return(a.t||=T()).signal}}};if(!this.s)_(this);else if(!this.state.error&&!this.state.promise)try{t(this.state.value,a.A)}catch(n){p(n)}return(this.l||=new Set).add(a),()=>{this.l.delete(a),a.t&&(a.t.abort(),a.t=void 0),this.l.size||i(this)}}[Symbol.toPrimitive](){return this.state.value}},c=class extends f{n=!1;m=!1;constructor(t,a){super(),this.d=t,this.V=a?.equals,this.r=t,this.state={promise:void 0,error:void 0,value:t}}set(t){let a=t instanceof Function?t(this.r):t;g(a,this.state.value,this.V)||(this.r=a,b(this))}};c.prototype.y=!0;c.prototype.s=!0;c.prototype.u=!1;var y=class extends f{s=!1;u=!1;n=!1;m=!1;f=0;t;i;c;constructor(t,a){super(),this.d=t,this.V=a?.equals,this.b=!!a?.persist;let n=this;this.A={get signal(){return(n.t||=T()).signal}},this.state={promise:I,error:void 0,value:void 0}}};y.prototype.y=!1;var V=()=>V,O=()=>{};Object.setPrototypeOf(V,new Proxy(V,{get:(e,t)=>t===Symbol.toPrimitive?O:V}));var I=Promise.reject();I.catch(O);var m=(e,t)=>e instanceof Function?new y(e,t):new c(e,t),U=e=>m((t,a)=>{let n,r,l=e(s=>{let o=t(s,!1);if(o.error)r=o.error;else if(o.promise)(n||=[]).push(o.promise);else return o.value;return V},a);if(r)throw r;if(n)throw Promise.all(n);return l},{equals:z}),R=(e,t)=>{let a=new WeakMap,n=new WeakMap;if(t)for(let[l,s]of t)a.set(l,s instanceof f?s:m(s));let r=((l,s=!1)=>{let o=a.get(l);if(s)return o||e?.(l,!0);if(!o){let P=e?.(l,!0),d=P||l;n.set(l,o=d.d instanceof Function?m((E,K)=>d.d((L,j)=>E(r(L),j),K),{equals:d.V,persist:d.b}):P||m(d.d))}return o});return r},z=(e,t)=>{if(typeof e!="object"||typeof t!="object"||!e||!t)return!1;let a=e.constructor;if(a!==t.constructor)return!1;if(a===Array){let r=e.length;if(r!==t.length)return!1;for(;(r=r-1|0)>=0;)if(!Object.is(e[r],t[r]))return!1;return!0}let n=0;for(let r in e){if(!(r in t&&Object.is(e[r],t[r])))return!1;n=n+1|0}for(let r in t)if((n=n-1|0)<0)return!1;return!0},h=!1,v=[],S=[],_=e=>{e.u||(e.u=!0,b(e))},b=e=>{e.n||(e.n=!0,v.push(e),h||(h=!0,queueMicrotask(C)))},C=()=>{h=!1;{let t=v;v=[];for(let a of t)a.state.promise=void 0,a.state.error=a.p,a.state.value=a.r,D(a)}let e=S;S=[];for(let t=e.length;t--;){let a=e[t];a.m=!1,a.u&&(a.n=!0,k(a)),a.n&&q(a)}},q=e=>{if(e.n=!1,e.o)for(let t of e.o)try{t()}catch(a){p(a)}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.h(e.state.value,t.A)}catch(a){p(a)}}if(e.a)for(let t of e.a)t.u=!0}},D=e=>{if(!e.m){if(e.m=!0,e.a)for(let t of e.a)D(t);S.push(e)}},u=class{e;constructor(t){this.e=t}},G=Symbol(),k=e=>{let t=++e.f;e.s=!0,e.u=!1,e.state.promise=void 0,e.t&&(e.t.abort(),e.t=void 0);try{let a=e.d((n,r=!0)=>{if(t!==e.f)throw G;if(e!==n&&(n.s||(k(n),n.n&&q(n)),(e.c||=new Set).add(n),(n.a||=new Set).add(e)),!r)return n.state;if(n.state.error)throw new u(n.state.error);if(n.state.promise)throw new u(n.state.promise);return n.state.value},e.A);W(a)?(e.state.promise=a,a.then(n=>{t===e.f&&(A(e),g(n,e.state.value,e.V)?e.state.promise=void 0:(e.r=n,e.p=void 0),b(e))},n=>{t===e.f&&(A(e),n instanceof u?n=n.e:p(n),e.p=n,b(e))})):(A(e),e.state.error=void 0,g(a,e.state.value,e.V)?e.n=!1:e.state.value=e.r=a)}catch(a){A(e),a===G?e.n=!1:(a instanceof u?a=a.e:p(a),e.state.error=a)}},A=e=>{++e.f;let t=e.i;if(e.i=e.c,t){for(let a of t)e.i?.has(a)||(a.a.delete(e),i(a));t.clear()}e.c=t},w=!1,x=new Set,i=e=>{!e.y&&!e.b&&!e.a?.size&&!e.o?.size&&!e.l?.size&&(x.add(e),w||(w=!0,setTimeout(M,0)))},M=()=>{for(let e of x)if(!e.y&&!e.b&&!e.a?.size&&!e.o?.size&&!e.l?.size&&(e.state.promise=I,e.r=e.p=e.state.error=e.state.value=void 0,e.n=e.u=e.s=!1,e.t&&(e.t.abort(),e.t=void 0),e.i)){for(let t of e.i)t.a.delete(e),i(t);if(e.i.clear(),e.c){for(let t of e.c)t.a.delete(e),i(t);e.c.clear()}}x.clear(),w=!1},g=(e,t,a)=>Object.is(e,t)||a!==void 0&&t!==void 0&&a(e,t),W=e=>typeof e?.then=="function",T=()=>{let e=new AbortController,t=e.signal,a=new Promise(n=>{t.then=r=>a.then(r),t.addEventListener("abort",n,{once:!0,passive:!0})});return{abort:()=>e.abort(),signal:t}},p=e=>{queueMicrotask(()=>{throw e})};export{m as $,U as $$,R as createScope,I as inactive};
|
package/dist/index.d.ts
CHANGED
|
@@ -65,9 +65,9 @@ export type AtomScope = {
|
|
|
65
65
|
<Value>(baseAtom: PrimitiveAtom<Value>): PrimitiveAtom<Value>;
|
|
66
66
|
<Value>(baseAtom: DerivedAtom<Value>): DerivedAtom<Value>;
|
|
67
67
|
<Value>(baseAtom: Atom<Value>): Atom<Value>;
|
|
68
|
-
<Value>(baseAtom: PrimitiveAtom<Value>,
|
|
69
|
-
<Value>(baseAtom: DerivedAtom<Value>,
|
|
70
|
-
<Value>(baseAtom: Atom<Value>,
|
|
68
|
+
<Value>(baseAtom: PrimitiveAtom<Value>, strict: true): PrimitiveAtom<Value> | undefined;
|
|
69
|
+
<Value>(baseAtom: DerivedAtom<Value>, strict: true): DerivedAtom<Value> | undefined;
|
|
70
|
+
<Value>(baseAtom: Atom<Value>, strict: true): Atom<Value> | undefined;
|
|
71
71
|
};
|
|
72
72
|
export type SetLike<Key> = Key[] | Set<Key> | (Key extends object ? WeakSet<Key> : never);
|
|
73
73
|
export type MapLike<Key, Value> = Map<Key, Value> | (Key extends object ? WeakMap<Key, Value> : never) | (Key extends string | number | symbol ? Record<Key, Value> : never);
|
package/dist/index.js
CHANGED
|
@@ -145,17 +145,19 @@ const $$ = (init) => $((get, options) => {
|
|
|
145
145
|
});
|
|
146
146
|
const createScope = (parentScope, atomValuePairs) => {
|
|
147
147
|
const scopeMap = /* @__PURE__ */ new WeakMap();
|
|
148
|
+
const atomMap = /* @__PURE__ */ new WeakMap();
|
|
148
149
|
if (atomValuePairs) {
|
|
149
150
|
for (const [atom, value] of atomValuePairs) {
|
|
150
151
|
scopeMap.set(atom, value instanceof CommonAtomInternal ? value : $(value));
|
|
151
152
|
}
|
|
152
153
|
}
|
|
153
|
-
const scope = ((baseAtom) => {
|
|
154
|
+
const scope = ((baseAtom, strict = false) => {
|
|
154
155
|
let scopedAtom = scopeMap.get(baseAtom);
|
|
156
|
+
if (strict) return scopedAtom || parentScope?.(baseAtom, true);
|
|
155
157
|
if (!scopedAtom) {
|
|
156
|
-
const parentAtom = parentScope?.(baseAtom);
|
|
158
|
+
const parentAtom = parentScope?.(baseAtom, true);
|
|
157
159
|
const realBaseAtom = parentAtom || baseAtom;
|
|
158
|
-
|
|
160
|
+
atomMap.set(
|
|
159
161
|
baseAtom,
|
|
160
162
|
scopedAtom = realBaseAtom.l instanceof Function ? $((get, options) => realBaseAtom.l(
|
|
161
163
|
(atom, unwrap) => get(scope(atom), unwrap),
|
package/package.json
CHANGED
package/tests/bansa.test.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
-
import { $, createScope, DerivedAtom, type ThenableSignal } from '../src/index';
|
|
2
|
+
import { $, createScope, type DerivedAtom, type PrimitiveAtom, type ThenableSignal } from '../src/index';
|
|
3
3
|
|
|
4
4
|
const flushMicrotasks = () =>
|
|
5
5
|
new Promise((resolve) => {
|
|
@@ -488,7 +488,7 @@ describe('Atom Library - Advanced Tests', () => {
|
|
|
488
488
|
expect(derivedAtom1.state.value).toEqual(undefined);
|
|
489
489
|
});
|
|
490
490
|
|
|
491
|
-
it('deep
|
|
491
|
+
it('scope with deep dependencies', async () => {
|
|
492
492
|
const $x1 = $(1);
|
|
493
493
|
const $x2 = $((get) => get($x1) + 1);
|
|
494
494
|
const $x3 = $((get) => get($x2) + 1);
|
|
@@ -531,6 +531,62 @@ describe('Atom Library - Advanced Tests', () => {
|
|
|
531
531
|
expect($y2.get()).toBe(25);
|
|
532
532
|
});
|
|
533
533
|
|
|
534
|
+
it('deep scope', async () => {
|
|
535
|
+
const $x1 = $(1); // 1
|
|
536
|
+
const $x2 = $((get) => get($x1) + 1); // 2
|
|
537
|
+
const $x3 = $((get) => get($x2) + 1); // 3
|
|
538
|
+
const $x4 = $((get) => get($x3) + 1); // 4
|
|
539
|
+
const $x5 = $((get) => get($x1) + get($x2) + get($x3) + get($x4)); // 10
|
|
540
|
+
const identity = (x: any) => x;
|
|
541
|
+
const scope0 = createScope(identity);
|
|
542
|
+
const scope1 = createScope(scope0, [
|
|
543
|
+
[$x1, 11],
|
|
544
|
+
]);
|
|
545
|
+
const scope2 = createScope(scope1, [
|
|
546
|
+
[$x2, 22],
|
|
547
|
+
]);
|
|
548
|
+
|
|
549
|
+
$x5.subscribe(nop);
|
|
550
|
+
await flushMicrotasks();
|
|
551
|
+
|
|
552
|
+
const $y0 = scope0($x5);
|
|
553
|
+
const $y1 = scope1($x5);
|
|
554
|
+
const $y2 = scope2($x5);
|
|
555
|
+
|
|
556
|
+
expect($x5.get()).toBe(10);
|
|
557
|
+
expect($y0.get()).toBe(10);
|
|
558
|
+
expect($y1.get()).toBe(50);
|
|
559
|
+
expect($y2.get()).toBe(80);
|
|
560
|
+
|
|
561
|
+
$x1.set(101);
|
|
562
|
+
await flushMicrotasks();
|
|
563
|
+
expect($x5.get()).toBe(410);
|
|
564
|
+
expect($y0.get()).toBe(410);
|
|
565
|
+
expect($y1.get()).toBe(50);
|
|
566
|
+
expect($y2.get()).toBe(80);
|
|
567
|
+
|
|
568
|
+
scope1($x1).set(101);
|
|
569
|
+
await flushMicrotasks();
|
|
570
|
+
expect($x5.get()).toBe(410);
|
|
571
|
+
expect($y0.get()).toBe(410);
|
|
572
|
+
expect($y1.get()).toBe(410);
|
|
573
|
+
expect($y2.get()).toBe(170);
|
|
574
|
+
|
|
575
|
+
scope2($x1).set(201);
|
|
576
|
+
await flushMicrotasks();
|
|
577
|
+
expect($x5.get()).toBe(410);
|
|
578
|
+
expect($y0.get()).toBe(410);
|
|
579
|
+
expect($y1.get()).toBe(810);
|
|
580
|
+
expect($y2.get()).toBe(270);
|
|
581
|
+
|
|
582
|
+
(scope2($x2) as PrimitiveAtom<number>).set(202);
|
|
583
|
+
await flushMicrotasks();
|
|
584
|
+
expect($x5.get()).toBe(410);
|
|
585
|
+
expect($y0.get()).toBe(410);
|
|
586
|
+
expect($y1.get()).toBe(810);
|
|
587
|
+
expect($y2.get()).toBe(810);
|
|
588
|
+
});
|
|
589
|
+
|
|
534
590
|
it('should not provide stale values to conditional dependents', async () => {
|
|
535
591
|
const dataAtom = $([100]);
|
|
536
592
|
const hasFilterAtom = $(false);
|