atomaric 0.0.23 → 0.0.24
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/build/atomaric.js +19 -18
- package/build/atomaric.umd.cjs +1 -1
- package/package.json +1 -1
- package/types/model.d.ts +6 -3
package/build/atomaric.js
CHANGED
|
@@ -56,41 +56,42 @@ const x = (t, e, i) => {
|
|
|
56
56
|
const a = typeof i == "object" && i != null && "do" in i ? i.do(
|
|
57
57
|
(s, r) => e.set(s, r),
|
|
58
58
|
() => e.get(),
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
59
|
+
void 0,
|
|
60
|
+
(s, r, g) => e.setDeferred(s, r, g)
|
|
61
|
+
) : null, d = {};
|
|
62
|
+
return a && Object.keys(a).forEach((s) => Object.defineProperty(d, s, { get: () => a[s] })), l && Object.keys(l).forEach(
|
|
63
|
+
(s) => Object.defineProperty(d, s, { get: () => l[s] })
|
|
64
|
+
), d;
|
|
64
65
|
}, J = (t) => t, y = (t, e) => t;
|
|
65
66
|
class F {
|
|
66
67
|
constructor(e, i) {
|
|
67
|
-
const l = (n) => r = n, a = () => r,
|
|
68
|
-
let r = e,
|
|
69
|
-
}, h = () => a(),
|
|
68
|
+
const l = (n) => r = n, a = () => r, d = /* @__PURE__ */ new Set(), s = (n) => n(h());
|
|
69
|
+
let r = e, g, v = () => {
|
|
70
|
+
}, h = () => a(), C = () => {
|
|
70
71
|
const n = x(e, this, i);
|
|
71
|
-
return
|
|
72
|
+
return C = () => n, n;
|
|
72
73
|
};
|
|
73
74
|
const I = new Proxy(this, {
|
|
74
|
-
get: (n, o) => o === "do" ?
|
|
75
|
+
get: (n, o) => o === "do" ? C() : n[o],
|
|
75
76
|
set: P
|
|
76
77
|
}), S = (n, o) => {
|
|
77
78
|
const f = typeof n == "function" ? n(h()) : n;
|
|
78
79
|
if (!(f === h() || f === void 0 || typeof f == "number" && isNaN(f))) {
|
|
79
|
-
l(f),
|
|
80
|
+
l(f), d.forEach(s, this);
|
|
80
81
|
try {
|
|
81
82
|
E.postMessage({ key: u, value: a() });
|
|
82
83
|
} catch {
|
|
83
84
|
}
|
|
84
|
-
o !== !0 &&
|
|
85
|
+
o !== !0 && v(f);
|
|
85
86
|
}
|
|
86
87
|
};
|
|
87
|
-
if (this.set = (n, o) => S(n, o), this.get = () => h(), this.defaultValue = e, this.subscribe = (n) => (
|
|
88
|
-
|
|
88
|
+
if (this.set = (n, o) => S(n, o), this.get = () => h(), this.defaultValue = e, this.subscribe = (n) => (d.add(n), () => {
|
|
89
|
+
d.delete(n);
|
|
89
90
|
}), this.reset = () => {
|
|
90
|
-
S(e, !0),
|
|
91
|
+
S(e, !0), d.forEach(s, this);
|
|
91
92
|
}, this.setDeferred = (n, o = 500, f, T = !0) => {
|
|
92
|
-
T &&
|
|
93
|
-
S(n, f),
|
|
93
|
+
T && g === void 0 && S(n, f), clearTimeout(g), g = setTimeout(() => {
|
|
94
|
+
S(n, f), g = void 0;
|
|
94
95
|
}, o);
|
|
95
96
|
}, i == null) return I;
|
|
96
97
|
let b = null, m = !0, D = !0, j = !1, A = e instanceof Set ? (n) => new Set(JSON.parse(n)) : (n) => JSON.parse(n), _ = e instanceof Set ? (n) => {
|
|
@@ -110,7 +111,7 @@ class F {
|
|
|
110
111
|
}
|
|
111
112
|
}
|
|
112
113
|
return a();
|
|
113
|
-
},
|
|
114
|
+
}, v = (n) => {
|
|
114
115
|
if (n === e) {
|
|
115
116
|
this.reset();
|
|
116
117
|
return;
|
package/build/atomaric.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(o,b){typeof exports=="object"&&typeof module<"u"?b(exports):typeof define=="function"&&define.amd?define(["exports"],b):(o=typeof globalThis<"u"?globalThis:o||self,b(o.atomaric={}))})(this,function(o){"use strict";const b=(t,e,i)=>{let u=null;typeof t=="number"?u=y({increment:s=>{e.set(+e.get()+(s??0))}}):typeof t=="boolean"?u=y({toggle:()=>{e.set(!e.get())}}):Array.isArray(t)?u=y({push:(...s)=>{e.set(e.get().concat(s))},unshift:(...s)=>{e.set(s.concat(e.get()))},update:s=>{const r=e.get().slice();s(r),e.set(r)},filter:s=>{e.set(e.get().filter(s??H))}}):t instanceof Set?u=y({add:s=>{e.set(new Set(e.get()).add(s))},delete:s=>{const r=new Set(e.get());r.delete(s),e.set(r)},clear:()=>{e.set(new Set)},update:s=>{const r=new Set(e.get());s(r),e.set(r)}}):t instanceof Object&&(u=y({setPartial:s=>e.set(r=>({...r,...typeof s=="function"?s(e.get()):s}))}));const a=typeof i=="object"&&i!=null&&"do"in i?i.do((s,r)=>e.set(s,r),()=>e.get(),void 0,(s,r,S)=>e.setDeferred(s,r,S)):null,g={};return a&&Object.keys(a).forEach(s=>Object.defineProperty(g,s,{get:()=>a[s]})),u&&Object.keys(u).forEach(s=>Object.defineProperty(g,s,{get:()=>u[s]})),g},H=t=>t,y=(t,e)=>t;class C{constructor(e,i){const u=n=>r=n,a=()=>r,g=new Set,s=n=>n(w());let r=e,S,P=()=>{},w=()=>a(),F=()=>{const n=b(e,this,i);return F=()=>n,n};const p=new Proxy(this,{get:(n,l)=>l==="do"?F():n[l],set:L}),A=(n,l)=>{const d=typeof n=="function"?n(w()):n;if(!(d===w()||d===void 0||typeof d=="number"&&isNaN(d))){u(d),g.forEach(s,this);try{I.postMessage({key:f,value:a()})}catch{}l!==!0&&P(d)}};if(this.set=(n,l)=>A(n,l),this.get=()=>w(),this.defaultValue=e,this.subscribe=n=>(g.add(n),()=>{g.delete(n)}),this.reset=()=>{A(e,!0),g.forEach(s,this)},this.setDeferred=(n,l=500,d,G=!0)=>{G&&S===void 0&&A(n,d),clearTimeout(S),S=setTimeout(()=>{A(n,d),S=void 0},l)},i==null)return p;let m=null,T=!0,v=!0,E=!1,D=e instanceof Set?n=>new Set(JSON.parse(n)):n=>JSON.parse(n),j=e instanceof Set?n=>{if(n instanceof Set)return JSON.stringify(Array.from(n));throw console.error(n),"The value is not Set instance"}:n=>JSON.stringify(n);if(typeof i=="string"?m=i:"storeKey"in i&&(T=i.warnOnDuplicateStoreKey??T,v=i.listenStorageChanges??v,m=i.storeKey,D=i.parseValue??D,j=i.stringifyValue??j,E=i.unchangable??E),m===null)return p;const f=`atom/${m}`;let V=!0;if(w=()=>{if(w=()=>a(),V){V=!1;try{u(f in c?D(c[f]):e)}catch{console.warn("Invalid json value",c[f])}}return a()},P=n=>{if(n===e){this.reset();return}c[f]=j(n)},this.reset=()=>{delete c[f],A(e,!0)},T&&h[f]!==void 0&&console.warn("Duplicate Atom key",m),v)if(E){let n=!1,l;_[f]=this,h[f]=()=>{clearTimeout(l),l=setTimeout(()=>n=!1,10),!n&&(n=!0,c[f]=j(a()))}}else h[f]=n=>{if(n.newValue===null){this.reset();return}try{A(D(n.newValue))}catch{console.warn("Invalid json value",n.newValue)}};return p}}let I;try{I=new BroadcastChannel("updateHere"),I.addEventListener("message",t=>{var e;(e=_[t.data.key])==null||e.set(t.data.value,!0)})}catch{}const c=window.localStorage,h={},_={},L=(t,e)=>{throw`${e} is readonly property`};window.addEventListener("storage",t=>{var e;t.key===null||t.newValue===t.oldValue||(e=h[t.key])==null||e.call(h,t)});const M=c.setItem.bind(c),$=c.removeItem.bind(c);c.setItem=(t,e)=>{_[t]===void 0&&M.call(c,t,e)},c.removeItem=t=>{_[t]===void 0&&$.call(c,t)};let N=()=>{throw"call configureAtomaric() before all!"};const B=t=>N=t.useSyncExternalStore,k=t=>N(t.subscribe,t.get),J=t=>t.set,U=t=>t.setDeferred,q=t=>t.get,z=t=>t.do,Q=t=>[k(t),J(t)],R=(t,e)=>new C(t,e);o.Atom=C,o.atom=R,o.configureAtomaric=B,o.useAtom=Q,o.useAtomDo=z,o.useAtomGet=q,o.useAtomSet=J,o.useAtomSetDeferred=U,o.useAtomValue=k,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
package/types/model.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { useSyncExternalStore } from 'react';
|
|
2
|
+
|
|
1
3
|
type Sunscriber<Value> = (value: Value) => void;
|
|
2
4
|
|
|
3
5
|
export type AtomStoreKey = `${string}${string}:${string}${string}`;
|
|
@@ -25,6 +27,7 @@ export type AtomOptions<Value, Actions extends Record<string, Function> = {}> =
|
|
|
25
27
|
do: (
|
|
26
28
|
set: (value: Value | ((value: Value) => Value), isPreventSave?: boolean) => void,
|
|
27
29
|
get: () => Value,
|
|
30
|
+
self: Atom<Value>,
|
|
28
31
|
setDeferred: (value: Value | ((value: Value) => Value), debounceMs?: number, isPreventSave?: boolean) => void,
|
|
29
32
|
) => Actions;
|
|
30
33
|
}
|
|
@@ -52,7 +55,7 @@ export type ObjectActions<Value> = {
|
|
|
52
55
|
setPartial: (value: Partial<Value> | ((value: Value) => Partial<Value>)) => void;
|
|
53
56
|
};
|
|
54
57
|
|
|
55
|
-
export type BooleanActions
|
|
58
|
+
export type BooleanActions = {
|
|
56
59
|
toggle: () => void;
|
|
57
60
|
};
|
|
58
61
|
|
|
@@ -71,7 +74,7 @@ export type ArrayActions<Value> = UpdateAction<Value[]> & {
|
|
|
71
74
|
export type DefaultActions<Value> = Value extends Set<infer Val>
|
|
72
75
|
? SetActions<Val>
|
|
73
76
|
: Value extends boolean
|
|
74
|
-
? BooleanActions
|
|
77
|
+
? BooleanActions
|
|
75
78
|
: Value extends (infer Val)[]
|
|
76
79
|
? ArrayActions<Val>
|
|
77
80
|
: Value extends number
|
|
@@ -80,7 +83,7 @@ export type DefaultActions<Value> = Value extends Set<infer Val>
|
|
|
80
83
|
? ObjectActions<Value>
|
|
81
84
|
: {};
|
|
82
85
|
|
|
83
|
-
|
|
86
|
+
declare class Atom<Value, Actions extends Record<string, Function> = {}> {
|
|
84
87
|
constructor(defaultValue: Value, storeKeyOrOptions: StoreKeyOrOptions<Value, Actions> | undefined);
|
|
85
88
|
|
|
86
89
|
readonly defaultValue: Value;
|