bansa 0.0.16 → 0.0.17

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.
@@ -1 +1 @@
1
- var f=class{r;p;a;o;l;get(){if(this.s||(P(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;I(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:k,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 k=Promise.reject();k.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,r=((l,s=!1)=>{let o=a.get(l);if(!o){let h=e?.(l,!0);if(s)return h;let d=h||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}):h||m(d.d))}return o});if(t)for(let[l,s]of t)a.set(l,s instanceof f?r(s):m(s));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},v=!1,S=[],w=[],_=e=>{e.u||(e.u=!0,b(e))},b=e=>{e.n||(e.n=!0,S.push(e),v||(v=!0,queueMicrotask(C)))},C=()=>{v=!1;{let t=S;S=[];for(let a of t)a.state.promise=void 0,a.state.error=a.p,a.state.value=a.r,D(a)}let e=w;w=[];for(let t=e.length;t--;){let a=e[t];a.m=!1,a.u&&(a.n=!0,P(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);w.push(e)}},u=class{e;constructor(t){this.e=t}},G=Symbol(),P=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||(P(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),I(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,I(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},x=!1,g=new Set,i=e=>{!e.y&&!e.b&&!e.a?.size&&!e.o?.size&&!e.l?.size&&(g.add(e),x||(x=!0,setTimeout(M,0)))},M=()=>{for(let e of g)if(!e.y&&!e.b&&!e.a?.size&&!e.o?.size&&!e.l?.size&&(e.state.promise=k,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()}}g.clear(),x=!1},I=(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,k as inactive};
1
+ var f=class{r;p;a;o;l;get(){if(this.s||(P(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;I(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:k,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 k=Promise.reject();k.catch(O);var m=(e,t)=>e instanceof Function?new y(e,t):new c(e,t),R=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:C}),z=e=>e instanceof f,$=(e,t)=>{let a=new WeakMap,n=new WeakMap,r=((l,s=!1)=>{let o=a.get(l);if(s||(o||=n.get(l)),!o){let h=e?.(l,!0);if(s)return h;let d=h||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}):h||m(d.d))}return o});if(t)for(let[l,s]of t)a.set(l,z(s)?r(s):m(s));return r},C=(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},v=!1,S=[],w=[],_=e=>{e.u||(e.u=!0,b(e))},b=e=>{e.n||(e.n=!0,S.push(e),v||(v=!0,queueMicrotask(M)))},M=()=>{v=!1;{let t=S;S=[];for(let a of t)a.state.promise=void 0,a.state.error=a.p,a.state.value=a.r,D(a)}let e=w;w=[];for(let t=e.length;t--;){let a=e[t];a.m=!1,a.u&&(a.n=!0,P(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);w.push(e)}},u=class{e;constructor(t){this.e=t}},G=Symbol(),P=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||(P(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);U(a)?(e.state.promise=a,a.then(n=>{t===e.f&&(A(e),I(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,I(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},x=!1,g=new Set,i=e=>{!e.y&&!e.b&&!e.a?.size&&!e.o?.size&&!e.l?.size&&(g.add(e),x||(x=!0,setTimeout(W,0)))},W=()=>{for(let e of g)if(!e.y&&!e.b&&!e.a?.size&&!e.o?.size&&!e.l?.size&&(e.state.promise=k,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()}}g.clear(),x=!1},I=(e,t,a)=>Object.is(e,t)||a!==void 0&&t!==void 0&&a(e,t),U=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 $,R as $$,$ as createScope,k as inactive,z as isAtom};
package/dist/index.d.ts CHANGED
@@ -74,6 +74,7 @@ export type MapLike<Key, Value> = Map<Key, Value> | (Key extends object ? WeakMa
74
74
  export declare const inactive: Promise<never>;
75
75
  export declare const $: CreateAtom;
76
76
  export declare const $$: <Value>(init: AtomGetter<Value>) => DerivedAtom<Value>;
77
+ export declare const isAtom: (x: unknown) => x is Atom<unknown>;
77
78
  export type AtomValuePair<Value> = [Atom<Value>, Value | PrimitiveAtom<Value>] | [DerivedAtom<Value>, Value | Atom<Value>];
78
79
  export declare const createScope: <T extends AtomValuePair<unknown>[]>(parentScope?: AtomScope | null, atomValuePairs?: T) => AtomScope;
79
80
  export {};
package/dist/index.js CHANGED
@@ -143,11 +143,13 @@ const $$ = (init) => $((get, options) => {
143
143
  }, {
144
144
  equals: shallowEquals
145
145
  });
146
+ const isAtom = (x) => x instanceof CommonAtomInternal;
146
147
  const createScope = (parentScope, atomValuePairs) => {
147
148
  const scopeMap = /* @__PURE__ */ new WeakMap();
148
149
  const atomMap = /* @__PURE__ */ new WeakMap();
149
150
  const scope = ((baseAtom, strict = false) => {
150
151
  let scopedAtom = scopeMap.get(baseAtom);
152
+ if (!strict) scopedAtom ||= atomMap.get(baseAtom);
151
153
  if (!scopedAtom) {
152
154
  const parentAtom = parentScope?.(baseAtom, true);
153
155
  if (strict) return parentAtom;
@@ -167,7 +169,7 @@ const createScope = (parentScope, atomValuePairs) => {
167
169
  });
168
170
  if (atomValuePairs) {
169
171
  for (const [atom, value] of atomValuePairs) {
170
- scopeMap.set(atom, value instanceof CommonAtomInternal ? scope(value) : $(value));
172
+ scopeMap.set(atom, isAtom(value) ? scope(value) : $(value));
171
173
  }
172
174
  }
173
175
  return scope;
@@ -449,5 +451,6 @@ export {
449
451
  $,
450
452
  $$,
451
453
  createScope,
452
- inactive
454
+ inactive,
455
+ isAtom
453
456
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bansa",
3
- "version": "0.0.16",
3
+ "version": "0.0.17",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -1109,7 +1109,7 @@ describe('Bansa Documentation Examples as Tests', () => {
1109
1109
  ]);
1110
1110
 
1111
1111
  const $y2 = scope($y);
1112
- expect(scope($x)).toBe($x2);
1112
+ expect(scope($x).get()).toBe($x2.get());
1113
1113
  expect($y2).not.toBe($y);
1114
1114
  expect($y2).toBe(scope($y));
1115
1115
 
@@ -1207,7 +1207,7 @@ describe('Bansa Documentation Examples as Tests', () => {
1207
1207
  expect($x.get()).toBe(10);
1208
1208
  expect($y.get()).toBe(11);
1209
1209
  expect($x2.get()).toBe(1000);
1210
- expect($y2.get()).toBe(1001);
1210
+ expect($y2.get()).toBe(101);
1211
1211
  });
1212
1212
  });
1213
1213
 
Binary file