atomaric 0.0.27 → 0.0.28
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 +36 -32
- package/build/atomaric.umd.cjs +1 -1
- package/package.json +1 -1
- package/types/model.d.ts +2 -0
package/build/atomaric.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const P = (n, e,
|
|
1
|
+
const P = (n, e, c) => {
|
|
2
2
|
let u = null;
|
|
3
3
|
typeof n == "number" ? u = p(
|
|
4
4
|
{
|
|
@@ -21,8 +21,8 @@ const P = (n, e, r) => {
|
|
|
21
21
|
e.set(s.concat(e.get()));
|
|
22
22
|
},
|
|
23
23
|
update: (s) => {
|
|
24
|
-
const
|
|
25
|
-
s(
|
|
24
|
+
const r = e.get().slice();
|
|
25
|
+
s(r), e.set(r);
|
|
26
26
|
},
|
|
27
27
|
filter: (s) => {
|
|
28
28
|
e.set(e.get().filter(s ?? V));
|
|
@@ -34,55 +34,59 @@ const P = (n, e, r) => {
|
|
|
34
34
|
e.set(new Set(e.get()).add(s));
|
|
35
35
|
},
|
|
36
36
|
delete: (s) => {
|
|
37
|
-
const
|
|
38
|
-
|
|
37
|
+
const r = new Set(e.get());
|
|
38
|
+
r.delete(s), e.set(r);
|
|
39
|
+
},
|
|
40
|
+
toggle: (s) => {
|
|
41
|
+
const r = new Set(e.get());
|
|
42
|
+
r.has(s) ? r.delete(s) : r.add(s), e.set(r);
|
|
39
43
|
},
|
|
40
44
|
clear: () => {
|
|
41
45
|
e.set(/* @__PURE__ */ new Set());
|
|
42
46
|
},
|
|
43
47
|
update: (s) => {
|
|
44
|
-
const
|
|
45
|
-
s(
|
|
48
|
+
const r = new Set(e.get());
|
|
49
|
+
s(r), e.set(r);
|
|
46
50
|
}
|
|
47
51
|
}
|
|
48
52
|
) : n instanceof Object && (u = p(
|
|
49
53
|
{
|
|
50
|
-
setPartial: (s) => e.set((
|
|
51
|
-
...
|
|
54
|
+
setPartial: (s) => e.set((r) => ({
|
|
55
|
+
...r,
|
|
52
56
|
...typeof s == "function" ? s(e.get()) : s
|
|
53
57
|
}))
|
|
54
58
|
}
|
|
55
59
|
));
|
|
56
|
-
const f = typeof
|
|
57
|
-
(s,
|
|
60
|
+
const f = typeof c == "object" && c != null && "do" in c ? c.do(
|
|
61
|
+
(s, r) => e.set(s, r),
|
|
58
62
|
() => e.get(),
|
|
59
63
|
e,
|
|
60
|
-
(s,
|
|
64
|
+
(s, r, w) => e.setDeferred(s, r, w)
|
|
61
65
|
) : null, d = {};
|
|
62
66
|
return f && Object.keys(f).forEach((s) => Object.defineProperty(d, s, { get: () => f[s] })), u && Object.keys(u).forEach(
|
|
63
67
|
(s) => Object.defineProperty(d, s, { get: () => u[s] })
|
|
64
68
|
), d;
|
|
65
69
|
}, V = (n) => n, p = (n, e) => n;
|
|
66
70
|
class H {
|
|
67
|
-
constructor(e,
|
|
68
|
-
const u = (t) =>
|
|
69
|
-
let
|
|
70
|
-
}, S = () => f(), g = null,
|
|
71
|
-
const t = P(e, _,
|
|
72
|
-
return
|
|
71
|
+
constructor(e, c) {
|
|
72
|
+
const u = (t) => r = t, f = () => r, d = /* @__PURE__ */ new Set(), s = (t) => t(S());
|
|
73
|
+
let r = e, w, $ = () => {
|
|
74
|
+
}, S = () => f(), g = null, k = () => {
|
|
75
|
+
const t = P(e, _, c);
|
|
76
|
+
return k = () => t, t;
|
|
73
77
|
};
|
|
74
78
|
const _ = new Proxy(this, {
|
|
75
|
-
get: (t, l) => l === "do" ?
|
|
79
|
+
get: (t, l) => l === "do" ? k() : t[l],
|
|
76
80
|
set: L
|
|
77
81
|
}), b = (t, l) => {
|
|
78
82
|
const a = typeof t == "function" ? t(S()) : t;
|
|
79
83
|
if (!(a === S() || a === void 0 || typeof a == "number" && isNaN(a))) {
|
|
80
84
|
u(a), d.forEach(s, this);
|
|
81
85
|
try {
|
|
82
|
-
|
|
86
|
+
C.postMessage({ key: i, value: f() });
|
|
83
87
|
} catch {
|
|
84
88
|
}
|
|
85
|
-
l !== !0 &&
|
|
89
|
+
l !== !0 && $(a);
|
|
86
90
|
}
|
|
87
91
|
};
|
|
88
92
|
this.set = (t, l) => b(t, l), this.get = () => S(), this.initialValue = e, this.isInitialValue = () => e === f(), this.subscribe = (t) => (d.add(t), () => {
|
|
@@ -95,17 +99,17 @@ class H {
|
|
|
95
99
|
};
|
|
96
100
|
if (this.setDeferred = (t, l = 500, a, z = !0) => {
|
|
97
101
|
z && w === void 0 && b(t, a), clearTimeout(w), w = setTimeout(J, l, t, a);
|
|
98
|
-
},
|
|
102
|
+
}, c == null) return _;
|
|
99
103
|
let h = null, y = null, I = !0, x = !0, j = !1, E = e instanceof Set ? (t) => new Set(t) : (t) => t, m = e instanceof Set ? (t) => {
|
|
100
104
|
if (t instanceof Set) return Array.from(t);
|
|
101
105
|
throw console.error(t), "The value is not Set instance";
|
|
102
106
|
} : (t) => t;
|
|
103
|
-
if (typeof
|
|
104
|
-
h =
|
|
105
|
-
else if ("storeKey" in
|
|
106
|
-
I =
|
|
107
|
+
if (typeof c == "string")
|
|
108
|
+
h = c;
|
|
109
|
+
else if ("storeKey" in c)
|
|
110
|
+
I = c.warnOnDuplicateStoreKey ?? I, x = c.listenStorageChanges ?? x, h = c.storeKey, E = c.unzipValue ?? E, m = c.zipValue ?? m, j = c.unchangable ?? j, y = c.exp ?? y;
|
|
107
111
|
else return _;
|
|
108
|
-
const
|
|
112
|
+
const v = y === null || !(y() instanceof Date) ? (t) => JSON.stringify([m(t)]) : (t) => (g ?? (g = {}), g.exp = y().getTime(), JSON.stringify([m(t), g])), T = (t) => {
|
|
109
113
|
const l = JSON.parse(t);
|
|
110
114
|
return g = l[1], g != null && g.exp != null && g.exp < Date.now() ? (this.reset(), e) : E(l[0]);
|
|
111
115
|
}, i = `atom\\${h}`;
|
|
@@ -120,19 +124,19 @@ class H {
|
|
|
120
124
|
}
|
|
121
125
|
}
|
|
122
126
|
return f();
|
|
123
|
-
},
|
|
127
|
+
}, $ = (t) => {
|
|
124
128
|
if (t === e) {
|
|
125
129
|
this.reset();
|
|
126
130
|
return;
|
|
127
131
|
}
|
|
128
|
-
o[i] =
|
|
132
|
+
o[i] = v(t);
|
|
129
133
|
}, this.reset = () => {
|
|
130
134
|
delete o[i], b(e, !0);
|
|
131
135
|
}, I && A[i] !== void 0 && console.warn("Duplicate Atom key", h), x)
|
|
132
136
|
if (j) {
|
|
133
137
|
let t = !1, l;
|
|
134
138
|
D[i] = this, A[i] = () => {
|
|
135
|
-
clearTimeout(l), l = setTimeout(() => t = !1, 10), !t && (t = !0, o[i] =
|
|
139
|
+
clearTimeout(l), l = setTimeout(() => t = !1, 10), !t && (t = !0, o[i] = v(f()));
|
|
136
140
|
};
|
|
137
141
|
} else
|
|
138
142
|
A[i] = (t) => {
|
|
@@ -149,9 +153,9 @@ class H {
|
|
|
149
153
|
return _;
|
|
150
154
|
}
|
|
151
155
|
}
|
|
152
|
-
let
|
|
156
|
+
let C;
|
|
153
157
|
try {
|
|
154
|
-
|
|
158
|
+
C = new BroadcastChannel("updateHere"), C.addEventListener("message", (n) => {
|
|
155
159
|
var e;
|
|
156
160
|
(e = D[n.data.key]) == null || e.set(n.data.value, !0);
|
|
157
161
|
});
|
package/build/atomaric.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(u,y){typeof exports=="object"&&typeof module<"u"?y(exports):typeof define=="function"&&define.amd?define(["exports"],y):(u=typeof globalThis<"u"?globalThis:u||self,y(u.atomaric={}))})(this,function(u){"use strict";const y=(n,e,
|
|
1
|
+
(function(u,y){typeof exports=="object"&&typeof module<"u"?y(exports):typeof define=="function"&&define.amd?define(["exports"],y):(u=typeof globalThis<"u"?globalThis:u||self,y(u.atomaric={}))})(this,function(u){"use strict";const y=(n,e,c)=>{let i=null;typeof n=="number"?i=p({increment:s=>{e.set(+e.get()+(s??0))}}):typeof n=="boolean"?i=p({toggle:()=>{e.set(!e.get())}}):Array.isArray(n)?i=p({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??L))}}):n instanceof Set?i=p({add:s=>{e.set(new Set(e.get()).add(s))},delete:s=>{const r=new Set(e.get());r.delete(s),e.set(r)},toggle:s=>{const r=new Set(e.get());r.has(s)?r.delete(s):r.add(s),e.set(r)},clear:()=>{e.set(new Set)},update:s=>{const r=new Set(e.get());s(r),e.set(r)}}):n instanceof Object&&(i=p({setPartial:s=>e.set(r=>({...r,...typeof s=="function"?s(e.get()):s}))}));const a=typeof c=="object"&&c!=null&&"do"in c?c.do((s,r)=>e.set(s,r),()=>e.get(),e,(s,r,A)=>e.setDeferred(s,r,A)):null,g={};return a&&Object.keys(a).forEach(s=>Object.defineProperty(g,s,{get:()=>a[s]})),i&&Object.keys(i).forEach(s=>Object.defineProperty(g,s,{get:()=>i[s]})),g},L=n=>n,p=(n,e)=>n;class N{constructor(e,c){const i=t=>r=t,a=()=>r,g=new Set,s=t=>t(b());let r=e,A,x=()=>{},b=()=>a(),h=null,F=()=>{const t=y(e,I,c);return F=()=>t,t};const I=new Proxy(this,{get:(t,l)=>l==="do"?F():t[l],set:M}),m=(t,l)=>{const d=typeof t=="function"?t(b()):t;if(!(d===b()||d===void 0||typeof d=="number"&&isNaN(d))){i(d),g.forEach(s,this);try{j.postMessage({key:f,value:a()})}catch{}l!==!0&&x(d)}};this.set=(t,l)=>m(t,l),this.get=()=>b(),this.initialValue=e,this.isInitialValue=()=>e===a(),this.subscribe=t=>(g.add(t),()=>{g.delete(t)}),this.reset=()=>{m(e,!0),g.forEach(s,this)};const Z=(t,l)=>{m(t,l),A=void 0};if(this.setDeferred=(t,l=500,d,H=!0)=>{H&&A===void 0&&m(t,d),clearTimeout(A),A=setTimeout(Z,l,t,d)},c==null)return I;let S=null,_=null,E=!0,C=!0,v=!1,$=e instanceof Set?t=>new Set(t):t=>t,T=e instanceof Set?t=>{if(t instanceof Set)return Array.from(t);throw console.error(t),"The value is not Set instance"}:t=>t;if(typeof c=="string")S=c;else if("storeKey"in c)E=c.warnOnDuplicateStoreKey??E,C=c.listenStorageChanges??C,S=c.storeKey,$=c.unzipValue??$,T=c.zipValue??T,v=c.unchangable??v,_=c.exp??_;else return I;const J=_===null||!(_()instanceof Date)?t=>JSON.stringify([T(t)]):t=>(h??(h={}),h.exp=_().getTime(),JSON.stringify([T(t),h])),k=t=>{const l=JSON.parse(t);return h=l[1],h!=null&&h.exp!=null&&h.exp<Date.now()?(this.reset(),e):$(l[0])},f=`atom\\${S}`;let G=!0;if(o[`atom/${S}`]&&(this.set(k(`[${o[`atom/${S}`]}]`)),delete o[`atom/${S}`]),b=()=>{if(b=a,G){G=!1;try{i(f in o?k(o[f]):e)}catch{console.warn("Invalid json value",o[f])}}return a()},x=t=>{if(t===e){this.reset();return}o[f]=J(t)},this.reset=()=>{delete o[f],m(e,!0)},E&&w[f]!==void 0&&console.warn("Duplicate Atom key",S),C)if(v){let t=!1,l;D[f]=this,w[f]=()=>{clearTimeout(l),l=setTimeout(()=>t=!1,10),!t&&(t=!0,o[f]=J(a()))}}else w[f]=t=>{if(t.newValue===null){this.reset();return}try{m(k(t.newValue))}catch{console.warn("Invalid json value",t.newValue)}};return I}}let j;try{j=new BroadcastChannel("updateHere"),j.addEventListener("message",n=>{var e;(e=D[n.data.key])==null||e.set(n.data.value,!0)})}catch{}const o=window.localStorage,w={},D={},M=(n,e)=>{throw`${e} is readonly property`};window.addEventListener("storage",n=>{var e;n.key===null||n.newValue===n.oldValue||(e=w[n.key])==null||e.call(w,n)});const B=o.setItem.bind(o),U=o.removeItem.bind(o);o.setItem=(n,e)=>{D[n]===void 0&&B.call(o,n,e)},o.removeItem=n=>{D[n]===void 0&&U.call(o,n)};let z=()=>{throw"call configureAtomaric() before all!"};const q=n=>z=n.useSyncExternalStore,P=n=>z(n.subscribe,n.get),V=n=>n.set,Q=n=>n.setDeferred,R=n=>n.get,W=n=>n.do,X=n=>[P(n),V(n)],Y=(n,e)=>new N(n,e);u.Atom=N,u.atom=Y,u.configureAtomaric=q,u.useAtom=X,u.useAtomDo=W,u.useAtomGet=R,u.useAtomSet=V,u.useAtomSetDeferred=Q,u.useAtomValue=P,Object.defineProperty(u,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
package/types/model.d.ts
CHANGED
|
@@ -74,6 +74,8 @@ export type SetActions<Value> = UpdateAction<Value> & {
|
|
|
74
74
|
add: (value: Value) => void;
|
|
75
75
|
/** like the Set.prototype.delete() method */
|
|
76
76
|
delete: (value: Value) => void;
|
|
77
|
+
/** will add value if it doesn't exist, otherwise delete */
|
|
78
|
+
toggle: (value: Value) => void;
|
|
77
79
|
/** like the Set.prototype.clear() method */
|
|
78
80
|
clear: () => void;
|
|
79
81
|
};
|