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