bansa 0.0.9 → 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.
- package/dist/index.browser.js +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +86 -58
- package/dist/react.d.ts +5 -1
- package/dist/react.js +27 -8
- package/package.json +1 -1
package/dist/index.browser.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
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.
|
|
16
|
+
(this.i ??= /* @__PURE__ */ new Set()).add(watcher);
|
|
17
17
|
return () => {
|
|
18
|
-
this.
|
|
19
|
-
if (!this.
|
|
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
|
-
|
|
27
|
-
|
|
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.
|
|
37
|
+
subscriber(this.state.value, atomSubscriber.m);
|
|
38
38
|
} catch (e) {
|
|
39
39
|
logError(e);
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
|
-
(this.
|
|
42
|
+
(this.j ??= /* @__PURE__ */ new Set()).add(atomSubscriber);
|
|
43
43
|
return () => {
|
|
44
|
-
this.
|
|
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.
|
|
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.
|
|
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.
|
|
65
|
+
this.state.value = this.g = init;
|
|
65
66
|
};
|
|
66
67
|
PrimitiveAtomPrototype.prototype.set = function(value) {
|
|
67
|
-
const nextValue = value instanceof Function ? value(this.
|
|
68
|
-
if (!equals(nextValue, this.state.value, this.
|
|
69
|
-
this.
|
|
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.
|
|
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.
|
|
82
|
-
this.
|
|
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.
|
|
91
|
+
this.m = {
|
|
91
92
|
get signal() {
|
|
92
93
|
return (self.a ??= createThenableSignal()).signal;
|
|
93
94
|
}
|
|
94
95
|
};
|
|
95
96
|
};
|
|
96
|
-
DerivedAtomPrototype.prototype.
|
|
97
|
+
DerivedAtomPrototype.prototype.p = false;
|
|
97
98
|
DerivedAtomPrototype.prototype.c = false;
|
|
98
99
|
DerivedAtomPrototype.prototype.b = false;
|
|
99
|
-
DerivedAtomPrototype.prototype.
|
|
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.
|
|
161
|
-
atom.
|
|
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.
|
|
183
|
-
atom.state.value = atom.
|
|
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.
|
|
192
|
-
if (atom.
|
|
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.
|
|
232
|
+
child.k = true;
|
|
206
233
|
}
|
|
207
234
|
}
|
|
208
|
-
if (atom.
|
|
209
|
-
for (const watcher of atom.
|
|
235
|
+
if (atom.i) {
|
|
236
|
+
for (const watcher of atom.i) {
|
|
210
237
|
watcher();
|
|
211
238
|
}
|
|
212
239
|
}
|
|
213
|
-
if (atom.
|
|
214
|
-
for (const subscriber of atom.
|
|
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.
|
|
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.
|
|
229
|
-
atom.
|
|
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.
|
|
272
|
+
const counter = ++atom.h;
|
|
246
273
|
atom.c = true;
|
|
247
|
-
atom.
|
|
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.
|
|
280
|
+
const oldDependencies = atom.l;
|
|
254
281
|
if (oldDependencies) {
|
|
255
|
-
atom.
|
|
282
|
+
atom.l = /* @__PURE__ */ new Set();
|
|
256
283
|
}
|
|
257
284
|
try {
|
|
258
|
-
const value = atom.
|
|
285
|
+
const value = atom.f(
|
|
259
286
|
(anotherAtom, unwrap = true) => {
|
|
260
|
-
if (counter !== atom.
|
|
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.
|
|
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.
|
|
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.
|
|
287
|
-
if (equals(value2, atom.state.value, atom.
|
|
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.
|
|
291
|
-
atom.
|
|
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.
|
|
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.
|
|
333
|
+
atom.o = e;
|
|
307
334
|
requestPropagate(atom);
|
|
308
335
|
}
|
|
309
336
|
}
|
|
310
337
|
}
|
|
311
338
|
);
|
|
312
339
|
} else {
|
|
313
|
-
++atom.
|
|
340
|
+
++atom.h;
|
|
314
341
|
atom.state.error = void 0;
|
|
315
|
-
if (equals(value, atom.state.value, atom.
|
|
342
|
+
if (equals(value, atom.state.value, atom.n)) {
|
|
316
343
|
atom.b = false;
|
|
317
344
|
} else {
|
|
318
|
-
atom.state.value = atom.
|
|
345
|
+
atom.state.value = atom.g = value;
|
|
319
346
|
}
|
|
320
347
|
}
|
|
321
348
|
} catch (e) {
|
|
322
|
-
++atom.
|
|
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.
|
|
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.
|
|
358
|
-
atom.b = atom.
|
|
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.
|
|
364
|
-
for (const dep of atom.
|
|
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.
|
|
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,9 @@
|
|
|
1
1
|
import { $ } from '.';
|
|
2
|
-
import type { Atom, AtomGetter, AtomOptions, DerivedAtom, PrimitiveAtom } 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;
|
|
3
7
|
export declare const useAtomValue: <Value>(atom: Atom<Value>) => Value;
|
|
4
8
|
export declare const useAtomState: <Value>(atom: DerivedAtom<Value>) => import(".").AtomState<Value>;
|
|
5
9
|
export declare const useAtom: <Value>(atom: PrimitiveAtom<Value>) => readonly [Value, (newState: Value) => void];
|
package/dist/react.js
CHANGED
|
@@ -1,10 +1,26 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
() =>
|
|
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)
|
|
6
6
|
);
|
|
7
|
-
const
|
|
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(
|
|
8
24
|
(watcher) => atom.watch(watcher),
|
|
9
25
|
() => {
|
|
10
26
|
try {
|
|
@@ -13,11 +29,12 @@ const useAtomState = (atom) => useSyncExternalStore(
|
|
|
13
29
|
}
|
|
14
30
|
return atom.state;
|
|
15
31
|
}
|
|
16
|
-
);
|
|
17
|
-
const useAtom = (atom) => [useAtomValue(atom), (newState) => atom.set(newState)];
|
|
32
|
+
));
|
|
33
|
+
const useAtom = (atom) => (atom = useContext(ScopeContext)(atom), [useAtomValue(atom), (newState) => atom.set(newState)]);
|
|
18
34
|
const useLocalAtom = ((init, options) => useState(() => $(init, options))[0]);
|
|
19
35
|
const useLocalAtomValue = (init, options) => useAtomValue(useLocalAtom(init, options));
|
|
20
36
|
const useStateAtom = (atom) => {
|
|
37
|
+
atom = useContext(ScopeContext)(atom);
|
|
21
38
|
const [state, setState] = useState(() => atom.get());
|
|
22
39
|
const setStateWithAtom = (newState) => {
|
|
23
40
|
setState(newState);
|
|
@@ -26,6 +43,8 @@ const useStateAtom = (atom) => {
|
|
|
26
43
|
return [state, setStateWithAtom];
|
|
27
44
|
};
|
|
28
45
|
export {
|
|
46
|
+
ScopeContext,
|
|
47
|
+
ScopeProvider,
|
|
29
48
|
useAtom,
|
|
30
49
|
useAtomState,
|
|
31
50
|
useAtomValue,
|