bansa 0.0.13 → 0.0.14

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 d=class{r;V;a;o;l;get(){if(this.s||(I(this),u(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||k(this),(this.o||=new Set).add(t),()=>{this.o.delete(t),this.o.size||u(this)}}subscribe(t){let a={h:t,p:{get signal(){return(a.t||=q()).signal}}};if(!this.s)k(this);else if(!this.state.error&&!this.state.promise)try{t(this.state.value,a.p)}catch(n){f(n)}return(this.l||=new Set).add(a),()=>{this.l.delete(a),a.t&&(a.t.abort(),a.t=void 0),this.l.size||u(this)}}[Symbol.toPrimitive](){return this.state.value}},i=class extends d{n=!1;A=!1;constructor(t,a){super(),this.d=t,this.m=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;x(a,this.state.value,this.m)||(this.r=a,m(this))}};i.prototype.y=!0;i.prototype.s=!0;i.prototype.u=!1;var A=class extends d{s=!1;u=!1;n=!1;A=!1;f=0;t;i;c;constructor(t,a){super(),this.d=t,this.m=a?.equals,this.b=!!a?.persist;let n=this;this.p={get signal(){return(n.t||=q()).signal}},this.state={promise:g,error:void 0,value:void 0}}};A.prototype.y=!1;var c=()=>c,_=()=>{};Object.setPrototypeOf(c,new Proxy(c,{get:(e,t)=>t===Symbol.toPrimitive?_:c}));var g=Promise.reject();g.catch(_);var p=(e,t)=>e instanceof Function?new A(e,t):new i(e,t),z=e=>p((t,a)=>{let n,r,o=e(y=>{let l=t(y,!1);if(l.error)r=l.error;else if(l.promise)(n||=[]).push(l.promise);else return l.value;return c},a);if(r)throw r;if(n)throw Promise.all(n);return o},{equals:T}),C=(e,t)=>{let a=new WeakMap;if(t)for(let[r,o]of t)a.set(r,o instanceof d?o:p(o));let n=(r=>{let o=a.get(r);return o||a.set(r,o=r.d instanceof Function?p((y,l)=>r.d((D,E)=>y(n(D),E),l)):e?.(r)||p(r.d)),o});return n},T=(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},b=!1,h=[],v=[],k=e=>{e.u||(e.u=!0,m(e))},m=e=>{e.n||(e.n=!0,h.push(e),b||(b=!0,queueMicrotask(K)))},K=()=>{b=!1;{let t=h;h=[];for(let a of t)a.state.promise=void 0,a.state.error=a.V,a.state.value=a.r,O(a)}let e=v;v=[];for(let t=e.length;t--;){let a=e[t];a.A=!1,a.u&&(a.n=!0,I(a)),a.n&&G(a)}},G=e=>{if(e.n=!1,e.o)for(let t of e.o)try{t()}catch(a){f(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.p)}catch(a){f(a)}}if(e.a)for(let t of e.a)t.u=!0}},O=e=>{if(!e.A){if(e.A=!0,e.a)for(let t of e.a)O(t);v.push(e)}},s=class{e;constructor(t){this.e=t}},P=Symbol(),I=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 P;if(e!==n&&(n.s||(I(n),n.n&&G(n)),(e.c||=new Set).add(n),(n.a||=new Set).add(e)),!r)return n.state;if(n.state.error)throw new s(n.state.error);if(n.state.promise)throw new s(n.state.promise);return n.state.value},e.p);j(a)?(e.state.promise=a,a.then(n=>{t===e.f&&(V(e),x(n,e.state.value,e.m)?e.state.promise=void 0:(e.r=n,e.V=void 0),m(e))},n=>{t===e.f&&(V(e),n instanceof s?n=n.e:f(n),e.V=n,m(e))})):(V(e),e.state.error=void 0,x(a,e.state.value,e.m)?e.n=!1:e.state.value=e.r=a)}catch(a){V(e),a===P?e.n=!1:(a instanceof s?a=a.e:f(a),e.state.error=a)}},V=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),u(a));t.clear()}e.c=t},S=!1,w=new Set,u=e=>{!e.y&&!e.b&&!e.a?.size&&!e.o?.size&&!e.l?.size&&(w.add(e),S||(S=!0,setTimeout(L,0)))},L=()=>{for(let e of w)if(!e.y&&!e.b&&!e.a?.size&&!e.o?.size&&!e.l?.size&&(e.state.promise=g,e.r=e.V=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),u(t);if(e.i.clear(),e.c){for(let t of e.c)t.a.delete(e),u(t);e.c.clear()}}w.clear(),S=!1},x=(e,t,a)=>Object.is(e,t)||a!==void 0&&t!==void 0&&a(e,t),j=e=>typeof e?.then=="function",q=()=>{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}},f=e=>{queueMicrotask(()=>{throw e})};export{p as $,z as $$,C as createScope,g as inactive};
1
+ var d=class{r;p;a;o;l;get(){if(this.s||(I(this),u(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||k(this),(this.o||=new Set).add(t),()=>{this.o.delete(t),this.o.size||u(this)}}subscribe(t){let a={h:t,A:{get signal(){return(a.t||=q()).signal}}};if(!this.s)k(this);else if(!this.state.error&&!this.state.promise)try{t(this.state.value,a.A)}catch(n){V(n)}return(this.l||=new Set).add(a),()=>{this.l.delete(a),a.t&&(a.t.abort(),a.t=void 0),this.l.size||u(this)}}[Symbol.toPrimitive](){return this.state.value}},i=class extends d{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;x(a,this.state.value,this.V)||(this.r=a,y(this))}};i.prototype.y=!0;i.prototype.s=!0;i.prototype.u=!1;var m=class extends d{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||=q()).signal}},this.state={promise:g,error:void 0,value:void 0}}};m.prototype.y=!1;var c=()=>c,_=()=>{};Object.setPrototypeOf(c,new Proxy(c,{get:(e,t)=>t===Symbol.toPrimitive?_:c}));var g=Promise.reject();g.catch(_);var A=(e,t)=>e instanceof Function?new m(e,t):new i(e,t),M=e=>A((t,a)=>{let n,r,l=e(f=>{let o=t(f,!1);if(o.error)r=o.error;else if(o.promise)(n||=[]).push(o.promise);else return o.value;return c},a);if(r)throw r;if(n)throw Promise.all(n);return l},{equals:L}),W=(e,t)=>{let a=new WeakMap;if(t)for(let[r,l]of t)a.set(r,l instanceof d?l:A(l));let n=(r=>{let l=a.get(r);if(!l){let f=e?.(r),o=f||r;a.set(r,l=o.d instanceof Function?A((D,T)=>o.d((E,K)=>D(n(E),K),T),{equals:o.V,persist:o.b}):f||A(o.d))}return l});return n},L=(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},b=!1,h=[],v=[],k=e=>{e.u||(e.u=!0,y(e))},y=e=>{e.n||(e.n=!0,h.push(e),b||(b=!0,queueMicrotask(j)))},j=()=>{b=!1;{let t=h;h=[];for(let a of t)a.state.promise=void 0,a.state.error=a.p,a.state.value=a.r,O(a)}let e=v;v=[];for(let t=e.length;t--;){let a=e[t];a.m=!1,a.u&&(a.n=!0,I(a)),a.n&&G(a)}},G=e=>{if(e.n=!1,e.o)for(let t of e.o)try{t()}catch(a){V(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){V(a)}}if(e.a)for(let t of e.a)t.u=!0}},O=e=>{if(!e.m){if(e.m=!0,e.a)for(let t of e.a)O(t);v.push(e)}},s=class{e;constructor(t){this.e=t}},P=Symbol(),I=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 P;if(e!==n&&(n.s||(I(n),n.n&&G(n)),(e.c||=new Set).add(n),(n.a||=new Set).add(e)),!r)return n.state;if(n.state.error)throw new s(n.state.error);if(n.state.promise)throw new s(n.state.promise);return n.state.value},e.A);C(a)?(e.state.promise=a,a.then(n=>{t===e.f&&(p(e),x(n,e.state.value,e.V)?e.state.promise=void 0:(e.r=n,e.p=void 0),y(e))},n=>{t===e.f&&(p(e),n instanceof s?n=n.e:V(n),e.p=n,y(e))})):(p(e),e.state.error=void 0,x(a,e.state.value,e.V)?e.n=!1:e.state.value=e.r=a)}catch(a){p(e),a===P?e.n=!1:(a instanceof s?a=a.e:V(a),e.state.error=a)}},p=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),u(a));t.clear()}e.c=t},S=!1,w=new Set,u=e=>{!e.y&&!e.b&&!e.a?.size&&!e.o?.size&&!e.l?.size&&(w.add(e),S||(S=!0,setTimeout(z,0)))},z=()=>{for(let e of w)if(!e.y&&!e.b&&!e.a?.size&&!e.o?.size&&!e.l?.size&&(e.state.promise=g,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),u(t);if(e.i.clear(),e.c){for(let t of e.c)t.a.delete(e),u(t);e.c.clear()}}w.clear(),S=!1},x=(e,t,a)=>Object.is(e,t)||a!==void 0&&t!==void 0&&a(e,t),C=e=>typeof e?.then=="function",q=()=>{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}},V=e=>{queueMicrotask(()=>{throw e})};export{A as $,M as $$,W as createScope,g as inactive};
package/dist/index.d.ts CHANGED
@@ -74,6 +74,6 @@ 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 type AtomValuePair<Value> = [Atom<Value>, Value | Atom<Value>];
78
- export declare const createScope: (parentScope?: AtomScope | null, atomValuePairs?: AtomValuePair<unknown>[]) => AtomScope;
77
+ export type AtomValuePair<Value> = [Atom<Value>, Value | PrimitiveAtom<Value>] | [DerivedAtom<Value>, Value | Atom<Value>];
78
+ export declare const createScope: <T extends AtomValuePair<unknown>[]>(parentScope?: AtomScope | null, atomValuePairs?: T) => AtomScope;
79
79
  export {};
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  class CommonAtomInternal {
2
2
  d;
3
- n;
3
+ o;
4
4
  b;
5
5
  f;
6
6
  g;
@@ -28,7 +28,7 @@ class CommonAtomInternal {
28
28
  subscribe(subscriber) {
29
29
  const atomSubscriber = {
30
30
  t: subscriber,
31
- o: {
31
+ p: {
32
32
  get signal() {
33
33
  return (atomSubscriber.a ||= createThenableSignal()).signal;
34
34
  }
@@ -38,7 +38,7 @@ class CommonAtomInternal {
38
38
  requestActivate(this);
39
39
  } else if (!this.state.error && !this.state.promise) {
40
40
  try {
41
- subscriber(this.state.value, atomSubscriber.o);
41
+ subscriber(this.state.value, atomSubscriber.p);
42
42
  } catch (e) {
43
43
  logError(e);
44
44
  }
@@ -61,11 +61,11 @@ class CommonAtomInternal {
61
61
  }
62
62
  class PrimitiveAtomInternal extends CommonAtomInternal {
63
63
  c = false;
64
- p = false;
64
+ q = false;
65
65
  constructor(init, options) {
66
66
  super();
67
67
  this.l = init;
68
- this.q = options?.equals;
68
+ this.m = options?.equals;
69
69
  this.d = init;
70
70
  this.state = {
71
71
  promise: void 0,
@@ -75,7 +75,7 @@ class PrimitiveAtomInternal extends CommonAtomInternal {
75
75
  }
76
76
  set(value) {
77
77
  const nextValue = value instanceof Function ? value(this.d) : value;
78
- if (!equals(nextValue, this.state.value, this.q)) {
78
+ if (!equals(nextValue, this.state.value, this.m)) {
79
79
  this.d = nextValue;
80
80
  requestPropagate(this);
81
81
  }
@@ -88,18 +88,18 @@ class DerivedAtomInternal extends CommonAtomInternal {
88
88
  h = false;
89
89
  i = false;
90
90
  c = false;
91
- p = false;
92
- m = 0;
91
+ q = false;
92
+ n = 0;
93
93
  a;
94
94
  j;
95
95
  k;
96
96
  constructor(init, options) {
97
97
  super();
98
98
  this.l = init;
99
- this.q = options?.equals;
99
+ this.m = options?.equals;
100
100
  this.s = !!options?.persist;
101
101
  const self = this;
102
- this.o = {
102
+ this.p = {
103
103
  get signal() {
104
104
  return (self.a ||= createThenableSignal()).signal;
105
105
  }
@@ -152,13 +152,20 @@ const createScope = (parentScope, atomValuePairs) => {
152
152
  }
153
153
  const scope = ((baseAtom) => {
154
154
  let scopedAtom = scopeMap.get(baseAtom);
155
- if (!scopedAtom) scopeMap.set(
156
- baseAtom,
157
- scopedAtom = baseAtom.l instanceof Function ? $((get, options) => baseAtom.l(
158
- (atom, unwrap) => get(scope(atom), unwrap),
159
- options
160
- )) : parentScope?.(baseAtom) || $(baseAtom.l)
161
- );
155
+ if (!scopedAtom) {
156
+ const parentAtom = parentScope?.(baseAtom);
157
+ const realBaseAtom = parentAtom || baseAtom;
158
+ scopeMap.set(
159
+ baseAtom,
160
+ scopedAtom = realBaseAtom.l instanceof Function ? $((get, options) => realBaseAtom.l(
161
+ (atom, unwrap) => get(scope(atom), unwrap),
162
+ options
163
+ ), {
164
+ equals: realBaseAtom.m,
165
+ persist: realBaseAtom.s
166
+ }) : parentAtom || $(realBaseAtom.l)
167
+ );
168
+ }
162
169
  return scopedAtom;
163
170
  });
164
171
  return scope;
@@ -207,7 +214,7 @@ const updateAtoms = () => {
207
214
  updateQueue = [];
208
215
  for (const atom of updatedAtoms) {
209
216
  atom.state.promise = void 0;
210
- atom.state.error = atom.n;
217
+ atom.state.error = atom.o;
211
218
  atom.state.value = atom.d;
212
219
  mark(atom);
213
220
  }
@@ -216,7 +223,7 @@ const updateAtoms = () => {
216
223
  stack = [];
217
224
  for (let i = markedAtoms.length; i--; ) {
218
225
  const atom = markedAtoms[i];
219
- atom.p = false;
226
+ atom.q = false;
220
227
  if (atom.i) {
221
228
  atom.c = true;
222
229
  execute(atom);
@@ -245,7 +252,7 @@ const propagate = (atom) => {
245
252
  subscriber.a = void 0;
246
253
  }
247
254
  try {
248
- subscriber.t(atom.state.value, subscriber.o);
255
+ subscriber.t(atom.state.value, subscriber.p);
249
256
  } catch (e) {
250
257
  logError(e);
251
258
  }
@@ -259,8 +266,8 @@ const propagate = (atom) => {
259
266
  }
260
267
  };
261
268
  const mark = (atom) => {
262
- if (!atom.p) {
263
- atom.p = true;
269
+ if (!atom.q) {
270
+ atom.q = true;
264
271
  if (atom.b) {
265
272
  for (const child of atom.b) {
266
273
  mark(child);
@@ -277,7 +284,7 @@ class Wrapped {
277
284
  }
278
285
  const expired = Symbol();
279
286
  const execute = (atom) => {
280
- const counter = ++atom.m;
287
+ const counter = ++atom.n;
281
288
  atom.h = true;
282
289
  atom.i = false;
283
290
  atom.state.promise = void 0;
@@ -288,7 +295,7 @@ const execute = (atom) => {
288
295
  try {
289
296
  const value = atom.l(
290
297
  (anotherAtom, unwrap = true) => {
291
- if (counter !== atom.m) throw expired;
298
+ if (counter !== atom.n) throw expired;
292
299
  if (atom !== anotherAtom) {
293
300
  if (!anotherAtom.h) {
294
301
  execute(anotherAtom);
@@ -306,32 +313,32 @@ const execute = (atom) => {
306
313
  throw new Wrapped(anotherAtom.state.promise);
307
314
  return anotherAtom.state.value;
308
315
  },
309
- atom.o
316
+ atom.p
310
317
  );
311
318
  if (isPromiseLike(value)) {
312
319
  atom.state.promise = value;
313
320
  value.then(
314
321
  (value2) => {
315
- if (counter === atom.m) {
322
+ if (counter === atom.n) {
316
323
  finalizeExecution(atom);
317
- if (equals(value2, atom.state.value, atom.q)) {
324
+ if (equals(value2, atom.state.value, atom.m)) {
318
325
  atom.state.promise = void 0;
319
326
  } else {
320
327
  atom.d = value2;
321
- atom.n = void 0;
328
+ atom.o = void 0;
322
329
  }
323
330
  requestPropagate(atom);
324
331
  }
325
332
  },
326
333
  (e) => {
327
- if (counter === atom.m) {
334
+ if (counter === atom.n) {
328
335
  finalizeExecution(atom);
329
336
  if (e instanceof Wrapped) {
330
337
  e = e.e;
331
338
  } else {
332
339
  logError(e);
333
340
  }
334
- atom.n = e;
341
+ atom.o = e;
335
342
  requestPropagate(atom);
336
343
  }
337
344
  }
@@ -339,7 +346,7 @@ const execute = (atom) => {
339
346
  } else {
340
347
  finalizeExecution(atom);
341
348
  atom.state.error = void 0;
342
- if (equals(value, atom.state.value, atom.q)) {
349
+ if (equals(value, atom.state.value, atom.m)) {
343
350
  atom.c = false;
344
351
  } else {
345
352
  atom.state.value = atom.d = value;
@@ -360,7 +367,7 @@ const execute = (atom) => {
360
367
  }
361
368
  };
362
369
  const finalizeExecution = (atom) => {
363
- ++atom.m;
370
+ ++atom.n;
364
371
  const oldDependencies = atom.j;
365
372
  atom.j = atom.k;
366
373
  if (oldDependencies) {
@@ -389,7 +396,7 @@ const gc = () => {
389
396
  for (const atom of gcCandidates) {
390
397
  if (!atom.r && !atom.s && !atom.b?.size && !atom.f?.size && !atom.g?.size) {
391
398
  atom.state.promise = inactive;
392
- atom.d = atom.n = atom.state.error = atom.state.value = void 0;
399
+ atom.d = atom.o = atom.state.error = atom.state.value = void 0;
393
400
  atom.c = atom.i = atom.h = false;
394
401
  if (atom.a) {
395
402
  atom.a.abort();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bansa",
3
- "version": "0.0.13",
3
+ "version": "0.0.14",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",