bansa 0.0.23 → 0.0.25

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/atom.js DELETED
@@ -1,419 +0,0 @@
1
- class CommonAtomInternal {
2
- d;
3
- o;
4
- b;
5
- f;
6
- g;
7
- get() {
8
- if (!this.h) {
9
- execute(this);
10
- disableAtom(this);
11
- }
12
- if (this.state.error) throw this.state.error;
13
- if (this.state.promise) throw this.state.promise;
14
- return this.state.value;
15
- }
16
- watch(watcher) {
17
- if (!this.h) {
18
- requestActivate(this);
19
- }
20
- (this.f ||= /* @__PURE__ */ new Set()).add(watcher);
21
- return () => {
22
- this.f.delete(watcher);
23
- if (!this.f.size) {
24
- disableAtom(this);
25
- }
26
- };
27
- }
28
- subscribe(subscriber) {
29
- const atomSubscriber = {
30
- t: subscriber,
31
- p: {
32
- get signal() {
33
- return (atomSubscriber.a ||= createThenableSignal()).signal;
34
- }
35
- }
36
- };
37
- if (!this.h) {
38
- requestActivate(this);
39
- } else if (!this.state.error && !this.state.promise) {
40
- try {
41
- subscriber(this.state.value, atomSubscriber.p);
42
- } catch (e) {
43
- logError(e);
44
- }
45
- }
46
- (this.g ||= /* @__PURE__ */ new Set()).add(atomSubscriber);
47
- return () => {
48
- this.g.delete(atomSubscriber);
49
- if (atomSubscriber.a) {
50
- atomSubscriber.a.abort();
51
- atomSubscriber.a = void 0;
52
- }
53
- if (!this.g.size) {
54
- disableAtom(this);
55
- }
56
- };
57
- }
58
- [Symbol.toPrimitive]() {
59
- return this.state.value;
60
- }
61
- }
62
- class PrimitiveAtomInternal extends CommonAtomInternal {
63
- c = false;
64
- q = false;
65
- constructor(init, options) {
66
- super();
67
- this.l = init;
68
- this.m = options?.equals;
69
- this.d = init;
70
- this.state = {
71
- promise: void 0,
72
- error: void 0,
73
- value: init
74
- };
75
- }
76
- set(value) {
77
- const nextValue = value instanceof Function ? value(this.d) : value;
78
- if (!equals(nextValue, this.state.value, this.m)) {
79
- this.d = nextValue;
80
- requestPropagate(this);
81
- }
82
- }
83
- }
84
- PrimitiveAtomInternal.prototype.r = true;
85
- PrimitiveAtomInternal.prototype.h = true;
86
- PrimitiveAtomInternal.prototype.i = false;
87
- class DerivedAtomInternal extends CommonAtomInternal {
88
- h = false;
89
- i = false;
90
- c = false;
91
- q = false;
92
- n = 0;
93
- a;
94
- j;
95
- k;
96
- constructor(init, options) {
97
- super();
98
- this.l = init;
99
- this.m = options?.equals;
100
- this.s = !!options?.persist;
101
- const self = this;
102
- this.p = {
103
- get signal() {
104
- return (self.a ||= createThenableSignal()).signal;
105
- }
106
- };
107
- this.state = {
108
- promise: inactive,
109
- error: void 0,
110
- value: void 0
111
- };
112
- }
113
- }
114
- DerivedAtomInternal.prototype.r = false;
115
- const inactive = Promise.reject();
116
- inactive.catch(() => {
117
- });
118
- const $ = (init, options) => {
119
- if (init instanceof Function)
120
- return new DerivedAtomInternal(init, options);
121
- return new PrimitiveAtomInternal(init, options);
122
- };
123
- const isAtom = (x) => x instanceof CommonAtomInternal;
124
- const isPrimitiveAtom = (x) => x instanceof PrimitiveAtomInternal;
125
- const createScope = (parentScope, atomValuePairs) => {
126
- const scopeMap = /* @__PURE__ */ new WeakMap();
127
- const atomMap = parentScope ? /* @__PURE__ */ new WeakMap() : scopeMap;
128
- const scope = ((baseAtom, strict = false) => {
129
- let scopedAtom = scopeMap.get(baseAtom);
130
- if (!strict) scopedAtom ||= atomMap.get(baseAtom);
131
- if (!scopedAtom) {
132
- const parentAtom = parentScope?.(baseAtom, true);
133
- if (strict) return parentAtom;
134
- const realBaseAtom = parentAtom || baseAtom;
135
- atomMap.set(
136
- baseAtom,
137
- scopedAtom = realBaseAtom.l instanceof Function ? $((get, options) => realBaseAtom.l(
138
- (atom, unwrap) => get(scope(atom), unwrap),
139
- options
140
- ), {
141
- equals: realBaseAtom.m,
142
- persist: realBaseAtom.s
143
- }) : parentAtom || $(realBaseAtom.l)
144
- );
145
- }
146
- return scopedAtom;
147
- });
148
- if (atomValuePairs) {
149
- for (const [atom, value] of atomValuePairs) {
150
- scopeMap.set(
151
- atom,
152
- isAtom(value) ? (parentScope || scope)(value) : $(value)
153
- );
154
- }
155
- }
156
- return scope;
157
- };
158
- let pendingUpdateAtoms = false;
159
- let updateQueue = [];
160
- let stack = [];
161
- const requestActivate = (atom) => {
162
- if (!atom.i) {
163
- atom.i = true;
164
- requestPropagate(atom);
165
- }
166
- };
167
- const requestPropagate = (atom) => {
168
- if (!atom.c) {
169
- atom.c = true;
170
- updateQueue.push(atom);
171
- if (!pendingUpdateAtoms) {
172
- pendingUpdateAtoms = true;
173
- queueMicrotask(updateAtoms);
174
- }
175
- }
176
- };
177
- const updateAtoms = () => {
178
- pendingUpdateAtoms = false;
179
- {
180
- const updatedAtoms = updateQueue;
181
- updateQueue = [];
182
- for (const atom of updatedAtoms) {
183
- atom.state.promise = void 0;
184
- atom.state.error = atom.o;
185
- atom.state.value = atom.d;
186
- mark(atom);
187
- }
188
- }
189
- const markedAtoms = stack;
190
- stack = [];
191
- for (let i = markedAtoms.length; i--; ) {
192
- const atom = markedAtoms[i];
193
- atom.q = false;
194
- if (atom.i) {
195
- atom.c = true;
196
- execute(atom);
197
- }
198
- if (atom.c) {
199
- propagate(atom);
200
- }
201
- }
202
- };
203
- const propagate = (atom) => {
204
- atom.c = false;
205
- if (atom.f) {
206
- for (const watcher of atom.f) {
207
- try {
208
- watcher();
209
- } catch (e) {
210
- logError(e);
211
- }
212
- }
213
- }
214
- if (!atom.state.error && !atom.state.promise) {
215
- if (atom.g) {
216
- for (const subscriber of atom.g) {
217
- if (subscriber.a) {
218
- subscriber.a.abort();
219
- subscriber.a = void 0;
220
- }
221
- try {
222
- subscriber.t(atom.state.value, subscriber.p);
223
- } catch (e) {
224
- logError(e);
225
- }
226
- }
227
- }
228
- if (atom.b) {
229
- for (const child of atom.b) {
230
- child.i = true;
231
- }
232
- }
233
- }
234
- };
235
- const mark = (atom) => {
236
- if (!atom.q) {
237
- atom.q = true;
238
- if (atom.b) {
239
- for (const child of atom.b) {
240
- mark(child);
241
- }
242
- }
243
- stack.push(atom);
244
- }
245
- };
246
- class Wrapped {
247
- e;
248
- constructor(e) {
249
- this.e = e;
250
- }
251
- }
252
- const expired = Symbol();
253
- const execute = (atom) => {
254
- const counter = ++atom.n;
255
- atom.h = true;
256
- atom.i = false;
257
- atom.state.promise = void 0;
258
- if (atom.a) {
259
- atom.a.abort();
260
- atom.a = void 0;
261
- }
262
- try {
263
- const value = atom.l(
264
- (anotherAtom, unwrap = true) => {
265
- if (counter !== atom.n) throw expired;
266
- if (atom !== anotherAtom) {
267
- if (!anotherAtom.h) {
268
- execute(anotherAtom);
269
- if (anotherAtom.c) {
270
- propagate(anotherAtom);
271
- }
272
- }
273
- (atom.k ||= /* @__PURE__ */ new Set()).add(anotherAtom);
274
- (anotherAtom.b ||= /* @__PURE__ */ new Set()).add(atom);
275
- }
276
- if (!unwrap) return anotherAtom.state;
277
- if (anotherAtom.state.error)
278
- throw new Wrapped(anotherAtom.state.error);
279
- if (anotherAtom.state.promise)
280
- throw new Wrapped(anotherAtom.state.promise);
281
- return anotherAtom.state.value;
282
- },
283
- atom.p
284
- );
285
- if (isPromiseLike(value)) {
286
- atom.state.promise = value;
287
- value.then(
288
- (value2) => {
289
- if (counter === atom.n) {
290
- finalizeExecution(atom);
291
- if (equals(value2, atom.state.value, atom.m)) {
292
- atom.state.promise = void 0;
293
- } else {
294
- atom.d = value2;
295
- atom.o = void 0;
296
- }
297
- requestPropagate(atom);
298
- }
299
- },
300
- (e) => {
301
- if (counter === atom.n) {
302
- finalizeExecution(atom);
303
- if (e instanceof Wrapped) {
304
- e = e.e;
305
- } else {
306
- logError(e);
307
- }
308
- atom.o = e;
309
- requestPropagate(atom);
310
- }
311
- }
312
- );
313
- } else {
314
- finalizeExecution(atom);
315
- atom.state.error = void 0;
316
- if (equals(value, atom.state.value, atom.m)) {
317
- atom.c = false;
318
- } else {
319
- atom.state.value = atom.d = value;
320
- }
321
- }
322
- } catch (e) {
323
- finalizeExecution(atom);
324
- if (e === expired) {
325
- atom.c = false;
326
- } else {
327
- if (e instanceof Wrapped) {
328
- e = e.e;
329
- } else {
330
- logError(e);
331
- }
332
- atom.state.error = e;
333
- }
334
- }
335
- };
336
- const finalizeExecution = (atom) => {
337
- ++atom.n;
338
- const oldDependencies = atom.j;
339
- atom.j = atom.k;
340
- if (oldDependencies) {
341
- for (const dep of oldDependencies) {
342
- if (!atom.j?.has(dep)) {
343
- dep.b.delete(atom);
344
- disableAtom(dep);
345
- }
346
- }
347
- oldDependencies.clear();
348
- }
349
- atom.k = oldDependencies;
350
- };
351
- let runningGc = false;
352
- let gcCandidates = /* @__PURE__ */ new Set();
353
- const disableAtom = (atom) => {
354
- if (!atom.r && !atom.s && !atom.b?.size && !atom.f?.size && !atom.g?.size) {
355
- gcCandidates.add(atom);
356
- if (!runningGc) {
357
- runningGc = true;
358
- setTimeout(gc, 0);
359
- }
360
- }
361
- };
362
- const gc = () => {
363
- for (const atom of gcCandidates) {
364
- if (!atom.r && !atom.s && !atom.b?.size && !atom.f?.size && !atom.g?.size) {
365
- atom.state.promise = inactive;
366
- atom.d = atom.o = atom.state.error = atom.state.value = void 0;
367
- atom.c = atom.i = atom.h = false;
368
- if (atom.a) {
369
- atom.a.abort();
370
- atom.a = void 0;
371
- }
372
- if (atom.j) {
373
- for (const dep of atom.j) {
374
- dep.b.delete(atom);
375
- disableAtom(dep);
376
- }
377
- atom.j.clear();
378
- if (atom.k) {
379
- for (const dep of atom.k) {
380
- dep.b.delete(atom);
381
- disableAtom(dep);
382
- }
383
- atom.k.clear();
384
- }
385
- }
386
- }
387
- }
388
- gcCandidates.clear();
389
- runningGc = false;
390
- };
391
- const equals = (value, prevValue, equalsFn) => Object.is(value, prevValue) || equalsFn !== void 0 && prevValue !== void 0 && equalsFn(value, prevValue);
392
- const isPromiseLike = (x) => typeof x?.then === "function";
393
- const createThenableSignal = () => {
394
- const ctrl = new AbortController();
395
- const signal = ctrl.signal;
396
- const promise = new Promise((resolve) => {
397
- signal.then = (f) => promise.then(f);
398
- signal.addEventListener("abort", resolve, {
399
- once: true,
400
- passive: true
401
- });
402
- });
403
- return {
404
- abort: () => ctrl.abort(),
405
- signal
406
- };
407
- };
408
- const logError = (e) => {
409
- queueMicrotask(() => {
410
- throw e;
411
- });
412
- };
413
- export {
414
- $,
415
- createScope,
416
- inactive,
417
- isAtom,
418
- isPrimitiveAtom
419
- };
@@ -1 +0,0 @@
1
- var p=class{a;m;n;r;s;get(){if(this.l||(P(this),d(this)),this.state.error)throw this.state.error;if(this.state.promise)throw this.state.promise;return this.state.value}watch(t){return this.l||O(this),(this.r||=new Set).add(t),()=>{this.r.delete(t),this.r.size||d(this)}}subscribe(t){let n={v:t,A:{get signal(){return(n.t||=j()).signal}}};if(!this.l)O(this);else if(!this.state.error&&!this.state.promise)try{t(this.state.value,n.A)}catch(o){m(o)}return(this.s||=new Set).add(n),()=>{this.s.delete(n),n.t&&(n.t.abort(),n.t=void 0),this.s.size||d(this)}}[Symbol.toPrimitive](){return this.state.value}},i=class extends p{o=!1;V=!1;constructor(t,n){super(),this.d=t,this.f=n?.equals,this.a=t,this.state={promise:void 0,error:void 0,value:t}}set(t){let n=t instanceof Function?t(this.a):t;g(n,this.state.value,this.f)||(this.a=n,b(this))}};i.prototype.y=!0;i.prototype.l=!0;i.prototype.i=!1;var y=class extends p{l=!1;i=!1;o=!1;V=!1;p=0;t;u;c;constructor(t,n){super(),this.d=t,this.f=n?.equals,this.b=!!n?.persist;let o=this;this.A={get signal(){return(o.t||=j()).signal}},this.state={promise:I,error:void 0,value:void 0}}};y.prototype.y=!1;var I=Promise.reject();I.catch(()=>{});var c=(e,t)=>e instanceof Function?new y(e,t):new i(e,t),v=e=>e instanceof p,_=e=>e instanceof i,H=(e,t)=>{let n=new WeakMap,o=e?new WeakMap:n,a=((r,l=!1)=>{let s=n.get(r);if(l||(s||=o.get(r)),!s){let h=e?.(r,!0);if(l)return h;let f=h||r;o.set(r,s=f.d instanceof Function?c((K,z)=>f.d((E,L)=>K(a(E),L),z),{equals:f.f,persist:f.b}):h||c(f.d))}return s});if(t)for(let[r,l]of t)n.set(r,v(l)?(e||a)(l):c(l));return a},S=!1,x=[],w=[],O=e=>{e.i||(e.i=!0,b(e))},b=e=>{e.o||(e.o=!0,x.push(e),S||(S=!0,queueMicrotask(M)))},M=()=>{S=!1;{let t=x;x=[];for(let n of t)n.state.promise=void 0,n.state.error=n.m,n.state.value=n.a,q(n)}let e=w;w=[];for(let t=e.length;t--;){let n=e[t];n.V=!1,n.i&&(n.o=!0,P(n)),n.o&&D(n)}},D=e=>{if(e.o=!1,e.r)for(let t of e.r)try{t()}catch(n){m(n)}if(!e.state.error&&!e.state.promise){if(e.s)for(let t of e.s){t.t&&(t.t.abort(),t.t=void 0);try{t.v(e.state.value,t.A)}catch(n){m(n)}}if(e.n)for(let t of e.n)t.i=!0}},q=e=>{if(!e.V){if(e.V=!0,e.n)for(let t of e.n)q(t);w.push(e)}},u=class{e;constructor(t){this.e=t}},G=Symbol(),P=e=>{let t=++e.p;e.l=!0,e.i=!1,e.state.promise=void 0,e.t&&(e.t.abort(),e.t=void 0);try{let n=e.d((o,a=!0)=>{if(t!==e.p)throw G;if(e!==o&&(o.l||(P(o),o.o&&D(o)),(e.c||=new Set).add(o),(o.n||=new Set).add(e)),!a)return o.state;if(o.state.error)throw new u(o.state.error);if(o.state.promise)throw new u(o.state.promise);return o.state.value},e.A);W(n)?(e.state.promise=n,n.then(o=>{t===e.p&&(V(e),g(o,e.state.value,e.f)?e.state.promise=void 0:(e.a=o,e.m=void 0),b(e))},o=>{t===e.p&&(V(e),o instanceof u?o=o.e:m(o),e.m=o,b(e))})):(V(e),e.state.error=void 0,g(n,e.state.value,e.f)?e.o=!1:e.state.value=e.a=n)}catch(n){V(e),n===G?e.o=!1:(n instanceof u?n=n.e:m(n),e.state.error=n)}},V=e=>{++e.p;let t=e.u;if(e.u=e.c,t){for(let n of t)e.u?.has(n)||(n.n.delete(e),d(n));t.clear()}e.c=t},k=!1,T=new Set,d=e=>{!e.y&&!e.b&&!e.n?.size&&!e.r?.size&&!e.s?.size&&(T.add(e),k||(k=!0,setTimeout(U,0)))},U=()=>{for(let e of T)if(!e.y&&!e.b&&!e.n?.size&&!e.r?.size&&!e.s?.size&&(e.state.promise=I,e.a=e.m=e.state.error=e.state.value=void 0,e.o=e.i=e.l=!1,e.t&&(e.t.abort(),e.t=void 0),e.u)){for(let t of e.u)t.n.delete(e),d(t);if(e.u.clear(),e.c){for(let t of e.c)t.n.delete(e),d(t);e.c.clear()}}T.clear(),k=!1},g=(e,t,n)=>Object.is(e,t)||n!==void 0&&t!==void 0&&n(e,t),W=e=>typeof e?.then=="function",j=()=>{let e=new AbortController,t=e.signal,n=new Promise(o=>{t.then=a=>n.then(a),t.addEventListener("abort",o,{once:!0,passive:!0})});return{abort:()=>e.abort(),signal:t}},m=e=>{queueMicrotask(()=>{throw e})};var A=()=>A,R=()=>{};Object.setPrototypeOf(A,new Proxy(A,{get:(e,t)=>t===Symbol.toPrimitive?R:A}));var C=e=>{if(typeof e!="object"||e===null)return c(e);if(Array.isArray(e))return e.map(C);let t=Object.create(null);for(let n in e)t[n]=C(e[n]);return t},$=e=>e.get(),F=(e,t=$)=>{let n=o=>{if(typeof o!="object"||o===null)return o;if(v(o))return t(o);if(Array.isArray(o))return o.map(n);let a=Object.create(null);for(let r in o)a[r]=n(o[r]);return a};return n(e)},X=(e,t)=>{let n=(o,a)=>{if(typeof o=="object"&&o!==null)if(v(o))_(o)&&o.set(a);else for(let r in a)n(o[r],a[r])};n(e,t)},B=e=>e instanceof Function?c((t,n)=>{let o,a,r=e(l=>{let s=t(l,!1);if(s.error)a=s.error;else if(s.promise)(o||=[]).push(s.promise);else return s.value;return A},n);if(a)throw a;if(o)throw Promise.all(o);return r},{equals:Q}):B(t=>F(e,t)),Q=(e,t)=>{if(typeof e!="object"||typeof t!="object"||!e||!t)return!1;let n=e.constructor;if(n!==t.constructor)return!1;if(n===Array){let a=e.length;if(a!==t.length)return!1;for(;(a=a-1|0)>=0;)if(!Object.is(e[a],t[a]))return!1;return!0}let o=0;for(let a in e){if(!(a in t&&Object.is(e[a],t[a])))return!1;o=o+1|0}for(let a in t)if((o=o-1|0)<0)return!1;return!0};export{c as $,B as $$,C as atomize,F as collectAtoms,H as createScope,I as inactive,v as isAtom,_ as isPrimitiveAtom,X as setAtoms};
package/dist/index.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from './atom';
2
- export * from './utils';
package/dist/index.js DELETED
@@ -1,2 +0,0 @@
1
- export * from "./atom";
2
- export * from "./utils";
package/dist/react.d.ts DELETED
@@ -1,15 +0,0 @@
1
- import type { Atom, AtomScope, AtomValuePair, DerivedAtom, PrimitiveAtom } from '.';
2
- export declare const ScopeContext: import("react").Context<AtomScope>;
3
- export declare const ScopeProvider: ({ value, children }: {
4
- value?: AtomValuePair<unknown>[];
5
- children: React.ReactNode;
6
- }) => import("react/jsx-runtime").JSX.Element;
7
- export declare const useAtomValue: <Value>(atom: Atom<Value>) => Value;
8
- export declare const useAtomState: <Value>(atom: DerivedAtom<Value>) => import("./atom").AtomState<Value>;
9
- export declare const useScopedAtom: UseScopedAtom;
10
- export declare const useAtom: <Value>(atom: PrimitiveAtom<Value>) => readonly [Value, (newState: Value) => void];
11
- export type UseScopedAtom = {
12
- <Value>(baseAtom: PrimitiveAtom<Value>): PrimitiveAtom<Value>;
13
- <Value>(baseAtom: DerivedAtom<Value>): DerivedAtom<Value>;
14
- <Value>(baseAtom: Atom<Value>): Atom<Value>;
15
- };
package/dist/react.js DELETED
@@ -1,40 +0,0 @@
1
- import { jsx } from "react/jsx-runtime";
2
- import { createContext, use, useContext, useMemo, useSyncExternalStore } from "react";
3
- import { createScope } from ".";
4
- const ScopeContext = createContext((x) => x);
5
- const ScopeProvider = ({ value, children }) => {
6
- const parentScope = value && useContext(ScopeContext);
7
- const scope = useMemo(() => createScope(parentScope, value), [parentScope]);
8
- return /* @__PURE__ */ jsx(ScopeContext.Provider, { value: scope, children });
9
- };
10
- const useAtomValue = (atom) => (atom = useContext(ScopeContext)(atom), useSyncExternalStore(
11
- (watcher) => atom.watch(watcher),
12
- () => {
13
- try {
14
- return atom.get();
15
- } catch (_) {
16
- if (atom.state.promise) use(Promise.resolve(atom.state.promise));
17
- throw atom.state.error;
18
- }
19
- }
20
- ));
21
- const useAtomState = (atom) => (atom = useContext(ScopeContext)(atom), useSyncExternalStore(
22
- (watcher) => atom.watch(watcher),
23
- () => {
24
- try {
25
- atom.get();
26
- } catch (_) {
27
- }
28
- return atom.state;
29
- }
30
- ));
31
- const useScopedAtom = ((atom) => useContext(ScopeContext)(atom));
32
- const useAtom = (atom) => (atom = useScopedAtom(atom), [useAtomValue(atom), (newState) => atom.set(newState)]);
33
- export {
34
- ScopeContext,
35
- ScopeProvider,
36
- useAtom,
37
- useAtomState,
38
- useAtomValue,
39
- useScopedAtom
40
- };
package/dist/utils.d.ts DELETED
@@ -1,19 +0,0 @@
1
- import { type PrimitiveAtom, type Atom, type AtomGetter, type DerivedAtom } from "./atom";
2
- export type Atomized<T> = T extends object ? {
3
- [K in keyof T]: Atomized<T[K]>;
4
- } : PrimitiveAtom<T>;
5
- export type CollectedAtoms<T> = T extends Atom<infer U> ? U : T extends object ? {
6
- [K in keyof T]: CollectedAtoms<T[K]>;
7
- } : T;
8
- type CollectAtom = {
9
- <Value>(init: AtomGetter<Value>): DerivedAtom<Value>;
10
- <Value>(init: Value): DerivedAtom<CollectedAtoms<Value>>;
11
- };
12
- export type RecursiveOptional<T> = T | (T extends object ? {
13
- [P in keyof T]: RecursiveOptional<T[P]>;
14
- } : never);
15
- export declare const atomize: <T>(tree: T) => Atomized<T>;
16
- export declare const collectAtoms: <T>(tree: T, get?: <T_1>(atom: Atom<T_1>) => T_1) => CollectedAtoms<T>;
17
- export declare const setAtoms: <T>(tree: T, values: RecursiveOptional<CollectedAtoms<T>>) => void;
18
- export declare const $$: CollectAtom;
19
- export {};
package/dist/utils.js DELETED
@@ -1,80 +0,0 @@
1
- import { $, isAtom, isPrimitiveAtom } from "./atom";
2
- const ouroboros = () => ouroboros;
3
- const toUndefined = () => void 0;
4
- Object.setPrototypeOf(
5
- ouroboros,
6
- new Proxy(ouroboros, {
7
- get: (_, k) => k === Symbol.toPrimitive ? toUndefined : ouroboros
8
- })
9
- );
10
- const atomize = (tree) => {
11
- if (typeof tree !== "object" || tree === null) return $(tree);
12
- if (Array.isArray(tree)) return tree.map(atomize);
13
- const result = /* @__PURE__ */ Object.create(null);
14
- for (const k in tree) result[k] = atomize(tree[k]);
15
- return result;
16
- };
17
- const getAtom = (atom) => atom.get();
18
- const collectAtoms = (tree, get = getAtom) => {
19
- const recurse = (t) => {
20
- if (typeof t !== "object" || t === null) return t;
21
- if (isAtom(t)) return get(t);
22
- if (Array.isArray(t)) return t.map(recurse);
23
- const result = /* @__PURE__ */ Object.create(null);
24
- for (const k in t) result[k] = recurse(t[k]);
25
- return result;
26
- };
27
- return recurse(tree);
28
- };
29
- const setAtoms = (tree, values) => {
30
- const recurse = (t, v) => {
31
- if (typeof t === "object" && t !== null) {
32
- if (isAtom(t)) {
33
- if (isPrimitiveAtom(t)) t.set(v);
34
- } else {
35
- for (const k in v) recurse(t[k], v[k]);
36
- }
37
- }
38
- };
39
- recurse(tree, values);
40
- };
41
- const $$ = (init) => init instanceof Function ? $((get, options) => {
42
- let promises;
43
- let error;
44
- const result = init((atom) => {
45
- const state = get(atom, false);
46
- if (state.error) error = state.error;
47
- else if (state.promise) (promises ||= []).push(state.promise);
48
- else return state.value;
49
- return ouroboros;
50
- }, options);
51
- if (error) throw error;
52
- if (promises) throw Promise.all(promises);
53
- return result;
54
- }, {
55
- equals: shallowEquals
56
- }) : $$((get) => collectAtoms(init, get));
57
- const shallowEquals = (a, b) => {
58
- if (typeof a !== "object" || typeof b !== "object" || !a || !b) return false;
59
- const c = a.constructor;
60
- if (c !== b.constructor) return false;
61
- if (c === Array) {
62
- let i = a.length;
63
- if (i !== b.length) return false;
64
- while ((i = i - 1 | 0) >= 0) if (!Object.is(a[i], b[i])) return false;
65
- return true;
66
- }
67
- let n = 0;
68
- for (const k in a) {
69
- if (!(k in b && Object.is(a[k], b[k]))) return false;
70
- n = n + 1 | 0;
71
- }
72
- for (const _ in b) if ((n = n - 1 | 0) < 0) return false;
73
- return true;
74
- };
75
- export {
76
- $$,
77
- atomize,
78
- collectAtoms,
79
- setAtoms
80
- };