atomaric 0.0.34 → 0.0.36
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 +79 -77
- package/build/atomaric.umd.cjs +1 -1
- package/package.json +3 -3
- package/types/{model.d.ts → index.d.ts} +15 -4
- package/types/paths.ts +12 -12
package/build/atomaric.js
CHANGED
|
@@ -1,20 +1,25 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
let M = () => {
|
|
2
|
+
throw "call configureAtomaric() before all!";
|
|
3
|
+
};
|
|
4
|
+
const $ = {}, v = (e) => {
|
|
5
|
+
$.useSyncExternalStore = M = e.useSyncExternalStore, $.keyPathSeparator = e.keyPathSeparator;
|
|
6
|
+
}, B = (e) => M(e.subscribe, e.get), G = (e) => e.set, K = (e) => e.setDeferred, O = (e) => e.get, ee = (e) => e.do, te = (e) => [B(e), G(e)], se = (e, r) => new q(e, r), U = (e, r, i) => {
|
|
7
|
+
let d = null;
|
|
8
|
+
typeof e == "number" ? d = _(
|
|
4
9
|
r,
|
|
5
10
|
(s) => ({
|
|
6
11
|
increment: (n) => {
|
|
7
12
|
s.set(+s.get() + (n ?? 0));
|
|
8
13
|
}
|
|
9
14
|
})
|
|
10
|
-
) : typeof e == "boolean" ?
|
|
15
|
+
) : typeof e == "boolean" ? d = _(
|
|
11
16
|
r,
|
|
12
17
|
(s) => ({
|
|
13
18
|
toggle: () => {
|
|
14
19
|
s.set(!s.get());
|
|
15
20
|
}
|
|
16
21
|
})
|
|
17
|
-
) : Array.isArray(e) ?
|
|
22
|
+
) : Array.isArray(e) ? d = _(
|
|
18
23
|
r,
|
|
19
24
|
(s) => ({
|
|
20
25
|
push: (...n) => {
|
|
@@ -28,14 +33,14 @@ const B = (e, r, i) => {
|
|
|
28
33
|
n(c), s.set(c);
|
|
29
34
|
},
|
|
30
35
|
filter: (n) => {
|
|
31
|
-
s.set(s.get().filter(n ??
|
|
36
|
+
s.set(s.get().filter(n ?? W));
|
|
32
37
|
},
|
|
33
38
|
toggle: (n, c) => {
|
|
34
39
|
const g = s.get().slice(), f = g.indexOf(n);
|
|
35
40
|
f < 0 ? c ? g.unshift(n) : g.push(n) : g.splice(f, 1), s.set(g);
|
|
36
41
|
}
|
|
37
42
|
})
|
|
38
|
-
) : e instanceof Set ?
|
|
43
|
+
) : e instanceof Set ? d = _(
|
|
39
44
|
r,
|
|
40
45
|
(s) => ({
|
|
41
46
|
add: (n) => {
|
|
@@ -57,7 +62,7 @@ const B = (e, r, i) => {
|
|
|
57
62
|
n(c), s.set(c);
|
|
58
63
|
}
|
|
59
64
|
})
|
|
60
|
-
) : e instanceof Object && (
|
|
65
|
+
) : e instanceof Object && (d = _(
|
|
61
66
|
r,
|
|
62
67
|
(s) => ({
|
|
63
68
|
setPartial: (n) => s.set((c) => ({
|
|
@@ -68,22 +73,22 @@ const B = (e, r, i) => {
|
|
|
68
73
|
const c = { ...s.get() };
|
|
69
74
|
n(c), s.set(c);
|
|
70
75
|
},
|
|
71
|
-
setDeepPartial: (n, c, g = ".") => {
|
|
76
|
+
setDeepPartial: (n, c, g = $.keyPathSeparator ?? ".") => {
|
|
72
77
|
if (n.includes(g)) {
|
|
73
78
|
let f = n.split(g);
|
|
74
79
|
const w = f[f.length - 1];
|
|
75
80
|
f = f.slice(0, -1);
|
|
76
81
|
const D = { ...s.get() };
|
|
77
|
-
let
|
|
82
|
+
let a = D;
|
|
78
83
|
for (const S of f) {
|
|
79
|
-
const
|
|
80
|
-
if (
|
|
84
|
+
const y = a[S];
|
|
85
|
+
if (y == null || typeof y != "object") {
|
|
81
86
|
s.do.setPartial({ [n]: typeof c == "function" ? c(void 0) : c });
|
|
82
87
|
return;
|
|
83
88
|
}
|
|
84
|
-
|
|
89
|
+
a = a[S] = Array.isArray(y) ? [...y] : { ...y };
|
|
85
90
|
}
|
|
86
|
-
|
|
91
|
+
a[w] = typeof c == "function" ? c(a[w]) : c, s.set(D);
|
|
87
92
|
} else s.do.setPartial({ [n]: c });
|
|
88
93
|
}
|
|
89
94
|
})
|
|
@@ -93,63 +98,63 @@ const B = (e, r, i) => {
|
|
|
93
98
|
() => r.get(),
|
|
94
99
|
r,
|
|
95
100
|
(s, n, c) => r.setDeferred(s, n, c)
|
|
96
|
-
) : null,
|
|
97
|
-
return h && Object.keys(h).forEach((s) => Object.defineProperty(
|
|
98
|
-
(s) => Object.defineProperty(
|
|
99
|
-
),
|
|
100
|
-
},
|
|
101
|
-
class
|
|
101
|
+
) : null, b = {};
|
|
102
|
+
return h && Object.keys(h).forEach((s) => Object.defineProperty(b, s, { get: () => h[s] })), d && Object.keys(d).forEach(
|
|
103
|
+
(s) => Object.defineProperty(b, s, { get: () => d[s] })
|
|
104
|
+
), b;
|
|
105
|
+
}, W = (e) => e, _ = (e, r, i) => r(e);
|
|
106
|
+
class q {
|
|
102
107
|
constructor(r, i) {
|
|
103
|
-
const
|
|
108
|
+
const d = (t) => n = t, h = () => n, b = /* @__PURE__ */ new Set(), s = (t) => t(f());
|
|
104
109
|
let n = r, c, g = () => {
|
|
105
110
|
}, f = () => h(), w = null, D = () => {
|
|
106
|
-
const t =
|
|
111
|
+
const t = U(r, a, i);
|
|
107
112
|
return D = () => t, t;
|
|
108
113
|
};
|
|
109
|
-
const
|
|
114
|
+
const a = new Proxy(this, {
|
|
110
115
|
get: (t, u) => u === "do" ? D() : t[u],
|
|
111
|
-
set:
|
|
116
|
+
set: Q
|
|
112
117
|
}), S = (t, u) => {
|
|
113
|
-
const
|
|
114
|
-
if (!(
|
|
115
|
-
|
|
118
|
+
const p = typeof t == "function" ? t(f()) : t;
|
|
119
|
+
if (!(p === f() || p === void 0 || typeof p == "number" && isNaN(p))) {
|
|
120
|
+
d(p), b.forEach(s, this);
|
|
116
121
|
try {
|
|
117
|
-
|
|
122
|
+
N.postMessage({ key: l, value: h() });
|
|
118
123
|
} catch {
|
|
119
124
|
}
|
|
120
|
-
u !== !0 && g(
|
|
125
|
+
u !== !0 && g(p);
|
|
121
126
|
}
|
|
122
127
|
};
|
|
123
|
-
this.set = (t, u) => S(t, u), this.get = () => f(), this.initialValue = r, this.isInitialValue = () => r === h(), this.subscribe = (t) => (
|
|
124
|
-
|
|
128
|
+
this.set = (t, u) => S(t, u), this.get = () => f(), this.initialValue = r, this.isInitialValue = () => r === h(), this.subscribe = (t) => (b.add(t), () => {
|
|
129
|
+
b.delete(t);
|
|
125
130
|
}), this.reset = () => {
|
|
126
|
-
S(r, !0),
|
|
131
|
+
S(r, !0), b.forEach(s, this);
|
|
127
132
|
};
|
|
128
|
-
const
|
|
133
|
+
const y = (t, u) => {
|
|
129
134
|
S(t, u), c = void 0;
|
|
130
135
|
};
|
|
131
|
-
if (this.setDeferred = (t, u = 500,
|
|
132
|
-
|
|
133
|
-
}, i == null) return
|
|
134
|
-
let
|
|
136
|
+
if (this.setDeferred = (t, u = 500, p, L = !0) => {
|
|
137
|
+
L && c === void 0 && S(t, p), clearTimeout(c), c = setTimeout(y, u, t, p);
|
|
138
|
+
}, i == null) return a;
|
|
139
|
+
let A = null, T = null, I = !0, E = !0, P = !1, V = -1, C = r instanceof Set ? (t) => new Set(t) : (t) => t, j = r instanceof Set ? (t) => {
|
|
135
140
|
if (t instanceof Set) return Array.from(t);
|
|
136
141
|
throw console.error(t), "The value is not Set instance";
|
|
137
142
|
} : (t) => t;
|
|
138
143
|
if (typeof i == "string")
|
|
139
|
-
|
|
144
|
+
A = i;
|
|
140
145
|
else if ("storeKey" in i)
|
|
141
|
-
I = i.warnOnDuplicateStoreKey ?? I, E = i.listenStorageChanges ?? E,
|
|
142
|
-
else return
|
|
143
|
-
const l = `${
|
|
146
|
+
I = i.warnOnDuplicateStoreKey ?? I, E = i.listenStorageChanges ?? E, A = i.storeKey, C = i.unzipValue ?? C, j = i.zipValue ?? j, P = i.unchangable ?? P, T = i.exp ?? T;
|
|
147
|
+
else return a;
|
|
148
|
+
const l = `${R}${A}`, F = T === null || !(T(a, l in o) instanceof Date) ? (t) => JSON.stringify([j(t)]) : (t) => (w ?? (w = {}), w.exp = T(a, l in o).getTime() + 0.2866, w.exp - Date.now() < 24 * 60 * 60 * 1e3 && (clearTimeout(V), clearTimeout(k[l]), V = setTimeout(() => this.reset(), w.exp - Date.now())), JSON.stringify([j(t), w])), J = (t) => {
|
|
144
149
|
const u = JSON.parse(t);
|
|
145
|
-
return w = u[1],
|
|
150
|
+
return w = u[1], C(u[0]);
|
|
146
151
|
};
|
|
147
|
-
let
|
|
148
|
-
if (
|
|
149
|
-
if (f = h,
|
|
150
|
-
|
|
152
|
+
let H = !0;
|
|
153
|
+
if (z[l] = a, o[`atom/${A}`] && (o[l] || (o[l] = `[${o[`atom/${A}`]}]`), delete o[`atom/${A}`]), f = () => {
|
|
154
|
+
if (f = h, H) {
|
|
155
|
+
H = !1;
|
|
151
156
|
try {
|
|
152
|
-
|
|
157
|
+
d(l in o ? J(o[l]) : r);
|
|
153
158
|
} catch {
|
|
154
159
|
console.warn("Invalid json value", o[l]);
|
|
155
160
|
}
|
|
@@ -160,14 +165,14 @@ class U {
|
|
|
160
165
|
this.reset();
|
|
161
166
|
return;
|
|
162
167
|
}
|
|
163
|
-
o[l] =
|
|
168
|
+
o[l] = F(t);
|
|
164
169
|
}, this.reset = () => {
|
|
165
170
|
delete o[l], S(r, !0);
|
|
166
|
-
}, I && x[l] !== void 0 && console.warn("Duplicate Atom key",
|
|
167
|
-
if (
|
|
171
|
+
}, I && x[l] !== void 0 && console.warn("Duplicate Atom key", A), E)
|
|
172
|
+
if (P) {
|
|
168
173
|
let t = !1, u;
|
|
169
174
|
m[l] = this, x[l] = () => {
|
|
170
|
-
clearTimeout(u), u = setTimeout(() => t = !1, 10), !t && (t = !0, o[l] =
|
|
175
|
+
clearTimeout(u), u = setTimeout(() => t = !1, 10), !t && (t = !0, o[l] = F(h()));
|
|
171
176
|
};
|
|
172
177
|
} else
|
|
173
178
|
x[l] = (t) => {
|
|
@@ -176,59 +181,56 @@ class U {
|
|
|
176
181
|
return;
|
|
177
182
|
}
|
|
178
183
|
try {
|
|
179
|
-
S(
|
|
184
|
+
S(J(t.newValue));
|
|
180
185
|
} catch {
|
|
181
186
|
console.warn("Invalid json value", t.newValue);
|
|
182
187
|
}
|
|
183
188
|
};
|
|
184
|
-
return
|
|
189
|
+
return a;
|
|
185
190
|
}
|
|
186
191
|
}
|
|
187
|
-
let
|
|
192
|
+
let N;
|
|
188
193
|
try {
|
|
189
|
-
|
|
194
|
+
N = new BroadcastChannel("updateHere"), N.addEventListener("message", (e) => {
|
|
190
195
|
var r;
|
|
191
196
|
(r = m[e.data.key]) == null || r.set(e.data.value, !0);
|
|
192
197
|
});
|
|
193
198
|
} catch {
|
|
194
199
|
}
|
|
195
|
-
const o = window.localStorage, x = {}, m = {},
|
|
200
|
+
const o = window.localStorage, x = {}, m = {}, Q = (e, r) => {
|
|
196
201
|
throw `${r} is readonly property`;
|
|
197
202
|
};
|
|
198
203
|
window.addEventListener("storage", (e) => {
|
|
199
204
|
var r;
|
|
200
205
|
e.key === null || e.newValue === e.oldValue || (r = x[e.key]) == null || r.call(x, e);
|
|
201
206
|
});
|
|
202
|
-
const
|
|
207
|
+
const X = o.setItem.bind(o), Y = o.removeItem.bind(o);
|
|
203
208
|
o.setItem = (e, r) => {
|
|
204
|
-
m[e] === void 0 &&
|
|
209
|
+
m[e] === void 0 && X.call(o, e, r);
|
|
205
210
|
};
|
|
206
211
|
o.removeItem = (e) => {
|
|
207
|
-
m[e] === void 0 &&
|
|
212
|
+
m[e] === void 0 && Y.call(o, e);
|
|
208
213
|
};
|
|
209
|
-
const
|
|
214
|
+
const Z = /"exp":(\d+)\.2866/, R = "atom\\", z = {}, k = {};
|
|
210
215
|
setTimeout(() => {
|
|
211
216
|
Object.keys(o).forEach((e) => {
|
|
212
217
|
var i;
|
|
213
|
-
if (!e.startsWith(
|
|
214
|
-
const r = +((i = o[e].match(
|
|
215
|
-
r && r - Date.now() < 24 * 60 * 60 * 1e3 && (
|
|
216
|
-
|
|
218
|
+
if (!e.startsWith(R) || typeof o[e] != "string") return;
|
|
219
|
+
const r = +((i = o[e].match(Z)) == null ? void 0 : i[1]);
|
|
220
|
+
r && r - Date.now() < 24 * 60 * 60 * 1e3 && (k[e] = setTimeout(() => {
|
|
221
|
+
z[e] ? z[e].reset() : delete o[e];
|
|
217
222
|
}, r - Date.now()));
|
|
218
223
|
});
|
|
219
224
|
}, 1e3);
|
|
220
|
-
let R = () => {
|
|
221
|
-
throw "call configureAtomaric() before all!";
|
|
222
|
-
};
|
|
223
|
-
const k = (e) => R = e.useSyncExternalStore, Y = (e) => R(e.subscribe, e.get), Z = (e) => e.set, v = (e) => e.setDeferred, K = (e) => e.get, O = (e) => e.do, ee = (e) => [Y(e), Z(e)], te = (e, r) => new U(e, r);
|
|
224
225
|
export {
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
226
|
+
q as Atom,
|
|
227
|
+
se as atom,
|
|
228
|
+
v as configureAtomaric,
|
|
229
|
+
$ as configuredOptions,
|
|
230
|
+
te as useAtom,
|
|
231
|
+
ee as useAtomDo,
|
|
232
|
+
O as useAtomGet,
|
|
233
|
+
G as useAtomSet,
|
|
234
|
+
K as useAtomSetDeferred,
|
|
235
|
+
B as useAtomValue
|
|
234
236
|
};
|
package/build/atomaric.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(f,T){typeof exports=="object"&&typeof module<"u"?T(exports):typeof define=="function"&&define.amd?define(["exports"],T):(f=typeof globalThis<"u"?globalThis:f||self,T(f.atomaric={}))})(this,function(f){"use strict";let T=()=>{throw"call configureAtomaric() before all!"};const E={},q=e=>{E.useSyncExternalStore=T=e.useSyncExternalStore,E.keyPathSeparator=e.keyPathSeparator},J=e=>T(e.subscribe,e.get),M=e=>e.set,Q=e=>e.setDeferred,X=e=>e.get,Y=e=>e.do,Z=e=>[J(e),M(e)],v=(e,r)=>new G(e,r),K=(e,r,i)=>{let g=null;typeof e=="number"?g=j(r,n=>({increment:s=>{n.set(+n.get()+(s??0))}})):typeof e=="boolean"?g=j(r,n=>({toggle:()=>{n.set(!n.get())}})):Array.isArray(e)?g=j(r,n=>({push:(...s)=>{n.set(n.get().concat(s))},unshift:(...s)=>{n.set(s.concat(n.get()))},update:s=>{const o=n.get().slice();s(o),n.set(o)},filter:s=>{n.set(n.get().filter(s??O))},toggle:(s,o)=>{const h=n.get().slice(),a=h.indexOf(s);a<0?o?h.unshift(s):h.push(s):h.splice(a,1),n.set(h)}})):e instanceof Set?g=j(r,n=>({add:s=>{n.set(new Set(n.get()).add(s))},delete:s=>{const o=new Set(n.get());o.delete(s),n.set(o)},toggle:s=>{const o=new Set(n.get());o.has(s)?o.delete(s):o.add(s),n.set(o)},clear:()=>{n.set(new Set)},update:s=>{const o=new Set(n.get());s(o),n.set(o)}})):e instanceof Object&&(g=j(r,n=>({setPartial:s=>n.set(o=>({...o,...typeof s=="function"?s(n.get()):s})),update:s=>{const o={...n.get()};s(o),n.set(o)},setDeepPartial:(s,o,h=E.keyPathSeparator??".")=>{if(s.includes(h)){let a=s.split(h);const w=a[a.length-1];a=a.slice(0,-1);const _={...n.get()};let d=_;for(const A of a){const y=d[A];if(y==null||typeof y!="object"){n.do.setPartial({[s]:typeof o=="function"?o(void 0):o});return}d=d[A]=Array.isArray(y)?[...y]:{...y}}d[w]=typeof o=="function"?o(d[w]):o,n.set(_)}else n.do.setPartial({[s]:o})}})));const S=typeof i=="object"&&i!=null&&"do"in i?i.do((n,s)=>r.set(n,s),()=>r.get(),r,(n,s,o)=>r.setDeferred(n,s,o)):null,p={};return S&&Object.keys(S).forEach(n=>Object.defineProperty(p,n,{get:()=>S[n]})),g&&Object.keys(g).forEach(n=>Object.defineProperty(p,n,{get:()=>g[n]})),p},O=e=>e,j=(e,r,i)=>r(e);class G{constructor(r,i){const g=t=>s=t,S=()=>s,p=new Set,n=t=>t(a());let s=r,o,h=()=>{},a=()=>S(),w=null,_=()=>{const t=K(r,d,i);return _=()=>t,t};const d=new Proxy(this,{get:(t,u)=>u==="do"?_():t[u],set:ee}),A=(t,u)=>{const b=typeof t=="function"?t(a()):t;if(!(b===a()||b===void 0||typeof b=="number"&&isNaN(b))){g(b),p.forEach(n,this);try{C.postMessage({key:l,value:S()})}catch{}u!==!0&&h(b)}};this.set=(t,u)=>A(t,u),this.get=()=>a(),this.initialValue=r,this.isInitialValue=()=>r===S(),this.subscribe=t=>(p.add(t),()=>{p.delete(t)}),this.reset=()=>{A(r,!0),p.forEach(n,this)};const y=(t,u)=>{A(t,u),o=void 0};if(this.setDeferred=(t,u=500,b,W=!0)=>{W&&o===void 0&&A(t,b),clearTimeout(o),o=setTimeout(y,u,t,b)},i==null)return d;let m=null,I=null,N=!0,V=!0,z=!1,R=-1,F=r instanceof Set?t=>new Set(t):t=>t,x=r 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 i=="string")m=i;else if("storeKey"in i)N=i.warnOnDuplicateStoreKey??N,V=i.listenStorageChanges??V,m=i.storeKey,F=i.unzipValue??F,x=i.zipValue??x,z=i.unchangable??z,I=i.exp??I;else return d;const l=`${H}${m}`,k=I===null||!(I(d,l in c)instanceof Date)?t=>JSON.stringify([x(t)]):t=>(w??(w={}),w.exp=I(d,l in c).getTime()+.2866,w.exp-Date.now()<24*60*60*1e3&&(clearTimeout(R),clearTimeout(L[l]),R=setTimeout(()=>this.reset(),w.exp-Date.now())),JSON.stringify([x(t),w])),B=t=>{const u=JSON.parse(t);return w=u[1],F(u[0])};let U=!0;if($[l]=d,c[`atom/${m}`]&&(c[l]||(c[l]=`[${c[`atom/${m}`]}]`),delete c[`atom/${m}`]),a=()=>{if(a=S,U){U=!1;try{g(l in c?B(c[l]):r)}catch{console.warn("Invalid json value",c[l])}}return S()},h=t=>{if(t===r){this.reset();return}c[l]=k(t)},this.reset=()=>{delete c[l],A(r,!0)},N&&D[l]!==void 0&&console.warn("Duplicate Atom key",m),V)if(z){let t=!1,u;P[l]=this,D[l]=()=>{clearTimeout(u),u=setTimeout(()=>t=!1,10),!t&&(t=!0,c[l]=k(S()))}}else D[l]=t=>{if(t.newValue===null){this.reset();return}try{A(B(t.newValue))}catch{console.warn("Invalid json value",t.newValue)}};return d}}let C;try{C=new BroadcastChannel("updateHere"),C.addEventListener("message",e=>{var r;(r=P[e.data.key])==null||r.set(e.data.value,!0)})}catch{}const c=window.localStorage,D={},P={},ee=(e,r)=>{throw`${r} is readonly property`};window.addEventListener("storage",e=>{var r;e.key===null||e.newValue===e.oldValue||(r=D[e.key])==null||r.call(D,e)});const te=c.setItem.bind(c),ne=c.removeItem.bind(c);c.setItem=(e,r)=>{P[e]===void 0&&te.call(c,e,r)},c.removeItem=e=>{P[e]===void 0&&ne.call(c,e)};const se=/"exp":(\d+)\.2866/,H="atom\\",$={},L={};setTimeout(()=>{Object.keys(c).forEach(e=>{var i;if(!e.startsWith(H)||typeof c[e]!="string")return;const r=+((i=c[e].match(se))==null?void 0:i[1]);r&&r-Date.now()<24*60*60*1e3&&(L[e]=setTimeout(()=>{$[e]?$[e].reset():delete c[e]},r-Date.now()))})},1e3),f.Atom=G,f.atom=v,f.configureAtomaric=q,f.configuredOptions=E,f.useAtom=Z,f.useAtomDo=Y,f.useAtomGet=X,f.useAtomSet=M,f.useAtomSetDeferred=Q,f.useAtomValue=J,Object.defineProperty(f,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "atomaric",
|
|
3
3
|
"description": "Manage your project state",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.36",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./build/atomaric.umd.cjs",
|
|
7
7
|
"module": "./build/atomaric.js",
|
|
8
|
-
"types": "./types/
|
|
8
|
+
"types": "./types/index.d.ts",
|
|
9
9
|
"exports": {
|
|
10
10
|
".": {
|
|
11
|
-
"types": "./types/
|
|
11
|
+
"types": "./types/index.d.ts",
|
|
12
12
|
"require": "./build/atomaric.umd.cjs",
|
|
13
13
|
"import": "./build/atomaric.js"
|
|
14
14
|
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { useSyncExternalStore } from 'react';
|
|
2
|
-
import { Path,
|
|
2
|
+
import { Path, TPathValue } from './paths';
|
|
3
|
+
|
|
4
|
+
export interface Register {}
|
|
3
5
|
|
|
4
6
|
type Sunscriber<Value> = (value: Value) => void;
|
|
5
7
|
|
|
@@ -66,8 +68,8 @@ export type ObjectActions<Value> = UpdateAction<Value> & {
|
|
|
66
68
|
/** pass partial value to update some deep values by flat path */
|
|
67
69
|
setDeepPartial: <
|
|
68
70
|
ValuePath extends Path<Value, Sep>,
|
|
69
|
-
Val extends
|
|
70
|
-
Sep extends string =
|
|
71
|
+
Val extends TPathValue<Value, Sep, ValuePath>,
|
|
72
|
+
Sep extends string = ObjectActionsSetDeepPartial,
|
|
71
73
|
>(
|
|
72
74
|
path: ValuePath,
|
|
73
75
|
value: Partial<Val> | ((value: Val) => Partial<Val>),
|
|
@@ -75,6 +77,12 @@ export type ObjectActions<Value> = UpdateAction<Value> & {
|
|
|
75
77
|
) => void;
|
|
76
78
|
};
|
|
77
79
|
|
|
80
|
+
export type ObjectActionsSetDeepPartial = Register extends {
|
|
81
|
+
keyPathSeparator: infer Separator extends string;
|
|
82
|
+
}
|
|
83
|
+
? Separator
|
|
84
|
+
: '.';
|
|
85
|
+
|
|
78
86
|
export type BooleanActions = {
|
|
79
87
|
/** toggle current value between true/false */
|
|
80
88
|
toggle: () => void;
|
|
@@ -159,4 +167,7 @@ export function atom<Value, Actions extends Record<string, Function> = {}>(
|
|
|
159
167
|
): Atom<Value, Actions>;
|
|
160
168
|
|
|
161
169
|
/** invoke this function before all atom usages */
|
|
162
|
-
export function configureAtomaric(options: {
|
|
170
|
+
export function configureAtomaric(options: {
|
|
171
|
+
useSyncExternalStore: typeof useSyncExternalStore;
|
|
172
|
+
keyPathSeparator: ObjectActionsSetDeepPartial;
|
|
173
|
+
}): void;
|
package/types/paths.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export type Path<T, Sep extends string> = T extends any ? PathInternal<T, Sep> : never;
|
|
2
|
-
|
|
2
|
+
export type PathValue<T, Sep extends string, ValuePath extends Path<T, Sep>> = TPathValue<T, Sep, ValuePath>;
|
|
3
3
|
|
|
4
4
|
/////////////////////////////////////////////////////////////////////////
|
|
5
5
|
/////////////////////////////////////////////////////////////////////////
|
|
@@ -20,7 +20,7 @@ type PathInternal<T, Sep extends string, TraversedTypes = T> = T extends Readonl
|
|
|
20
20
|
[K in keyof T]-?: PathImpl<K & string, Sep, T[K], TraversedTypes>;
|
|
21
21
|
}[keyof T];
|
|
22
22
|
|
|
23
|
-
|
|
23
|
+
type TupleKeys<T extends ReadonlyArray<any>> = Exclude<keyof T, keyof any[]>;
|
|
24
24
|
|
|
25
25
|
type PathImpl<K extends string | number, Sep extends string, V, TraversedTypes> = V extends
|
|
26
26
|
| Primitive
|
|
@@ -30,13 +30,13 @@ type PathImpl<K extends string | number, Sep extends string, V, TraversedTypes>
|
|
|
30
30
|
? `${K}`
|
|
31
31
|
: `${K}` | `${K}${Sep}${PathInternal<V, Sep, TraversedTypes | V>}`;
|
|
32
32
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
33
|
+
type ArrayKey = number;
|
|
34
|
+
type Primitive = null | undefined | string | number | boolean | symbol | bigint;
|
|
35
|
+
type IsTuple<T extends ReadonlyArray<any>> = number extends T['length'] ? false : true;
|
|
36
|
+
type BrowserNativeObject = Date | FileList | File;
|
|
37
37
|
|
|
38
38
|
type AnyIsEqual<T1, T2> = T1 extends T2 ? (IsEqual<T1, T2> extends true ? true : never) : never;
|
|
39
|
-
|
|
39
|
+
type IsEqual<T1, T2> = T1 extends T2
|
|
40
40
|
? (<G>() => G extends T1 ? 1 : 2) extends <G>() => G extends T2 ? 1 : 2
|
|
41
41
|
? true
|
|
42
42
|
: false
|
|
@@ -56,15 +56,15 @@ export type IsEqual<T1, T2> = T1 extends T2
|
|
|
56
56
|
////////////////////////////////////////////////////////////////////////////
|
|
57
57
|
////////////////////////////////////////////////////////////////////////////
|
|
58
58
|
|
|
59
|
-
|
|
59
|
+
type TPathValue<T, Sep extends string, P extends Path<T, Sep> | ArrayPath<T, Sep>> = T extends any
|
|
60
60
|
? P extends `${infer K}${Sep}${infer R}`
|
|
61
61
|
? K extends keyof T
|
|
62
62
|
? R extends Path<T[K], Sep>
|
|
63
|
-
?
|
|
63
|
+
? TPathValue<T[K], Sep, R>
|
|
64
64
|
: never
|
|
65
65
|
: K extends `${ArrayKey}`
|
|
66
66
|
? T extends ReadonlyArray<infer V>
|
|
67
|
-
?
|
|
67
|
+
? TPathValue<V, Sep, R & Path<V, Sep>>
|
|
68
68
|
: never
|
|
69
69
|
: never
|
|
70
70
|
: P extends keyof T
|
|
@@ -76,7 +76,7 @@ export type PathValue<T, Sep extends string, P extends Path<T, Sep> | ArrayPath<
|
|
|
76
76
|
: never
|
|
77
77
|
: never;
|
|
78
78
|
|
|
79
|
-
|
|
79
|
+
type ArrayPath<T, Sep extends string> = T extends any ? ArrayPathInternal<T, Sep> : never;
|
|
80
80
|
|
|
81
81
|
type ArrayPathInternal<T, Sep extends string, TraversedTypes = T> = T extends ReadonlyArray<infer V>
|
|
82
82
|
? IsTuple<T> extends true
|
|
@@ -106,4 +106,4 @@ type ArrayPathImpl<K extends string | number, Sep extends string, V, TraversedTy
|
|
|
106
106
|
? never
|
|
107
107
|
: `${K}${Sep}${ArrayPathInternal<V, Sep, TraversedTypes | V>}`;
|
|
108
108
|
|
|
109
|
-
|
|
109
|
+
type IsAny<T> = 0 extends 1 & T ? true : false;
|