bansa 0.0.18 → 0.0.19
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.d.ts +1 -0
- package/dist/atom.js +3 -1
- package/dist/index.browser.js +1 -1
- package/dist/utils.d.ts +6 -2
- package/dist/utils.js +15 -2
- package/package.json +1 -1
package/dist/atom.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 isAtom: (x: unknown) => x is Atom<unknown>;
|
|
77
|
+
export declare const isPrimitiveAtom: (x: unknown) => x is PrimitiveAtom<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/atom.js
CHANGED
|
@@ -121,6 +121,7 @@ const $ = (init, options) => {
|
|
|
121
121
|
return new PrimitiveAtomInternal(init, options);
|
|
122
122
|
};
|
|
123
123
|
const isAtom = (x) => x instanceof CommonAtomInternal;
|
|
124
|
+
const isPrimitiveAtom = (x) => x instanceof PrimitiveAtomInternal;
|
|
124
125
|
const createScope = (parentScope, atomValuePairs) => {
|
|
125
126
|
const scopeMap = /* @__PURE__ */ new WeakMap();
|
|
126
127
|
const atomMap = /* @__PURE__ */ new WeakMap();
|
|
@@ -410,5 +411,6 @@ export {
|
|
|
410
411
|
$,
|
|
411
412
|
createScope,
|
|
412
413
|
inactive,
|
|
413
|
-
isAtom
|
|
414
|
+
isAtom,
|
|
415
|
+
isPrimitiveAtom
|
|
414
416
|
};
|
package/dist/index.browser.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
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=new WeakMap,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)?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/utils.d.ts
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
import { type PrimitiveAtom, type Atom, type AtomGetter, type DerivedAtom } from "./atom";
|
|
2
|
-
type Atomized<T> = T extends object ? {
|
|
2
|
+
export type Atomized<T> = T extends object ? {
|
|
3
3
|
[K in keyof T]: Atomized<T[K]>;
|
|
4
4
|
} : PrimitiveAtom<T>;
|
|
5
|
-
type CollectedAtoms<T> = T extends Atom<infer U> ? U : T extends object ? {
|
|
5
|
+
export type CollectedAtoms<T> = T extends Atom<infer U> ? U : T extends object ? {
|
|
6
6
|
[K in keyof T]: CollectedAtoms<T[K]>;
|
|
7
7
|
} : T;
|
|
8
8
|
type CollectAtom = {
|
|
9
9
|
<Value>(init: AtomGetter<Value>): DerivedAtom<Value>;
|
|
10
10
|
<Value>(init: Value): DerivedAtom<CollectedAtoms<Value>>;
|
|
11
11
|
};
|
|
12
|
+
export type RecursiveOptional<T> = T | (T extends object ? {
|
|
13
|
+
[P in keyof T]: RecursiveOptional<T[P]>;
|
|
14
|
+
} : never);
|
|
12
15
|
export declare const atomize: <T>(tree: T) => Atomized<T>;
|
|
13
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;
|
|
14
18
|
export declare const $$: CollectAtom;
|
|
15
19
|
export {};
|
package/dist/utils.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { $, isAtom } from "./atom";
|
|
1
|
+
import { $, isAtom, isPrimitiveAtom } from "./atom";
|
|
2
2
|
const ouroboros = () => ouroboros;
|
|
3
3
|
const toUndefined = () => void 0;
|
|
4
4
|
Object.setPrototypeOf(
|
|
@@ -26,6 +26,18 @@ const collectAtoms = (tree, get = getAtom) => {
|
|
|
26
26
|
};
|
|
27
27
|
return recurse(tree);
|
|
28
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
|
+
};
|
|
29
41
|
const $$ = (init) => init instanceof Function ? $((get, options) => {
|
|
30
42
|
let promises;
|
|
31
43
|
let error;
|
|
@@ -63,5 +75,6 @@ const shallowEquals = (a, b) => {
|
|
|
63
75
|
export {
|
|
64
76
|
$$,
|
|
65
77
|
atomize,
|
|
66
|
-
collectAtoms
|
|
78
|
+
collectAtoms,
|
|
79
|
+
setAtoms
|
|
67
80
|
};
|