bansa 0.0.8 → 0.0.10

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 s=function(){};s.prototype.get=function(){if(this.r||(v(this),l(this)),this.state.promise)throw this.state.promise;if(this.state.error)throw this.state.error;return this.state.value};s.prototype.watch=function(e){return this.r||w(this),(this.l??=new Set).add(e),()=>{this.l.delete(e),this.l.size||l(this)}};s.prototype.subscribe=function(e){let t={d:e,V:{get signal(){return(t.t??=g()).signal}}};if(!this.r)w(this);else if(!this.state.promise&&!this.state.error)try{e(this.state.value,t.V)}catch(r){A(r)}return(this.s??=new Set).add(t),()=>{this.s.delete(t),t.t&&(t.t.abort(),t.t=void 0),this.s.size||l(this)}};s.prototype[Symbol.toPrimitive]=function(){return this.state.value};var u=function(e,t){this.c=t?.equals,this.state={promise:void 0,error:void 0,value:e},this.state.value=this.a=e};u.prototype.set=function(e){let t=e instanceof Function?e(this.a):e;h(t,this.state.value,this.c)||(this.a=t,f(this))};u.prototype.A=!0;u.prototype.r=!0;u.prototype.n=!1;Object.setPrototypeOf(u.prototype,s.prototype);var p=function(e,t){this.y=e,this.c=t?.equals,this.h=t?.persist,this.state={promise:S,error:void 0,value:void 0};let r=this;this.V={get signal(){return(r.t??=g()).signal}}};p.prototype.A=!1;p.prototype.r=!1;p.prototype.n=!1;p.prototype.i=0;Object.setPrototypeOf(p.prototype,s.prototype);var c=()=>c,k=()=>{};Object.setPrototypeOf(c,new Proxy(c,{get:(e,t)=>t===Symbol.toPrimitive?k:c}));var S=Promise.resolve(),_=(e,t)=>e instanceof Function?new p(e,t):new u(e,t),D=e=>_((t,r)=>{let o,n,m=e(x=>{try{return t(x)}catch(V){if(!V)throw V;b(V)?(o??=[]).push(V):n=V}return c},r);if(n)throw n;if(o)throw Promise.all(o);return m},{equals:O}),O=(e,t)=>{if(typeof e!="object"||typeof t!="object"||!e||!t)return!1;let r=e.constructor;if(r!==t.constructor)return!1;if(r===Array){let n=e.length;if(n!==t.length)return!1;for(;(n=n-1|0)>=0;)if(!Object.is(e[n],t[n]))return!1;return!0}let o=0;for(let n in e){if(!(n in t&&Object.is(e[n],t[n])))return!1;o=o+1|0}for(let n in t)if((o=o-1|0)<0)return!1;return!0},y=!1,a=[],w=e=>{e.u||(e.u=!0,f(e))},f=e=>{e.n||(e.n=!0,a.push(e),y||(y=!0,queueMicrotask(G)))},G=()=>{y=!1;{let t=a;a=[];for(let r of t)r.state.promise=void 0,r.state.error=r.f,r.state.value=r.a,P(r)}let e=a;a=[];for(let t=e.length;t--;){let r=e[t];r.m=!1,r.u&&(r.n=!0,v(r)),r.n&&I(r)}},I=e=>{if(e.n=!1,e.o)for(let t of e.o)t.u=!0;if(e.l)for(let t of e.l)t();if(e.s&&!e.state.promise&&!e.state.error)for(let t of e.s){t.t&&(t.t.abort(),t.t=void 0);try{t.d(e.state.value,t.V)}catch(r){A(r)}}},P=e=>{if(!e.m){if(e.m=!0,e.o)for(let t of e.o)P(t);a.push(e)}},i=class{e;constructor(t){this.e=t}},v=e=>{let t=++e.i;e.r=!0,e.u=!1,e.state.promise=void 0,e.t&&(e.t.abort(),e.t=void 0);let r=e.p;r&&(e.p=new Set);try{let o=e.y((n,m=!0)=>{if(t!==e.i)throw void 0;if(e!==n&&(n.r||(v(n),n.n&&(n.n=!1,I(n))),r?.delete(n),(e.p??=new Set).add(n),(n.o??=new Set).add(e)),!m)return n.state;if(n.state.promise)throw new i(n.state.promise);if(n.state.error)throw new i(n.state.error);return n.state.value},e.V);b(o)?(e.state.promise=o,o.then(n=>{t===e.i&&(h(n,e.state.value,e.c)?e.state.promise=void 0:(e.a=n,e.f=void 0,f(e)))},n=>{t===e.i&&(n instanceof Promise?e.state.promise=void 0:(n instanceof i?n=n.e:A(n),e.f=n,f(e)))})):(++e.i,e.state.error=void 0,h(o,e.state.value,e.c)?e.n=!1:e.state.value=e.a=o)}catch(o){++e.i,o?(o instanceof i?o=o.e:A(o),b(o)?e.state.promise=o:e.state.error=o):e.n=!1}if(r)for(let o of r)o.o.delete(e),l(o)},d=!1,l=e=>{if(!e.A&&!e.h&&!e.o?.size&&!e.l?.size&&!e.s?.size){if(!d){setTimeout(()=>{d=!0,l(e),d=!1},0);return}if(e.state.promise=S,e.a=e.f=e.state.error=e.state.value=void 0,e.n=e.u=e.r=!1,e.t&&(e.t.abort(),e.t=void 0),e.p){for(let t of e.p)t.o.delete(e),l(t);e.p.clear()}}},h=(e,t,r)=>Object.is(e,t)||r!==void 0&&t!==void 0&&r(e,t),b=e=>typeof e?.then=="function",g=()=>{let e=new AbortController,t=e.signal,r=new Promise(o=>{t.then=n=>r.then(n),t.addEventListener("abort",o,{once:!0,passive:!0})});return{abort:()=>e.abort(),signal:t}},A=e=>{queueMicrotask(()=>{throw e})};export{_ as $,D as $$,S as inactive};
1
+ var p=function(){};p.prototype.get=function(){if(this.o||(w(this),u(this)),this.state.promise)throw this.state.promise;if(this.state.error)throw this.state.error;return this.state.value};p.prototype.watch=function(e){return this.o||g(this),(this.s??=new Set).add(e),()=>{this.s.delete(e),this.s.size||u(this)}};p.prototype.subscribe=function(e){let t={y:e,c:{get signal(){return(t.t??=k()).signal}}};if(!this.o)g(this);else if(!this.state.promise&&!this.state.error)try{e(this.state.value,t.c)}catch(o){y(o)}return(this.u??=new Set).add(t),()=>{this.u.delete(t),t.t&&(t.t.abort(),t.t=void 0),this.u.size||u(this)}};p.prototype[Symbol.toPrimitive]=function(){return this.state.value};var V=function(e,t){this.a=e,this.f=t?.equals,this.state={promise:void 0,error:void 0,value:e},this.state.value=this.l=e};V.prototype.set=function(e){let t=e instanceof Function?e(this.l):e;b(t,this.state.value,this.f)||(this.l=t,d(this))};V.prototype.m=!0;V.prototype.o=!0;V.prototype.n=!1;Object.setPrototypeOf(V.prototype,p.prototype);var c=function(e,t){this.a=e,this.f=t?.equals,this.h=t?.persist,this.state={promise:I,error:void 0,value:void 0};let o=this;this.c={get signal(){return(o.t??=k()).signal}}};c.prototype.m=!1;c.prototype.o=!1;c.prototype.n=!1;c.prototype.i=0;Object.setPrototypeOf(c.prototype,p.prototype);var m=()=>m,_=()=>{};Object.setPrototypeOf(m,new Proxy(m,{get:(e,t)=>t===Symbol.toPrimitive?_:m}));var I=Promise.resolve(),A=(e,t)=>e instanceof Function?new c(e,t):new V(e,t),D=e=>A((t,o)=>{let r,n,f=e(l=>{try{return t(l)}catch(a){if(!a)throw a;S(a)?(r??=[]).push(a):n=a}return m},o);if(n)throw n;if(r)throw Promise.all(r);return f},{equals:O}),T=(e,t)=>{let o=new WeakMap;if(t)for(let[r,n]of t)o.set(r,A(n));return(r=>{{let n=o.get(r);if(n)return n}return r.a instanceof Function?A((n,f)=>r.a(l=>{let a=o.get(l);return a||(e?a=e(l):e!==null?a=l:o.set(l,a=A(l.a))),n(a)},f)):A(r.a)})},O=(e,t)=>{if(typeof e!="object"||typeof t!="object"||!e||!t)return!1;let o=e.constructor;if(o!==t.constructor)return!1;if(o===Array){let n=e.length;if(n!==t.length)return!1;for(;(n=n-1|0)>=0;)if(!Object.is(e[n],t[n]))return!1;return!0}let r=0;for(let n in e){if(!(n in t&&Object.is(e[n],t[n])))return!1;r=r+1|0}for(let n in t)if((r=r-1|0)<0)return!1;return!0},v=!1,i=[],g=e=>{e.p||(e.p=!0,d(e))},d=e=>{e.n||(e.n=!0,i.push(e),v||(v=!0,queueMicrotask(G)))},G=()=>{v=!1;{let t=i;i=[];for(let o of t)o.state.promise=void 0,o.state.error=o.A,o.state.value=o.l,x(o)}let e=i;i=[];for(let t=e.length;t--;){let o=e[t];o.d=!1,o.p&&(o.n=!0,w(o)),o.n&&P(o)}},P=e=>{if(e.n=!1,e.r)for(let t of e.r)t.p=!0;if(e.s)for(let t of e.s)t();if(e.u&&!e.state.promise&&!e.state.error)for(let t of e.u){t.t&&(t.t.abort(),t.t=void 0);try{t.y(e.state.value,t.c)}catch(o){y(o)}}},x=e=>{if(!e.d){if(e.d=!0,e.r)for(let t of e.r)x(t);i.push(e)}},s=class{e;constructor(t){this.e=t}},w=e=>{let t=++e.i;e.o=!0,e.p=!1,e.state.promise=void 0,e.t&&(e.t.abort(),e.t=void 0);let o=e.V;o&&(e.V=new Set);try{let r=e.a((n,f=!0)=>{if(t!==e.i)throw void 0;if(e!==n&&(n.o||(w(n),n.n&&(n.n=!1,P(n))),o?.delete(n),(e.V??=new Set).add(n),(n.r??=new Set).add(e)),!f)return n.state;if(n.state.promise)throw new s(n.state.promise);if(n.state.error)throw new s(n.state.error);return n.state.value},e.c);S(r)?(e.state.promise=r,r.then(n=>{t===e.i&&(b(n,e.state.value,e.f)?e.state.promise=void 0:(e.l=n,e.A=void 0,d(e)))},n=>{t===e.i&&(n instanceof Promise?e.state.promise=void 0:(n instanceof s?n=n.e:y(n),e.A=n,d(e)))})):(++e.i,e.state.error=void 0,b(r,e.state.value,e.f)?e.n=!1:e.state.value=e.l=r)}catch(r){++e.i,r?(r instanceof s?r=r.e:y(r),S(r)?e.state.promise=r:e.state.error=r):e.n=!1}if(o)for(let r of o)r.r.delete(e),u(r)},h=!1,u=e=>{if(!e.m&&!e.h&&!e.r?.size&&!e.s?.size&&!e.u?.size){if(!h){setTimeout(()=>{h=!0,u(e),h=!1},0);return}if(e.state.promise=I,e.l=e.A=e.state.error=e.state.value=void 0,e.n=e.p=e.o=!1,e.t&&(e.t.abort(),e.t=void 0),e.V){for(let t of e.V)t.r.delete(e),u(t);e.V.clear()}}},b=(e,t,o)=>Object.is(e,t)||o!==void 0&&t!==void 0&&o(e,t),S=e=>typeof e?.then=="function",k=()=>{let e=new AbortController,t=e.signal,o=new Promise(r=>{t.then=n=>o.then(n),t.addEventListener("abort",r,{once:!0,passive:!0})});return{abort:()=>e.abort(),signal:t}},y=e=>{queueMicrotask(()=>{throw e})};export{A as $,D as $$,T as createScope,I as inactive};
package/dist/index.d.ts CHANGED
@@ -77,4 +77,6 @@ export type MapLike<Key, Value> = Map<Key, Value> | (Key extends object ? WeakMa
77
77
  export declare const inactive: Promise<void>;
78
78
  export declare const $: CreateAtom;
79
79
  export declare const $$: <Value>(init: AtomGetter<Value>) => DerivedAtom<Value>;
80
+ type AtomWithValue<Value> = [Atom<Value>, Value];
81
+ export declare const createScope: <T extends AtomWithValue<unknown>[]>(parentScope?: AtomScope | null, atomWithValues?: T) => AtomScope;
80
82
  export {};
package/dist/index.js CHANGED
@@ -13,18 +13,18 @@ AtomPrototype.prototype.watch = function(watcher) {
13
13
  if (!this.c) {
14
14
  requestActivate(this);
15
15
  }
16
- (this.h ??= /* @__PURE__ */ new Set()).add(watcher);
16
+ (this.i ??= /* @__PURE__ */ new Set()).add(watcher);
17
17
  return () => {
18
- this.h.delete(watcher);
19
- if (!this.h.size) {
18
+ this.i.delete(watcher);
19
+ if (!this.i.size) {
20
20
  disableAtom(this);
21
21
  }
22
22
  };
23
23
  };
24
24
  AtomPrototype.prototype.subscribe = function(subscriber) {
25
25
  const atomSubscriber = {
26
- q: subscriber,
27
- l: {
26
+ r: subscriber,
27
+ m: {
28
28
  get signal() {
29
29
  return (atomSubscriber.a ??= createThenableSignal()).signal;
30
30
  }
@@ -34,19 +34,19 @@ AtomPrototype.prototype.subscribe = function(subscriber) {
34
34
  requestActivate(this);
35
35
  } else if (!this.state.promise && !this.state.error) {
36
36
  try {
37
- subscriber(this.state.value, atomSubscriber.l);
37
+ subscriber(this.state.value, atomSubscriber.m);
38
38
  } catch (e) {
39
39
  logError(e);
40
40
  }
41
41
  }
42
- (this.i ??= /* @__PURE__ */ new Set()).add(atomSubscriber);
42
+ (this.j ??= /* @__PURE__ */ new Set()).add(atomSubscriber);
43
43
  return () => {
44
- this.i.delete(atomSubscriber);
44
+ this.j.delete(atomSubscriber);
45
45
  if (atomSubscriber.a) {
46
46
  atomSubscriber.a.abort();
47
47
  atomSubscriber.a = void 0;
48
48
  }
49
- if (!this.i.size) {
49
+ if (!this.j.size) {
50
50
  disableAtom(this);
51
51
  }
52
52
  };
@@ -55,22 +55,23 @@ AtomPrototype.prototype[Symbol.toPrimitive] = function() {
55
55
  return this.state.value;
56
56
  };
57
57
  const PrimitiveAtomPrototype = function(init, options) {
58
- this.m = options?.equals;
58
+ this.f = init;
59
+ this.n = options?.equals;
59
60
  this.state = {
60
61
  promise: void 0,
61
62
  error: void 0,
62
63
  value: init
63
64
  };
64
- this.state.value = this.f = init;
65
+ this.state.value = this.g = init;
65
66
  };
66
67
  PrimitiveAtomPrototype.prototype.set = function(value) {
67
- const nextValue = value instanceof Function ? value(this.f) : value;
68
- if (!equals(nextValue, this.state.value, this.m)) {
69
- this.f = nextValue;
68
+ const nextValue = value instanceof Function ? value(this.g) : value;
69
+ if (!equals(nextValue, this.state.value, this.n)) {
70
+ this.g = nextValue;
70
71
  requestPropagate(this);
71
72
  }
72
73
  };
73
- PrimitiveAtomPrototype.prototype.o = true;
74
+ PrimitiveAtomPrototype.prototype.p = true;
74
75
  PrimitiveAtomPrototype.prototype.c = true;
75
76
  PrimitiveAtomPrototype.prototype.b = false;
76
77
  Object.setPrototypeOf(
@@ -78,8 +79,8 @@ Object.setPrototypeOf(
78
79
  AtomPrototype.prototype
79
80
  );
80
81
  const DerivedAtomPrototype = function(init, options) {
81
- this.r = init;
82
- this.m = options?.equals;
82
+ this.f = init;
83
+ this.n = options?.equals;
83
84
  this.s = options?.persist;
84
85
  this.state = {
85
86
  promise: inactive,
@@ -87,16 +88,16 @@ const DerivedAtomPrototype = function(init, options) {
87
88
  value: void 0
88
89
  };
89
90
  const self = this;
90
- this.l = {
91
+ this.m = {
91
92
  get signal() {
92
93
  return (self.a ??= createThenableSignal()).signal;
93
94
  }
94
95
  };
95
96
  };
96
- DerivedAtomPrototype.prototype.o = false;
97
+ DerivedAtomPrototype.prototype.p = false;
97
98
  DerivedAtomPrototype.prototype.c = false;
98
99
  DerivedAtomPrototype.prototype.b = false;
99
- DerivedAtomPrototype.prototype.g = 0;
100
+ DerivedAtomPrototype.prototype.h = 0;
100
101
  Object.setPrototypeOf(DerivedAtomPrototype.prototype, AtomPrototype.prototype);
101
102
  const ouroboros = () => ouroboros;
102
103
  const toUndefined = () => void 0;
@@ -136,6 +137,32 @@ const $$ = (init) => $((get, options) => {
136
137
  }, {
137
138
  equals: shallowEquals
138
139
  });
140
+ const createScope = (parentScope, atomWithValues) => {
141
+ const scopeMap = /* @__PURE__ */ new WeakMap();
142
+ if (atomWithValues) {
143
+ for (const [atom, value] of atomWithValues) {
144
+ scopeMap.set(atom, $(value));
145
+ }
146
+ }
147
+ return ((baseAtom) => {
148
+ {
149
+ const scopedAtom = scopeMap.get(baseAtom);
150
+ if (scopedAtom) return scopedAtom;
151
+ }
152
+ if (baseAtom.f instanceof Function) return $(
153
+ (get, options) => baseAtom.f((atom) => {
154
+ let scopedAtom = scopeMap.get(atom);
155
+ if (!scopedAtom) {
156
+ if (parentScope) scopedAtom = parentScope(atom);
157
+ else if (parentScope !== null) scopedAtom = atom;
158
+ else scopeMap.set(atom, scopedAtom = $(atom.f));
159
+ }
160
+ return get(scopedAtom);
161
+ }, options)
162
+ );
163
+ return $(baseAtom.f);
164
+ });
165
+ };
139
166
  const shallowEquals = (a, b) => {
140
167
  if (typeof a !== "object" || typeof b !== "object" || !a || !b) return false;
141
168
  const c = a.constructor;
@@ -157,8 +184,8 @@ const shallowEquals = (a, b) => {
157
184
  let pendingUpdateAtoms = false;
158
185
  let stack = [];
159
186
  const requestActivate = (atom) => {
160
- if (!atom.j) {
161
- atom.j = true;
187
+ if (!atom.k) {
188
+ atom.k = true;
162
189
  requestPropagate(atom);
163
190
  }
164
191
  };
@@ -179,8 +206,8 @@ const updateAtoms = () => {
179
206
  stack = [];
180
207
  for (const atom of updatedAtoms) {
181
208
  atom.state.promise = void 0;
182
- atom.state.error = atom.n;
183
- atom.state.value = atom.f;
209
+ atom.state.error = atom.o;
210
+ atom.state.value = atom.g;
184
211
  mark(atom);
185
212
  }
186
213
  }
@@ -188,8 +215,8 @@ const updateAtoms = () => {
188
215
  stack = [];
189
216
  for (let i = markedAtoms.length; i--; ) {
190
217
  const atom = markedAtoms[i];
191
- atom.p = false;
192
- if (atom.j) {
218
+ atom.q = false;
219
+ if (atom.k) {
193
220
  atom.b = true;
194
221
  execute(atom);
195
222
  }
@@ -202,22 +229,22 @@ const propagate = (atom) => {
202
229
  atom.b = false;
203
230
  if (atom.d) {
204
231
  for (const child of atom.d) {
205
- child.j = true;
232
+ child.k = true;
206
233
  }
207
234
  }
208
- if (atom.h) {
209
- for (const watcher of atom.h) {
235
+ if (atom.i) {
236
+ for (const watcher of atom.i) {
210
237
  watcher();
211
238
  }
212
239
  }
213
- if (atom.i && !atom.state.promise && !atom.state.error) {
214
- for (const subscriber of atom.i) {
240
+ if (atom.j && !atom.state.promise && !atom.state.error) {
241
+ for (const subscriber of atom.j) {
215
242
  if (subscriber.a) {
216
243
  subscriber.a.abort();
217
244
  subscriber.a = void 0;
218
245
  }
219
246
  try {
220
- subscriber.q(atom.state.value, subscriber.l);
247
+ subscriber.r(atom.state.value, subscriber.m);
221
248
  } catch (e) {
222
249
  logError(e);
223
250
  }
@@ -225,8 +252,8 @@ const propagate = (atom) => {
225
252
  }
226
253
  };
227
254
  const mark = (atom) => {
228
- if (!atom.p) {
229
- atom.p = true;
255
+ if (!atom.q) {
256
+ atom.q = true;
230
257
  if (atom.d) {
231
258
  for (const child of atom.d) {
232
259
  mark(child);
@@ -242,22 +269,22 @@ class Wrapped {
242
269
  }
243
270
  }
244
271
  const execute = (atom) => {
245
- const counter = ++atom.g;
272
+ const counter = ++atom.h;
246
273
  atom.c = true;
247
- atom.j = false;
274
+ atom.k = false;
248
275
  atom.state.promise = void 0;
249
276
  if (atom.a) {
250
277
  atom.a.abort();
251
278
  atom.a = void 0;
252
279
  }
253
- const oldDependencies = atom.k;
280
+ const oldDependencies = atom.l;
254
281
  if (oldDependencies) {
255
- atom.k = /* @__PURE__ */ new Set();
282
+ atom.l = /* @__PURE__ */ new Set();
256
283
  }
257
284
  try {
258
- const value = atom.r(
285
+ const value = atom.f(
259
286
  (anotherAtom, unwrap = true) => {
260
- if (counter !== atom.g) throw void 0;
287
+ if (counter !== atom.h) throw void 0;
261
288
  if (atom !== anotherAtom) {
262
289
  if (!anotherAtom.c) {
263
290
  execute(anotherAtom);
@@ -267,7 +294,7 @@ const execute = (atom) => {
267
294
  }
268
295
  }
269
296
  oldDependencies?.delete(anotherAtom);
270
- (atom.k ??= /* @__PURE__ */ new Set()).add(anotherAtom);
297
+ (atom.l ??= /* @__PURE__ */ new Set()).add(anotherAtom);
271
298
  (anotherAtom.d ??= /* @__PURE__ */ new Set()).add(atom);
272
299
  }
273
300
  if (!unwrap) return anotherAtom.state;
@@ -277,24 +304,24 @@ const execute = (atom) => {
277
304
  throw new Wrapped(anotherAtom.state.error);
278
305
  return anotherAtom.state.value;
279
306
  },
280
- atom.l
307
+ atom.m
281
308
  );
282
309
  if (isPromiseLike(value)) {
283
310
  atom.state.promise = value;
284
311
  value.then(
285
312
  (value2) => {
286
- if (counter === atom.g) {
287
- if (equals(value2, atom.state.value, atom.m)) {
313
+ if (counter === atom.h) {
314
+ if (equals(value2, atom.state.value, atom.n)) {
288
315
  atom.state.promise = void 0;
289
316
  } else {
290
- atom.f = value2;
291
- atom.n = void 0;
317
+ atom.g = value2;
318
+ atom.o = void 0;
292
319
  requestPropagate(atom);
293
320
  }
294
321
  }
295
322
  },
296
323
  (e) => {
297
- if (counter === atom.g) {
324
+ if (counter === atom.h) {
298
325
  if (e instanceof Promise) {
299
326
  atom.state.promise = void 0;
300
327
  } else {
@@ -303,23 +330,23 @@ const execute = (atom) => {
303
330
  } else {
304
331
  logError(e);
305
332
  }
306
- atom.n = e;
333
+ atom.o = e;
307
334
  requestPropagate(atom);
308
335
  }
309
336
  }
310
337
  }
311
338
  );
312
339
  } else {
313
- ++atom.g;
340
+ ++atom.h;
314
341
  atom.state.error = void 0;
315
- if (equals(value, atom.state.value, atom.m)) {
342
+ if (equals(value, atom.state.value, atom.n)) {
316
343
  atom.b = false;
317
344
  } else {
318
- atom.state.value = atom.f = value;
345
+ atom.state.value = atom.g = value;
319
346
  }
320
347
  }
321
348
  } catch (e) {
322
- ++atom.g;
349
+ ++atom.h;
323
350
  if (!e) {
324
351
  atom.b = false;
325
352
  } else {
@@ -344,7 +371,7 @@ const execute = (atom) => {
344
371
  };
345
372
  let disabling = false;
346
373
  const disableAtom = (atom) => {
347
- if (!atom.o && !atom.s && !atom.d?.size && !atom.h?.size && !atom.i?.size) {
374
+ if (!atom.p && !atom.s && !atom.d?.size && !atom.i?.size && !atom.j?.size) {
348
375
  if (!disabling) {
349
376
  setTimeout(() => {
350
377
  disabling = true;
@@ -354,18 +381,18 @@ const disableAtom = (atom) => {
354
381
  return;
355
382
  }
356
383
  atom.state.promise = inactive;
357
- atom.f = atom.n = atom.state.error = atom.state.value = void 0;
358
- atom.b = atom.j = atom.c = false;
384
+ atom.g = atom.o = atom.state.error = atom.state.value = void 0;
385
+ atom.b = atom.k = atom.c = false;
359
386
  if (atom.a) {
360
387
  atom.a.abort();
361
388
  atom.a = void 0;
362
389
  }
363
- if (atom.k) {
364
- for (const dep of atom.k) {
390
+ if (atom.l) {
391
+ for (const dep of atom.l) {
365
392
  dep.d.delete(atom);
366
393
  disableAtom(dep);
367
394
  }
368
- atom.k.clear();
395
+ atom.l.clear();
369
396
  }
370
397
  }
371
398
  };
@@ -394,5 +421,6 @@ const logError = (e) => {
394
421
  export {
395
422
  $,
396
423
  $$,
424
+ createScope,
397
425
  inactive
398
426
  };
package/dist/react.d.ts CHANGED
@@ -1,5 +1,12 @@
1
- import type { Atom, DerivedAtom, PrimitiveAtom } from '.';
1
+ import { $ } from '.';
2
+ import type { Atom, AtomGetter, AtomOptions, AtomScope, DerivedAtom, PrimitiveAtom } from '.';
3
+ export declare const ScopeContext: import("react").Context<AtomScope>;
4
+ export declare const ScopeProvider: ({ children }: {
5
+ children: React.ReactNode;
6
+ }) => import("react/jsx-runtime").JSX.Element;
2
7
  export declare const useAtomValue: <Value>(atom: Atom<Value>) => Value;
3
8
  export declare const useAtomState: <Value>(atom: DerivedAtom<Value>) => import(".").AtomState<Value>;
4
9
  export declare const useAtom: <Value>(atom: PrimitiveAtom<Value>) => readonly [Value, (newState: Value) => void];
10
+ export declare const useLocalAtom: typeof $;
11
+ export declare const useLocalAtomValue: <Value>(init: Value | AtomGetter<Value>, options?: AtomOptions<Value>) => Value;
5
12
  export declare const useStateAtom: <Value>(atom: PrimitiveAtom<Value>) => readonly [Value, (newState: Value) => void];
package/dist/react.js CHANGED
@@ -1,9 +1,26 @@
1
- import { useState, useSyncExternalStore } from "react";
2
- const useAtomValue = (atom) => useSyncExternalStore(
3
- (watcher) => atom.watch(watcher),
4
- () => atom.get()
1
+ import { jsx } from "react/jsx-runtime";
2
+ import { createContext, use, useContext, useState, useSyncExternalStore } from "react";
3
+ import { $, createScope } from ".";
4
+ const ScopeContext = createContext(
5
+ ((x) => x)
5
6
  );
6
- const useAtomState = (atom) => useSyncExternalStore(
7
+ const ScopeProvider = ({ children }) => {
8
+ const parentScope = useContext(ScopeContext);
9
+ const scope = useState(() => createScope(parentScope))[0];
10
+ return /* @__PURE__ */ jsx(ScopeContext.Provider, { value: scope, children });
11
+ };
12
+ const useAtomValue = (atom) => (atom = useContext(ScopeContext)(atom), useSyncExternalStore(
13
+ (watcher) => atom.watch(watcher),
14
+ () => {
15
+ try {
16
+ return atom.get();
17
+ } catch (_) {
18
+ if (atom.state.promise) use(Promise.resolve(atom.state.promise));
19
+ throw atom.state.error;
20
+ }
21
+ }
22
+ ));
23
+ const useAtomState = (atom) => (atom = useContext(ScopeContext)(atom), useSyncExternalStore(
7
24
  (watcher) => atom.watch(watcher),
8
25
  () => {
9
26
  try {
@@ -12,9 +29,12 @@ const useAtomState = (atom) => useSyncExternalStore(
12
29
  }
13
30
  return atom.state;
14
31
  }
15
- );
16
- const useAtom = (atom) => [useAtomValue(atom), (newState) => atom.set(newState)];
32
+ ));
33
+ const useAtom = (atom) => (atom = useContext(ScopeContext)(atom), [useAtomValue(atom), (newState) => atom.set(newState)]);
34
+ const useLocalAtom = ((init, options) => useState(() => $(init, options))[0]);
35
+ const useLocalAtomValue = (init, options) => useAtomValue(useLocalAtom(init, options));
17
36
  const useStateAtom = (atom) => {
37
+ atom = useContext(ScopeContext)(atom);
18
38
  const [state, setState] = useState(() => atom.get());
19
39
  const setStateWithAtom = (newState) => {
20
40
  setState(newState);
@@ -23,8 +43,12 @@ const useStateAtom = (atom) => {
23
43
  return [state, setStateWithAtom];
24
44
  };
25
45
  export {
46
+ ScopeContext,
47
+ ScopeProvider,
26
48
  useAtom,
27
49
  useAtomState,
28
50
  useAtomValue,
51
+ useLocalAtom,
52
+ useLocalAtomValue,
29
53
  useStateAtom
30
54
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bansa",
3
- "version": "0.0.8",
3
+ "version": "0.0.10",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -16,16 +16,6 @@
16
16
  "default": "./dist/index.js"
17
17
  }
18
18
  },
19
- "scripts": {
20
- "build": "pnpm run \"/^build:.*/\"",
21
- "build:types": "tsc --noEmit false --emitDeclarationOnly --outDir dist",
22
- "build:index": "esbuild src/index.ts --mangle-props=^_ --format=esm --target=es2022 --outfile=dist/index.js",
23
- "build:index-browser": "esbuild src/index.ts --mangle-props=^_ --minify --bundle --format=esm --target=es2022 --platform=browser --outfile=dist/index.browser.js",
24
- "build:react": "esbuild src/react.tsx --mangle-props=^_ --format=esm --target=es2022 --outfile=dist/react.js",
25
- "test": "vitest",
26
- "format": "biome format --write ./src",
27
- "prepare": "pnpm run build"
28
- },
29
19
  "devDependencies": {
30
20
  "@biomejs/biome": "^2.2.4",
31
21
  "@types/react": "^19.1.15",
@@ -55,11 +45,19 @@
55
45
  "publishConfig": {
56
46
  "access": "public"
57
47
  },
58
- "packageManager": "pnpm@10.17.1",
59
48
  "homepage": "https://github.com/cgiosy/bansa",
60
49
  "keywords": [
61
50
  "bansa",
62
51
  "state",
63
52
  "react"
64
- ]
65
- }
53
+ ],
54
+ "scripts": {
55
+ "build": "pnpm run \"/^build:.*/\"",
56
+ "build:types": "tsc --noEmit false --emitDeclarationOnly --outDir dist",
57
+ "build:index": "esbuild src/index.ts --mangle-props=^_ --format=esm --target=es2022 --outfile=dist/index.js",
58
+ "build:index-browser": "esbuild src/index.ts --mangle-props=^_ --minify --bundle --format=esm --target=es2022 --platform=browser --outfile=dist/index.browser.js",
59
+ "build:react": "esbuild src/react.tsx --mangle-props=^_ --format=esm --target=es2022 --outfile=dist/react.js",
60
+ "test": "vitest",
61
+ "format": "biome format --write ./src"
62
+ }
63
+ }