atomaric 0.0.38 → 0.0.40
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 +128 -119
- package/build/atomaric.umd.cjs +1 -1
- package/package.json +1 -1
- package/types/index.d.ts +4 -0
package/build/atomaric.js
CHANGED
|
@@ -3,213 +3,222 @@ let R = () => {
|
|
|
3
3
|
};
|
|
4
4
|
const V = {}, K = (e) => {
|
|
5
5
|
V.useSyncExternalStore = R = e.useSyncExternalStore, V.keyPathSeparator = e.keyPathSeparator;
|
|
6
|
-
}, U = (e) => R(e.subscribe, e.get), W = (e) => e.set, ee = (e) => e.setDeferred, te = (e) => e.get,
|
|
7
|
-
const
|
|
8
|
-
get: (t,
|
|
9
|
-
const
|
|
10
|
-
return typeof
|
|
11
|
-
Array.isArray(
|
|
12
|
-
g.concat(Array.isArray(t) ? +
|
|
6
|
+
}, U = (e) => R(e.subscribe, e.get), W = (e) => e.set, ee = (e) => e.setDeferred, te = (e) => e.get, ne = (e) => e.do, re = (e) => [U(e), W(e)], se = (e, s) => new Y(e, s), q = (e, s) => {
|
|
7
|
+
const i = (d, g, w) => new Proxy(d, {
|
|
8
|
+
get: (t, n, c) => {
|
|
9
|
+
const o = w === 0 && s.get != null ? s.get(t, n, c) : t[n];
|
|
10
|
+
return typeof o == "object" && o !== null ? i(
|
|
11
|
+
Array.isArray(o) ? o.slice(0) : { ...o },
|
|
12
|
+
g.concat(Array.isArray(t) ? +n : n),
|
|
13
13
|
w + 1
|
|
14
|
-
) :
|
|
14
|
+
) : o;
|
|
15
15
|
},
|
|
16
|
-
set: (t,
|
|
16
|
+
set: (t, n, c) => (s.onSet(t, g, n, c, t[n]) && (t[n] = c), !0)
|
|
17
17
|
});
|
|
18
|
-
return
|
|
19
|
-
}, Q = (e, s,
|
|
18
|
+
return i(e, [], 0);
|
|
19
|
+
}, Q = (e, s, i) => {
|
|
20
20
|
let d = null;
|
|
21
|
-
typeof e == "number" ? d =
|
|
21
|
+
typeof e == "number" ? d = j(
|
|
22
22
|
s,
|
|
23
23
|
(t) => ({
|
|
24
|
-
increment: (
|
|
25
|
-
t.set(+t.get() + (
|
|
24
|
+
increment: (n) => {
|
|
25
|
+
t.set(+t.get() + (n ?? 0));
|
|
26
26
|
}
|
|
27
27
|
})
|
|
28
|
-
) : typeof e == "boolean" ? d =
|
|
28
|
+
) : typeof e == "boolean" ? d = j(
|
|
29
29
|
s,
|
|
30
30
|
(t) => ({
|
|
31
31
|
toggle: () => {
|
|
32
32
|
t.set(!t.get());
|
|
33
33
|
}
|
|
34
34
|
})
|
|
35
|
-
) : Array.isArray(e) ? d =
|
|
35
|
+
) : Array.isArray(e) ? d = j(
|
|
36
36
|
s,
|
|
37
37
|
(t) => ({
|
|
38
|
-
push: (...
|
|
39
|
-
t.set(t.get().concat(
|
|
38
|
+
push: (...n) => {
|
|
39
|
+
t.set(t.get().concat(n));
|
|
40
40
|
},
|
|
41
|
-
unshift: (...
|
|
42
|
-
t.set(
|
|
41
|
+
unshift: (...n) => {
|
|
42
|
+
t.set(n.concat(t.get()));
|
|
43
43
|
},
|
|
44
|
-
update: (
|
|
45
|
-
const c = t.get(),
|
|
46
|
-
|
|
44
|
+
update: (n) => {
|
|
45
|
+
const c = t.get(), o = M(c, n);
|
|
46
|
+
o !== c && t.set(o);
|
|
47
47
|
},
|
|
48
|
-
filter: (
|
|
49
|
-
t.set(t.get().filter(
|
|
48
|
+
filter: (n) => {
|
|
49
|
+
t.set(t.get().filter(n ?? X));
|
|
50
50
|
},
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
51
|
+
add: (n) => {
|
|
52
|
+
t.get().includes(n) || t.set(t.get().concat([n]));
|
|
53
|
+
},
|
|
54
|
+
remove: (n) => {
|
|
55
|
+
const c = t.get().indexOf(n);
|
|
56
|
+
if (c < 0) return;
|
|
57
|
+
const o = t.get().slice(0);
|
|
58
|
+
o.splice(c, 1), t.set(o);
|
|
59
|
+
},
|
|
60
|
+
toggle: (n, c) => {
|
|
61
|
+
const o = t.get().slice(), f = o.indexOf(n);
|
|
62
|
+
f < 0 ? c ? o.unshift(n) : o.push(n) : o.splice(f, 1), t.set(o);
|
|
54
63
|
}
|
|
55
64
|
})
|
|
56
|
-
) : e instanceof Set ? d =
|
|
65
|
+
) : e instanceof Set ? d = j(
|
|
57
66
|
s,
|
|
58
67
|
(t) => ({
|
|
59
|
-
add: (
|
|
60
|
-
t.set(new Set(t.get()).add(
|
|
68
|
+
add: (n) => {
|
|
69
|
+
t.set(new Set(t.get()).add(n));
|
|
61
70
|
},
|
|
62
|
-
delete: (
|
|
71
|
+
delete: (n) => {
|
|
63
72
|
const c = new Set(t.get());
|
|
64
|
-
c.delete(
|
|
73
|
+
c.delete(n), t.set(c);
|
|
65
74
|
},
|
|
66
|
-
toggle: (
|
|
75
|
+
toggle: (n) => {
|
|
67
76
|
const c = new Set(t.get());
|
|
68
|
-
c.has(
|
|
77
|
+
c.has(n) ? c.delete(n) : c.add(n), t.set(c);
|
|
69
78
|
},
|
|
70
79
|
clear: () => {
|
|
71
80
|
t.set(/* @__PURE__ */ new Set());
|
|
72
81
|
}
|
|
73
82
|
})
|
|
74
|
-
) : e instanceof Object && (d =
|
|
83
|
+
) : e instanceof Object && (d = j(
|
|
75
84
|
s,
|
|
76
85
|
(t) => ({
|
|
77
|
-
setPartial: (
|
|
86
|
+
setPartial: (n) => t.set((c) => ({
|
|
78
87
|
...c,
|
|
79
|
-
...typeof
|
|
88
|
+
...typeof n == "function" ? n(t.get()) : n
|
|
80
89
|
})),
|
|
81
|
-
update: (
|
|
82
|
-
const c = t.get(),
|
|
83
|
-
|
|
90
|
+
update: (n) => {
|
|
91
|
+
const c = t.get(), o = M(c, n);
|
|
92
|
+
o !== c && t.set(o);
|
|
84
93
|
},
|
|
85
|
-
setDeepPartial: (
|
|
86
|
-
if (
|
|
87
|
-
let h =
|
|
94
|
+
setDeepPartial: (n, c, o, f = V.keyPathSeparator ?? ".") => {
|
|
95
|
+
if (n.includes(f)) {
|
|
96
|
+
let h = n.split(f);
|
|
88
97
|
const b = h[h.length - 1];
|
|
89
98
|
h = h.slice(0, -1);
|
|
90
99
|
const y = { ...t.get() };
|
|
91
|
-
let
|
|
92
|
-
for (const
|
|
93
|
-
let p = S
|
|
94
|
-
const
|
|
95
|
-
if (p == null && (p = Array.isArray(
|
|
96
|
-
t.do.setPartial({ [
|
|
100
|
+
let A = y, D = o;
|
|
101
|
+
for (const S of h) {
|
|
102
|
+
let p = A[S];
|
|
103
|
+
const T = D = D == null ? void 0 : D[Array.isArray(D) ? "0" : S];
|
|
104
|
+
if (p == null && (p = Array.isArray(T) ? [] : {}), p == null || typeof p != "object") {
|
|
105
|
+
t.do.setPartial({ [n]: typeof c == "function" ? c(void 0) : c });
|
|
97
106
|
return;
|
|
98
107
|
}
|
|
99
|
-
|
|
108
|
+
A = A[S] = Array.isArray(p) ? [...p] : { ...p };
|
|
100
109
|
}
|
|
101
|
-
|
|
102
|
-
} else t.do.setPartial({ [
|
|
110
|
+
A[b] = typeof c == "function" ? c(A[b]) : c, t.set(y);
|
|
111
|
+
} else t.do.setPartial({ [n]: c });
|
|
103
112
|
}
|
|
104
113
|
})
|
|
105
114
|
));
|
|
106
|
-
const g = typeof
|
|
107
|
-
(t,
|
|
115
|
+
const g = typeof i == "object" && i != null && "do" in i ? i.do(
|
|
116
|
+
(t, n) => s.set(t, n),
|
|
108
117
|
() => s.get(),
|
|
109
118
|
s,
|
|
110
|
-
(t,
|
|
119
|
+
(t, n, c) => s.setDeferred(t, n, c)
|
|
111
120
|
) : null, w = {};
|
|
112
121
|
return g && Object.keys(g).forEach((t) => Object.defineProperty(w, t, { get: () => g[t] })), d && Object.keys(d).forEach(
|
|
113
122
|
(t) => Object.defineProperty(w, t, { get: () => d[t] })
|
|
114
123
|
), w;
|
|
115
|
-
}, X = (e) => e,
|
|
116
|
-
const
|
|
124
|
+
}, X = (e) => e, j = (e, s, i) => s(e), M = (e, s) => {
|
|
125
|
+
const i = Array.isArray(e) ? e.slice(0) : { ...e };
|
|
117
126
|
let d = !1;
|
|
118
127
|
const g = q(e, {
|
|
119
|
-
onSet: (w, t,
|
|
120
|
-
if (c ===
|
|
121
|
-
let f =
|
|
128
|
+
onSet: (w, t, n, c, o) => {
|
|
129
|
+
if (c === o) return !0;
|
|
130
|
+
let f = i;
|
|
122
131
|
d = !0;
|
|
123
132
|
for (const h of t) {
|
|
124
133
|
const b = f[h];
|
|
125
134
|
f = f[h] = Array.isArray(b) ? b.slice(0) : { ...b };
|
|
126
135
|
}
|
|
127
|
-
return f[
|
|
136
|
+
return f[n] = c, !0;
|
|
128
137
|
}
|
|
129
138
|
});
|
|
130
|
-
return s(g), d ?
|
|
139
|
+
return s(g), d ? i : e;
|
|
131
140
|
};
|
|
132
141
|
class Y {
|
|
133
|
-
constructor(s,
|
|
134
|
-
const d = (
|
|
135
|
-
let
|
|
142
|
+
constructor(s, i) {
|
|
143
|
+
const d = (r) => n = r, g = () => n, w = /* @__PURE__ */ new Set(), t = (r) => r(f());
|
|
144
|
+
let n = s, c, o = () => {
|
|
136
145
|
}, f = () => g(), h = null, b = () => {
|
|
137
|
-
const
|
|
138
|
-
return b = () =>
|
|
146
|
+
const r = Q(s, y, i);
|
|
147
|
+
return b = () => r, r;
|
|
139
148
|
};
|
|
140
149
|
const y = new Proxy(this, {
|
|
141
|
-
get: (
|
|
150
|
+
get: (r, a) => a === "do" ? b() : r[a],
|
|
142
151
|
set: Z
|
|
143
|
-
}),
|
|
144
|
-
const x = typeof
|
|
152
|
+
}), A = (r, a) => {
|
|
153
|
+
const x = typeof r == "function" ? r(f()) : r;
|
|
145
154
|
if (!(x === f() || x === void 0 || typeof x == "number" && isNaN(x))) {
|
|
146
155
|
d(x), w.forEach(t, this);
|
|
147
156
|
try {
|
|
148
157
|
$.postMessage({ key: u, value: g() });
|
|
149
158
|
} catch {
|
|
150
159
|
}
|
|
151
|
-
a !== !0 &&
|
|
160
|
+
a !== !0 && o(x);
|
|
152
161
|
}
|
|
153
162
|
};
|
|
154
|
-
this.set = (
|
|
155
|
-
w.delete(
|
|
163
|
+
this.set = (r, a) => A(r, a), this.get = () => f(), this.initialValue = s, this.isInitialValue = () => s === g(), this.subscribe = (r) => (w.add(r), () => {
|
|
164
|
+
w.delete(r);
|
|
156
165
|
}), this.reset = () => {
|
|
157
|
-
|
|
166
|
+
A(s, !0), w.forEach(t, this);
|
|
158
167
|
};
|
|
159
|
-
const
|
|
160
|
-
|
|
168
|
+
const D = (r, a) => {
|
|
169
|
+
A(r, a), c = void 0;
|
|
161
170
|
};
|
|
162
|
-
if (this.setDeferred = (
|
|
163
|
-
L && c === void 0 &&
|
|
164
|
-
},
|
|
165
|
-
let
|
|
166
|
-
if (
|
|
167
|
-
throw console.error(
|
|
168
|
-
} : (
|
|
169
|
-
if (typeof
|
|
170
|
-
|
|
171
|
-
else if ("storeKey" in
|
|
172
|
-
|
|
171
|
+
if (this.setDeferred = (r, a = 500, x, L = !0) => {
|
|
172
|
+
L && c === void 0 && A(r, x), clearTimeout(c), c = setTimeout(D, a, r, x);
|
|
173
|
+
}, i == null) return y;
|
|
174
|
+
let S = null, p = null, T = !0, P = !0, E = !1, z = -1, C = s instanceof Set ? (r) => new Set(r) : (r) => r, m = s instanceof Set ? (r) => {
|
|
175
|
+
if (r instanceof Set) return Array.from(r);
|
|
176
|
+
throw console.error(r), "The value is not Set instance";
|
|
177
|
+
} : (r) => r;
|
|
178
|
+
if (typeof i == "string")
|
|
179
|
+
S = i;
|
|
180
|
+
else if ("storeKey" in i)
|
|
181
|
+
T = i.warnOnDuplicateStoreKey ?? T, P = i.listenStorageChanges ?? P, S = i.storeKey, C = i.unzipValue ?? C, m = i.zipValue ?? m, E = i.unchangable ?? E, p = i.exp ?? p;
|
|
173
182
|
else return y;
|
|
174
|
-
const u = `${B}${
|
|
175
|
-
const a = JSON.parse(
|
|
183
|
+
const u = `${B}${S}`, F = p === null || !(p(y, u in l) instanceof Date) ? (r) => JSON.stringify([m(r)]) : (r) => (h ?? (h = {}), h.exp = p(y, u in l).getTime() + 0.2866, h.exp - Date.now() < 24 * 60 * 60 * 1e3 && (clearTimeout(z), clearTimeout(G[u]), z = setTimeout(() => this.reset(), h.exp - Date.now())), JSON.stringify([m(r), h])), J = (r) => {
|
|
184
|
+
const a = JSON.parse(r);
|
|
176
185
|
return h = a[1], C(a[0]);
|
|
177
186
|
};
|
|
178
187
|
let H = !0;
|
|
179
|
-
if (N[u] = y,
|
|
188
|
+
if (N[u] = y, l[`atom/${S}`] && (l[u] || (l[u] = `[${l[`atom/${S}`]}]`), delete l[`atom/${S}`]), f = () => {
|
|
180
189
|
if (f = g, H) {
|
|
181
190
|
H = !1;
|
|
182
191
|
try {
|
|
183
|
-
d(u in
|
|
192
|
+
d(u in l ? J(l[u]) : s);
|
|
184
193
|
} catch {
|
|
185
|
-
console.warn("Invalid json value",
|
|
194
|
+
console.warn("Invalid json value", l[u]);
|
|
186
195
|
}
|
|
187
196
|
}
|
|
188
197
|
return g();
|
|
189
|
-
},
|
|
190
|
-
if (
|
|
198
|
+
}, o = (r) => {
|
|
199
|
+
if (r === s) {
|
|
191
200
|
this.reset();
|
|
192
201
|
return;
|
|
193
202
|
}
|
|
194
|
-
|
|
203
|
+
l[u] = F(r);
|
|
195
204
|
}, this.reset = () => {
|
|
196
|
-
delete
|
|
197
|
-
},
|
|
205
|
+
delete l[u], A(s, !0);
|
|
206
|
+
}, T && _[u] !== void 0 && console.warn("Duplicate Atom key", S), P)
|
|
198
207
|
if (E) {
|
|
199
|
-
let
|
|
200
|
-
I[u] = this,
|
|
201
|
-
clearTimeout(a), a = setTimeout(() =>
|
|
208
|
+
let r = !1, a;
|
|
209
|
+
I[u] = this, _[u] = () => {
|
|
210
|
+
clearTimeout(a), a = setTimeout(() => r = !1, 10), !r && (r = !0, l[u] = F(g()));
|
|
202
211
|
};
|
|
203
212
|
} else
|
|
204
|
-
|
|
205
|
-
if (
|
|
213
|
+
_[u] = (r) => {
|
|
214
|
+
if (r.newValue === null) {
|
|
206
215
|
this.reset();
|
|
207
216
|
return;
|
|
208
217
|
}
|
|
209
218
|
try {
|
|
210
|
-
|
|
219
|
+
A(J(r.newValue));
|
|
211
220
|
} catch {
|
|
212
|
-
console.warn("Invalid json value",
|
|
221
|
+
console.warn("Invalid json value", r.newValue);
|
|
213
222
|
}
|
|
214
223
|
};
|
|
215
224
|
return y;
|
|
@@ -223,28 +232,28 @@ try {
|
|
|
223
232
|
});
|
|
224
233
|
} catch {
|
|
225
234
|
}
|
|
226
|
-
const
|
|
235
|
+
const l = window.localStorage, _ = {}, I = {}, Z = (e, s) => {
|
|
227
236
|
throw `${s} is readonly property`;
|
|
228
237
|
};
|
|
229
238
|
window.addEventListener("storage", (e) => {
|
|
230
239
|
var s;
|
|
231
|
-
e.key === null || e.newValue === e.oldValue || (s =
|
|
240
|
+
e.key === null || e.newValue === e.oldValue || (s = _[e.key]) == null || s.call(_, e);
|
|
232
241
|
});
|
|
233
|
-
const v =
|
|
234
|
-
|
|
235
|
-
I[e] === void 0 && v.call(
|
|
242
|
+
const v = l.setItem.bind(l), k = l.removeItem.bind(l);
|
|
243
|
+
l.setItem = (e, s) => {
|
|
244
|
+
I[e] === void 0 && v.call(l, e, s);
|
|
236
245
|
};
|
|
237
|
-
|
|
238
|
-
I[e] === void 0 && k.call(
|
|
246
|
+
l.removeItem = (e) => {
|
|
247
|
+
I[e] === void 0 && k.call(l, e);
|
|
239
248
|
};
|
|
240
249
|
const O = /"exp":(\d+)\.2866/, B = "atom\\", N = {}, G = {};
|
|
241
250
|
setTimeout(() => {
|
|
242
|
-
Object.keys(
|
|
243
|
-
var
|
|
244
|
-
if (!e.startsWith(B) || typeof
|
|
245
|
-
const s = +((
|
|
251
|
+
Object.keys(l).forEach((e) => {
|
|
252
|
+
var i;
|
|
253
|
+
if (!e.startsWith(B) || typeof l[e] != "string") return;
|
|
254
|
+
const s = +((i = l[e].match(O)) == null ? void 0 : i[1]);
|
|
246
255
|
s && s - Date.now() < 24 * 60 * 60 * 1e3 && (G[e] = setTimeout(() => {
|
|
247
|
-
N[e] ? N[e].reset() : delete
|
|
256
|
+
N[e] ? N[e].reset() : delete l[e];
|
|
248
257
|
}, s - Date.now()));
|
|
249
258
|
});
|
|
250
259
|
}, 1e3);
|
|
@@ -253,8 +262,8 @@ export {
|
|
|
253
262
|
se as atom,
|
|
254
263
|
K as configureAtomaric,
|
|
255
264
|
V as configuredOptions,
|
|
256
|
-
|
|
257
|
-
|
|
265
|
+
re as useAtom,
|
|
266
|
+
ne as useAtomDo,
|
|
258
267
|
te as useAtomGet,
|
|
259
268
|
W as useAtomSet,
|
|
260
269
|
ee as useAtomSetDeferred,
|
package/build/atomaric.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(g,j){typeof exports=="object"&&typeof module<"u"?j(exports):typeof define=="function"&&define.amd?define(["exports"],j):(g=typeof globalThis<"u"?globalThis:g||self,j(g.atomaric={}))})(this,function(g){"use strict";let j=()=>{throw"call configureAtomaric() before all!"};const I={},X=e=>{I.useSyncExternalStore=j=e.useSyncExternalStore,I.keyPathSeparator=e.keyPathSeparator},J=e=>j(e.subscribe,e.get),M=e=>e.set,Y=e=>e.setDeferred,Z=e=>e.get,v=e=>e.do,k=e=>[J(e),M(e)],O=(e,r)=>new H(e,r),K=(e,r)=>{const
|
|
1
|
+
(function(g,j){typeof exports=="object"&&typeof module<"u"?j(exports):typeof define=="function"&&define.amd?define(["exports"],j):(g=typeof globalThis<"u"?globalThis:g||self,j(g.atomaric={}))})(this,function(g){"use strict";let j=()=>{throw"call configureAtomaric() before all!"};const I={},X=e=>{I.useSyncExternalStore=j=e.useSyncExternalStore,I.keyPathSeparator=e.keyPathSeparator},J=e=>j(e.subscribe,e.get),M=e=>e.set,Y=e=>e.setDeferred,Z=e=>e.get,v=e=>e.do,k=e=>[J(e),M(e)],O=(e,r)=>new H(e,r),K=(e,r)=>{const i=(d,h,w)=>new Proxy(d,{get:(t,n,c)=>{const o=w===0&&r.get!=null?r.get(t,n,c):t[n];return typeof o=="object"&&o!==null?i(Array.isArray(o)?o.slice(0):{...o},h.concat(Array.isArray(t)?+n:n),w+1):o},set:(t,n,c)=>(r.onSet(t,h,n,c,t[n])&&(t[n]=c),!0)});return i(e,[],0)},ee=(e,r,i)=>{let d=null;typeof e=="number"?d=x(r,t=>({increment:n=>{t.set(+t.get()+(n??0))}})):typeof e=="boolean"?d=x(r,t=>({toggle:()=>{t.set(!t.get())}})):Array.isArray(e)?d=x(r,t=>({push:(...n)=>{t.set(t.get().concat(n))},unshift:(...n)=>{t.set(n.concat(t.get()))},update:n=>{const c=t.get(),o=G(c,n);o!==c&&t.set(o)},filter:n=>{t.set(t.get().filter(n??te))},add:n=>{t.get().includes(n)||t.set(t.get().concat([n]))},remove:n=>{const c=t.get().indexOf(n);if(c<0)return;const o=t.get().slice(0);o.splice(c,1),t.set(o)},toggle:(n,c)=>{const o=t.get().slice(),f=o.indexOf(n);f<0?c?o.unshift(n):o.push(n):o.splice(f,1),t.set(o)}})):e instanceof Set?d=x(r,t=>({add:n=>{t.set(new Set(t.get()).add(n))},delete:n=>{const c=new Set(t.get());c.delete(n),t.set(c)},toggle:n=>{const c=new Set(t.get());c.has(n)?c.delete(n):c.add(n),t.set(c)},clear:()=>{t.set(new Set)}})):e instanceof Object&&(d=x(r,t=>({setPartial:n=>t.set(c=>({...c,...typeof n=="function"?n(t.get()):n})),update:n=>{const c=t.get(),o=G(c,n);o!==c&&t.set(o)},setDeepPartial:(n,c,o,f=I.keyPathSeparator??".")=>{if(n.includes(f)){let A=n.split(f);const m=A[A.length-1];A=A.slice(0,-1);const b={...t.get()};let y=b,T=o;for(const p of A){let S=y[p];const P=T=T==null?void 0:T[Array.isArray(T)?"0":p];if(S==null&&(S=Array.isArray(P)?[]:{}),S==null||typeof S!="object"){t.do.setPartial({[n]:typeof c=="function"?c(void 0):c});return}y=y[p]=Array.isArray(S)?[...S]:{...S}}y[m]=typeof c=="function"?c(y[m]):c,t.set(b)}else t.do.setPartial({[n]:c})}})));const h=typeof i=="object"&&i!=null&&"do"in i?i.do((t,n)=>r.set(t,n),()=>r.get(),r,(t,n,c)=>r.setDeferred(t,n,c)):null,w={};return h&&Object.keys(h).forEach(t=>Object.defineProperty(w,t,{get:()=>h[t]})),d&&Object.keys(d).forEach(t=>Object.defineProperty(w,t,{get:()=>d[t]})),w},te=e=>e,x=(e,r,i)=>r(e),G=(e,r)=>{const i=Array.isArray(e)?e.slice(0):{...e};let d=!1;const h=K(e,{onSet:(w,t,n,c,o)=>{if(c===o)return!0;let f=i;d=!0;for(const A of t){const m=f[A];f=f[A]=Array.isArray(m)?m.slice(0):{...m}}return f[n]=c,!0}});return r(h),d?i:e};class H{constructor(r,i){const d=s=>n=s,h=()=>n,w=new Set,t=s=>s(f());let n=r,c,o=()=>{},f=()=>h(),A=null,m=()=>{const s=ee(r,b,i);return m=()=>s,s};const b=new Proxy(this,{get:(s,a)=>a==="do"?m():s[a],set:ne}),y=(s,a)=>{const D=typeof s=="function"?s(f()):s;if(!(D===f()||D===void 0||typeof D=="number"&&isNaN(D))){d(D),w.forEach(t,this);try{C.postMessage({key:l,value:h()})}catch{}a!==!0&&o(D)}};this.set=(s,a)=>y(s,a),this.get=()=>f(),this.initialValue=r,this.isInitialValue=()=>r===h(),this.subscribe=s=>(w.add(s),()=>{w.delete(s)}),this.reset=()=>{y(r,!0),w.forEach(t,this)};const T=(s,a)=>{y(s,a),c=void 0};if(this.setDeferred=(s,a=500,D,Q=!0)=>{Q&&c===void 0&&y(s,D),clearTimeout(c),c=setTimeout(T,a,s,D)},i==null)return b;let p=null,S=null,P=!0,N=!0,z=!1,B=-1,F=r instanceof Set?s=>new Set(s):s=>s,V=r instanceof Set?s=>{if(s instanceof Set)return Array.from(s);throw console.error(s),"The value is not Set instance"}:s=>s;if(typeof i=="string")p=i;else if("storeKey"in i)P=i.warnOnDuplicateStoreKey??P,N=i.listenStorageChanges??N,p=i.storeKey,F=i.unzipValue??F,V=i.zipValue??V,z=i.unchangable??z,S=i.exp??S;else return b;const l=`${L}${p}`,U=S===null||!(S(b,l in u)instanceof Date)?s=>JSON.stringify([V(s)]):s=>(A??(A={}),A.exp=S(b,l in u).getTime()+.2866,A.exp-Date.now()<24*60*60*1e3&&(clearTimeout(B),clearTimeout(R[l]),B=setTimeout(()=>this.reset(),A.exp-Date.now())),JSON.stringify([V(s),A])),W=s=>{const a=JSON.parse(s);return A=a[1],F(a[0])};let q=!0;if($[l]=b,u[`atom/${p}`]&&(u[l]||(u[l]=`[${u[`atom/${p}`]}]`),delete u[`atom/${p}`]),f=()=>{if(f=h,q){q=!1;try{d(l in u?W(u[l]):r)}catch{console.warn("Invalid json value",u[l])}}return h()},o=s=>{if(s===r){this.reset();return}u[l]=U(s)},this.reset=()=>{delete u[l],y(r,!0)},P&&_[l]!==void 0&&console.warn("Duplicate Atom key",p),N)if(z){let s=!1,a;E[l]=this,_[l]=()=>{clearTimeout(a),a=setTimeout(()=>s=!1,10),!s&&(s=!0,u[l]=U(h()))}}else _[l]=s=>{if(s.newValue===null){this.reset();return}try{y(W(s.newValue))}catch{console.warn("Invalid json value",s.newValue)}};return b}}let C;try{C=new BroadcastChannel("updateHere"),C.addEventListener("message",e=>{var r;(r=E[e.data.key])==null||r.set(e.data.value,!0)})}catch{}const u=window.localStorage,_={},E={},ne=(e,r)=>{throw`${r} is readonly property`};window.addEventListener("storage",e=>{var r;e.key===null||e.newValue===e.oldValue||(r=_[e.key])==null||r.call(_,e)});const se=u.setItem.bind(u),re=u.removeItem.bind(u);u.setItem=(e,r)=>{E[e]===void 0&&se.call(u,e,r)},u.removeItem=e=>{E[e]===void 0&&re.call(u,e)};const ce=/"exp":(\d+)\.2866/,L="atom\\",$={},R={};setTimeout(()=>{Object.keys(u).forEach(e=>{var i;if(!e.startsWith(L)||typeof u[e]!="string")return;const r=+((i=u[e].match(ce))==null?void 0:i[1]);r&&r-Date.now()<24*60*60*1e3&&(R[e]=setTimeout(()=>{$[e]?$[e].reset():delete u[e]},r-Date.now()))})},1e3),g.Atom=H,g.atom=O,g.configureAtomaric=X,g.configuredOptions=I,g.useAtom=k,g.useAtomDo=v,g.useAtomGet=Z,g.useAtomSet=M,g.useAtomSetDeferred=Y,g.useAtomValue=J,Object.defineProperty(g,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
package/types/index.d.ts
CHANGED
|
@@ -109,6 +109,10 @@ export type ArrayActions<Value> = UpdateAction<Value[]> & {
|
|
|
109
109
|
filter: (filter?: (value: Value, index: number, array: Value[]) => any) => void;
|
|
110
110
|
/** will add value if it doesn't exist, otherwise delete */
|
|
111
111
|
toggle: (value: Value, isAddInStart?: boolean) => void;
|
|
112
|
+
/** will add value if not exists */
|
|
113
|
+
add: (value: Value) => void;
|
|
114
|
+
/** will delete value from array */
|
|
115
|
+
remove: (value: Value) => void;
|
|
112
116
|
};
|
|
113
117
|
|
|
114
118
|
export type DefaultActions<Value> = Value extends Set<infer Val>
|