atomaric 0.0.22 → 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 +23 -19
- package/build/atomaric.umd.cjs +1 -1
- package/package.json +1 -1
- package/types/model.d.ts +6 -3
package/build/atomaric.js
CHANGED
|
@@ -47,47 +47,51 @@ const x = (t, e, i) => {
|
|
|
47
47
|
}
|
|
48
48
|
) : t instanceof Object && (l = y(
|
|
49
49
|
{
|
|
50
|
-
setPartial: (s) => e.set((r) => ({
|
|
50
|
+
setPartial: (s) => e.set((r) => ({
|
|
51
|
+
...r,
|
|
52
|
+
...typeof s == "function" ? s(e.get()) : s
|
|
53
|
+
}))
|
|
51
54
|
}
|
|
52
55
|
));
|
|
53
56
|
const a = typeof i == "object" && i != null && "do" in i ? i.do(
|
|
54
57
|
(s, r) => e.set(s, r),
|
|
55
58
|
() => e.get(),
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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;
|
|
61
65
|
}, J = (t) => t, y = (t, e) => t;
|
|
62
66
|
class F {
|
|
63
67
|
constructor(e, i) {
|
|
64
|
-
const l = (n) => r = n, a = () => r,
|
|
65
|
-
let r = e,
|
|
66
|
-
}, 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 = () => {
|
|
67
71
|
const n = x(e, this, i);
|
|
68
|
-
return
|
|
72
|
+
return C = () => n, n;
|
|
69
73
|
};
|
|
70
74
|
const I = new Proxy(this, {
|
|
71
|
-
get: (n, o) => o === "do" ?
|
|
75
|
+
get: (n, o) => o === "do" ? C() : n[o],
|
|
72
76
|
set: P
|
|
73
77
|
}), S = (n, o) => {
|
|
74
78
|
const f = typeof n == "function" ? n(h()) : n;
|
|
75
79
|
if (!(f === h() || f === void 0 || typeof f == "number" && isNaN(f))) {
|
|
76
|
-
l(f),
|
|
80
|
+
l(f), d.forEach(s, this);
|
|
77
81
|
try {
|
|
78
82
|
E.postMessage({ key: u, value: a() });
|
|
79
83
|
} catch {
|
|
80
84
|
}
|
|
81
|
-
o !== !0 &&
|
|
85
|
+
o !== !0 && v(f);
|
|
82
86
|
}
|
|
83
87
|
};
|
|
84
|
-
if (this.set = (n, o) => S(n, o), this.get = () => h(), this.defaultValue = e, this.subscribe = (n) => (
|
|
85
|
-
|
|
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);
|
|
86
90
|
}), this.reset = () => {
|
|
87
|
-
S(e, !0),
|
|
91
|
+
S(e, !0), d.forEach(s, this);
|
|
88
92
|
}, this.setDeferred = (n, o = 500, f, T = !0) => {
|
|
89
|
-
T &&
|
|
90
|
-
S(n, f),
|
|
93
|
+
T && g === void 0 && S(n, f), clearTimeout(g), g = setTimeout(() => {
|
|
94
|
+
S(n, f), g = void 0;
|
|
91
95
|
}, o);
|
|
92
96
|
}, i == null) return I;
|
|
93
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) => {
|
|
@@ -107,7 +111,7 @@ class F {
|
|
|
107
111
|
}
|
|
108
112
|
}
|
|
109
113
|
return a();
|
|
110
|
-
},
|
|
114
|
+
}, v = (n) => {
|
|
111
115
|
if (n === e) {
|
|
112
116
|
this.reset();
|
|
113
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;
|