atomaric 0.0.26 → 0.0.27
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 +111 -102
- package/build/atomaric.umd.cjs +1 -1
- package/package.json +1 -1
- package/types/model.d.ts +8 -4
package/build/atomaric.js
CHANGED
|
@@ -1,179 +1,188 @@
|
|
|
1
|
-
const
|
|
1
|
+
const P = (n, e, r) => {
|
|
2
2
|
let u = null;
|
|
3
|
-
typeof
|
|
3
|
+
typeof n == "number" ? u = p(
|
|
4
4
|
{
|
|
5
|
-
increment: (
|
|
6
|
-
e.set(+e.get() + (
|
|
5
|
+
increment: (s) => {
|
|
6
|
+
e.set(+e.get() + (s ?? 0));
|
|
7
7
|
}
|
|
8
8
|
}
|
|
9
|
-
) : typeof
|
|
9
|
+
) : typeof n == "boolean" ? u = p(
|
|
10
10
|
{
|
|
11
11
|
toggle: () => {
|
|
12
12
|
e.set(!e.get());
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
|
-
) : Array.isArray(
|
|
15
|
+
) : Array.isArray(n) ? u = p(
|
|
16
16
|
{
|
|
17
|
-
push: (...
|
|
18
|
-
e.set(e.get().concat(
|
|
17
|
+
push: (...s) => {
|
|
18
|
+
e.set(e.get().concat(s));
|
|
19
19
|
},
|
|
20
|
-
unshift: (...
|
|
21
|
-
e.set(
|
|
20
|
+
unshift: (...s) => {
|
|
21
|
+
e.set(s.concat(e.get()));
|
|
22
22
|
},
|
|
23
|
-
update: (
|
|
24
|
-
const
|
|
25
|
-
|
|
23
|
+
update: (s) => {
|
|
24
|
+
const c = e.get().slice();
|
|
25
|
+
s(c), e.set(c);
|
|
26
26
|
},
|
|
27
|
-
filter: (
|
|
28
|
-
e.set(e.get().filter(
|
|
27
|
+
filter: (s) => {
|
|
28
|
+
e.set(e.get().filter(s ?? V));
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
|
-
) :
|
|
31
|
+
) : n instanceof Set ? u = p(
|
|
32
32
|
{
|
|
33
|
-
add: (
|
|
34
|
-
e.set(new Set(e.get()).add(
|
|
33
|
+
add: (s) => {
|
|
34
|
+
e.set(new Set(e.get()).add(s));
|
|
35
35
|
},
|
|
36
|
-
delete: (
|
|
37
|
-
const
|
|
38
|
-
|
|
36
|
+
delete: (s) => {
|
|
37
|
+
const c = new Set(e.get());
|
|
38
|
+
c.delete(s), e.set(c);
|
|
39
39
|
},
|
|
40
40
|
clear: () => {
|
|
41
41
|
e.set(/* @__PURE__ */ new Set());
|
|
42
42
|
},
|
|
43
|
-
update: (
|
|
44
|
-
const
|
|
45
|
-
|
|
43
|
+
update: (s) => {
|
|
44
|
+
const c = new Set(e.get());
|
|
45
|
+
s(c), e.set(c);
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
|
-
) :
|
|
48
|
+
) : n instanceof Object && (u = p(
|
|
49
49
|
{
|
|
50
|
-
setPartial: (
|
|
51
|
-
...
|
|
52
|
-
...typeof
|
|
50
|
+
setPartial: (s) => e.set((c) => ({
|
|
51
|
+
...c,
|
|
52
|
+
...typeof s == "function" ? s(e.get()) : s
|
|
53
53
|
}))
|
|
54
54
|
}
|
|
55
55
|
));
|
|
56
|
-
const f = typeof
|
|
57
|
-
(
|
|
56
|
+
const f = typeof r == "object" && r != null && "do" in r ? r.do(
|
|
57
|
+
(s, c) => e.set(s, c),
|
|
58
58
|
() => e.get(),
|
|
59
59
|
e,
|
|
60
|
-
(
|
|
60
|
+
(s, c, w) => e.setDeferred(s, c, w)
|
|
61
61
|
) : null, d = {};
|
|
62
|
-
return f && Object.keys(f).forEach((
|
|
63
|
-
(
|
|
62
|
+
return f && Object.keys(f).forEach((s) => Object.defineProperty(d, s, { get: () => f[s] })), u && Object.keys(u).forEach(
|
|
63
|
+
(s) => Object.defineProperty(d, s, { get: () => u[s] })
|
|
64
64
|
), d;
|
|
65
|
-
},
|
|
66
|
-
class
|
|
67
|
-
constructor(e,
|
|
68
|
-
const u = (
|
|
69
|
-
let
|
|
70
|
-
},
|
|
71
|
-
const
|
|
72
|
-
return
|
|
65
|
+
}, V = (n) => n, p = (n, e) => n;
|
|
66
|
+
class H {
|
|
67
|
+
constructor(e, r) {
|
|
68
|
+
const u = (t) => c = t, f = () => c, d = /* @__PURE__ */ new Set(), s = (t) => t(S());
|
|
69
|
+
let c = e, w, C = () => {
|
|
70
|
+
}, S = () => f(), g = null, $ = () => {
|
|
71
|
+
const t = P(e, _, r);
|
|
72
|
+
return $ = () => t, t;
|
|
73
73
|
};
|
|
74
|
-
const
|
|
75
|
-
get: (
|
|
76
|
-
set:
|
|
77
|
-
}),
|
|
78
|
-
const a = typeof
|
|
79
|
-
if (!(a ===
|
|
80
|
-
u(a), d.forEach(
|
|
74
|
+
const _ = new Proxy(this, {
|
|
75
|
+
get: (t, l) => l === "do" ? $() : t[l],
|
|
76
|
+
set: L
|
|
77
|
+
}), b = (t, l) => {
|
|
78
|
+
const a = typeof t == "function" ? t(S()) : t;
|
|
79
|
+
if (!(a === S() || a === void 0 || typeof a == "number" && isNaN(a))) {
|
|
80
|
+
u(a), d.forEach(s, this);
|
|
81
81
|
try {
|
|
82
|
-
|
|
82
|
+
v.postMessage({ key: i, value: f() });
|
|
83
83
|
} catch {
|
|
84
84
|
}
|
|
85
85
|
l !== !0 && C(a);
|
|
86
86
|
}
|
|
87
87
|
};
|
|
88
|
-
|
|
89
|
-
d.delete(
|
|
88
|
+
this.set = (t, l) => b(t, l), this.get = () => S(), this.initialValue = e, this.isInitialValue = () => e === f(), this.subscribe = (t) => (d.add(t), () => {
|
|
89
|
+
d.delete(t);
|
|
90
90
|
}), this.reset = () => {
|
|
91
|
-
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
91
|
+
b(e, !0), d.forEach(s, this);
|
|
92
|
+
};
|
|
93
|
+
const J = (t, l) => {
|
|
94
|
+
b(t, l), w = void 0;
|
|
95
|
+
};
|
|
96
|
+
if (this.setDeferred = (t, l = 500, a, z = !0) => {
|
|
97
|
+
z && w === void 0 && b(t, a), clearTimeout(w), w = setTimeout(J, l, t, a);
|
|
98
|
+
}, r == null) return _;
|
|
99
|
+
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
|
+
if (t instanceof Set) return Array.from(t);
|
|
101
|
+
throw console.error(t), "The value is not Set instance";
|
|
102
|
+
} : (t) => t;
|
|
103
|
+
if (typeof r == "string")
|
|
104
|
+
h = r;
|
|
105
|
+
else if ("storeKey" in r)
|
|
106
|
+
I = r.warnOnDuplicateStoreKey ?? I, x = r.listenStorageChanges ?? x, h = r.storeKey, E = r.unzipValue ?? E, m = r.zipValue ?? m, j = r.unchangable ?? j, y = r.exp ?? y;
|
|
107
|
+
else return _;
|
|
108
|
+
const k = 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
|
+
const l = JSON.parse(t);
|
|
110
|
+
return g = l[1], g != null && g.exp != null && g.exp < Date.now() ? (this.reset(), e) : E(l[0]);
|
|
111
|
+
}, i = `atom\\${h}`;
|
|
103
112
|
let N = !0;
|
|
104
|
-
if (h = () => {
|
|
105
|
-
if (
|
|
113
|
+
if (o[`atom/${h}`] && (this.set(T(`[${o[`atom/${h}`]}]`)), delete o[`atom/${h}`]), S = () => {
|
|
114
|
+
if (S = f, N) {
|
|
106
115
|
N = !1;
|
|
107
116
|
try {
|
|
108
|
-
u(i in o ?
|
|
117
|
+
u(i in o ? T(o[i]) : e);
|
|
109
118
|
} catch {
|
|
110
119
|
console.warn("Invalid json value", o[i]);
|
|
111
120
|
}
|
|
112
121
|
}
|
|
113
122
|
return f();
|
|
114
|
-
}, C = (
|
|
115
|
-
if (
|
|
123
|
+
}, C = (t) => {
|
|
124
|
+
if (t === e) {
|
|
116
125
|
this.reset();
|
|
117
126
|
return;
|
|
118
127
|
}
|
|
119
|
-
o[i] =
|
|
128
|
+
o[i] = k(t);
|
|
120
129
|
}, this.reset = () => {
|
|
121
|
-
delete o[i],
|
|
122
|
-
},
|
|
123
|
-
if (
|
|
124
|
-
let
|
|
125
|
-
|
|
126
|
-
clearTimeout(l), l = setTimeout(() =>
|
|
130
|
+
delete o[i], b(e, !0);
|
|
131
|
+
}, I && A[i] !== void 0 && console.warn("Duplicate Atom key", h), x)
|
|
132
|
+
if (j) {
|
|
133
|
+
let t = !1, l;
|
|
134
|
+
D[i] = this, A[i] = () => {
|
|
135
|
+
clearTimeout(l), l = setTimeout(() => t = !1, 10), !t && (t = !0, o[i] = k(f()));
|
|
127
136
|
};
|
|
128
137
|
} else
|
|
129
|
-
|
|
130
|
-
if (
|
|
138
|
+
A[i] = (t) => {
|
|
139
|
+
if (t.newValue === null) {
|
|
131
140
|
this.reset();
|
|
132
141
|
return;
|
|
133
142
|
}
|
|
134
143
|
try {
|
|
135
|
-
|
|
144
|
+
b(T(t.newValue));
|
|
136
145
|
} catch {
|
|
137
|
-
console.warn("Invalid json value",
|
|
146
|
+
console.warn("Invalid json value", t.newValue);
|
|
138
147
|
}
|
|
139
148
|
};
|
|
140
|
-
return
|
|
149
|
+
return _;
|
|
141
150
|
}
|
|
142
151
|
}
|
|
143
|
-
let
|
|
152
|
+
let v;
|
|
144
153
|
try {
|
|
145
|
-
|
|
154
|
+
v = new BroadcastChannel("updateHere"), v.addEventListener("message", (n) => {
|
|
146
155
|
var e;
|
|
147
|
-
(e =
|
|
156
|
+
(e = D[n.data.key]) == null || e.set(n.data.value, !0);
|
|
148
157
|
});
|
|
149
158
|
} catch {
|
|
150
159
|
}
|
|
151
|
-
const o = window.localStorage,
|
|
160
|
+
const o = window.localStorage, A = {}, D = {}, L = (n, e) => {
|
|
152
161
|
throw `${e} is readonly property`;
|
|
153
162
|
};
|
|
154
|
-
window.addEventListener("storage", (
|
|
163
|
+
window.addEventListener("storage", (n) => {
|
|
155
164
|
var e;
|
|
156
|
-
|
|
165
|
+
n.key === null || n.newValue === n.oldValue || (e = A[n.key]) == null || e.call(A, n);
|
|
157
166
|
});
|
|
158
|
-
const
|
|
159
|
-
o.setItem = (
|
|
160
|
-
|
|
167
|
+
const B = o.setItem.bind(o), G = o.removeItem.bind(o);
|
|
168
|
+
o.setItem = (n, e) => {
|
|
169
|
+
D[n] === void 0 && B.call(o, n, e);
|
|
161
170
|
};
|
|
162
|
-
o.removeItem = (
|
|
163
|
-
|
|
171
|
+
o.removeItem = (n) => {
|
|
172
|
+
D[n] === void 0 && G.call(o, n);
|
|
164
173
|
};
|
|
165
|
-
let
|
|
174
|
+
let F = () => {
|
|
166
175
|
throw "call configureAtomaric() before all!";
|
|
167
176
|
};
|
|
168
|
-
const
|
|
177
|
+
const q = (n) => F = n.useSyncExternalStore, M = (n) => F(n.subscribe, n.get), U = (n) => n.set, Q = (n) => n.setDeferred, R = (n) => n.get, W = (n) => n.do, X = (n) => [M(n), U(n)], Y = (n, e) => new H(n, e);
|
|
169
178
|
export {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
+
H as Atom,
|
|
180
|
+
Y as atom,
|
|
181
|
+
q as configureAtomaric,
|
|
182
|
+
X as useAtom,
|
|
183
|
+
W as useAtomDo,
|
|
184
|
+
R as useAtomGet,
|
|
185
|
+
U as useAtomSet,
|
|
186
|
+
Q as useAtomSetDeferred,
|
|
187
|
+
M as useAtomValue
|
|
179
188
|
};
|
package/build/atomaric.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
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,o)=>{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 c=e.get().slice();s(c),e.set(c)},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 c=new Set(e.get());c.delete(s),e.set(c)},clear:()=>{e.set(new Set)},update:s=>{const c=new Set(e.get());s(c),e.set(c)}}):n instanceof Object&&(i=p({setPartial:s=>e.set(c=>({...c,...typeof s=="function"?s(e.get()):s}))}));const a=typeof o=="object"&&o!=null&&"do"in o?o.do((s,c)=>e.set(s,c),()=>e.get(),e,(s,c,A)=>e.setDeferred(s,c,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,o){const i=t=>c=t,a=()=>c,g=new Set,s=t=>t(b());let c=e,A,x=()=>{},b=()=>a(),h=null,F=()=>{const t=y(e,I,o);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)},o==null)return I;let S=null,_=null,v=!0,E=!0,C=!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 o=="string")S=o;else if("storeKey"in o)v=o.warnOnDuplicateStoreKey??v,E=o.listenStorageChanges??E,S=o.storeKey,$=o.unzipValue??$,T=o.zipValue??T,C=o.unchangable??C,_=o.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(r[`atom/${S}`]&&(this.set(k(`[${r[`atom/${S}`]}]`)),delete r[`atom/${S}`]),b=()=>{if(b=a,G){G=!1;try{i(f in r?k(r[f]):e)}catch{console.warn("Invalid json value",r[f])}}return a()},x=t=>{if(t===e){this.reset();return}r[f]=J(t)},this.reset=()=>{delete r[f],m(e,!0)},v&&w[f]!==void 0&&console.warn("Duplicate Atom key",S),E)if(C){let t=!1,l;D[f]=this,w[f]=()=>{clearTimeout(l),l=setTimeout(()=>t=!1,10),!t&&(t=!0,r[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 r=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=r.setItem.bind(r),U=r.removeItem.bind(r);r.setItem=(n,e)=>{D[n]===void 0&&B.call(r,n,e)},r.removeItem=n=>{D[n]===void 0&&U.call(r,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
|
@@ -11,12 +11,16 @@ export type AtomOptions<Value, Actions extends Record<string, Function> = {}> =
|
|
|
11
11
|
* **default: true**
|
|
12
12
|
*/
|
|
13
13
|
listenStorageChanges?: boolean;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
|
|
15
|
+
/** zip Value to stringifiable value */
|
|
16
|
+
zipValue?: (value: Value) => any;
|
|
17
|
+
/** unzip stringifiable value to Value */
|
|
18
|
+
unzipValue?: (packedValue: any) => Value;
|
|
19
|
+
|
|
18
20
|
/** make your localStorage value unchangable */
|
|
19
21
|
unchangable?: true;
|
|
22
|
+
/** return value expire Date */
|
|
23
|
+
exp?: () => Date;
|
|
20
24
|
} & (
|
|
21
25
|
| {
|
|
22
26
|
/** save in localStorage by this key */
|