atomaric 0.0.33 → 0.0.35
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 +72 -72
- 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,20 @@
|
|
|
1
|
-
const
|
|
2
|
-
let
|
|
3
|
-
typeof e == "number" ?
|
|
1
|
+
const B = (e, r, i) => {
|
|
2
|
+
let a = null;
|
|
3
|
+
typeof e == "number" ? a = _(
|
|
4
4
|
r,
|
|
5
5
|
(s) => ({
|
|
6
6
|
increment: (n) => {
|
|
7
7
|
s.set(+s.get() + (n ?? 0));
|
|
8
8
|
}
|
|
9
9
|
})
|
|
10
|
-
) : typeof e == "boolean" ?
|
|
10
|
+
) : typeof e == "boolean" ? a = _(
|
|
11
11
|
r,
|
|
12
12
|
(s) => ({
|
|
13
13
|
toggle: () => {
|
|
14
14
|
s.set(!s.get());
|
|
15
15
|
}
|
|
16
16
|
})
|
|
17
|
-
) : Array.isArray(e) ?
|
|
17
|
+
) : Array.isArray(e) ? a = _(
|
|
18
18
|
r,
|
|
19
19
|
(s) => ({
|
|
20
20
|
push: (...n) => {
|
|
@@ -28,14 +28,14 @@ const v = (e, r, l) => {
|
|
|
28
28
|
n(c), s.set(c);
|
|
29
29
|
},
|
|
30
30
|
filter: (n) => {
|
|
31
|
-
s.set(s.get().filter(n ??
|
|
31
|
+
s.set(s.get().filter(n ?? G));
|
|
32
32
|
},
|
|
33
33
|
toggle: (n, c) => {
|
|
34
34
|
const g = s.get().slice(), f = g.indexOf(n);
|
|
35
35
|
f < 0 ? c ? g.unshift(n) : g.push(n) : g.splice(f, 1), s.set(g);
|
|
36
36
|
}
|
|
37
37
|
})
|
|
38
|
-
) : e instanceof Set ?
|
|
38
|
+
) : e instanceof Set ? a = _(
|
|
39
39
|
r,
|
|
40
40
|
(s) => ({
|
|
41
41
|
add: (n) => {
|
|
@@ -57,7 +57,7 @@ const v = (e, r, l) => {
|
|
|
57
57
|
n(c), s.set(c);
|
|
58
58
|
}
|
|
59
59
|
})
|
|
60
|
-
) : e instanceof Object && (
|
|
60
|
+
) : e instanceof Object && (a = _(
|
|
61
61
|
r,
|
|
62
62
|
(s) => ({
|
|
63
63
|
setPartial: (n) => s.set((c) => ({
|
|
@@ -71,87 +71,87 @@ const v = (e, r, l) => {
|
|
|
71
71
|
setDeepPartial: (n, c, g = ".") => {
|
|
72
72
|
if (n.includes(g)) {
|
|
73
73
|
let f = n.split(g);
|
|
74
|
-
const
|
|
74
|
+
const w = f[f.length - 1];
|
|
75
75
|
f = f.slice(0, -1);
|
|
76
76
|
const D = { ...s.get() };
|
|
77
|
-
let
|
|
78
|
-
for (const
|
|
79
|
-
const A =
|
|
77
|
+
let d = D;
|
|
78
|
+
for (const S of f) {
|
|
79
|
+
const A = d[S];
|
|
80
80
|
if (A == null || typeof A != "object") {
|
|
81
|
-
s.do.setPartial({ [n]: c });
|
|
81
|
+
s.do.setPartial({ [n]: typeof c == "function" ? c(void 0) : c });
|
|
82
82
|
return;
|
|
83
83
|
}
|
|
84
|
-
|
|
84
|
+
d = d[S] = Array.isArray(A) ? [...A] : { ...A };
|
|
85
85
|
}
|
|
86
|
-
|
|
86
|
+
d[w] = typeof c == "function" ? c(d[w]) : c, s.set(D);
|
|
87
87
|
} else s.do.setPartial({ [n]: c });
|
|
88
88
|
}
|
|
89
89
|
})
|
|
90
90
|
));
|
|
91
|
-
const h = typeof
|
|
91
|
+
const h = typeof i == "object" && i != null && "do" in i ? i.do(
|
|
92
92
|
(s, n) => r.set(s, n),
|
|
93
93
|
() => r.get(),
|
|
94
94
|
r,
|
|
95
95
|
(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
|
|
102
|
-
constructor(r,
|
|
103
|
-
const
|
|
96
|
+
) : null, p = {};
|
|
97
|
+
return h && Object.keys(h).forEach((s) => Object.defineProperty(p, s, { get: () => h[s] })), a && Object.keys(a).forEach(
|
|
98
|
+
(s) => Object.defineProperty(p, s, { get: () => a[s] })
|
|
99
|
+
), p;
|
|
100
|
+
}, G = (e) => e, _ = (e, r, i) => r(e);
|
|
101
|
+
class U {
|
|
102
|
+
constructor(r, i) {
|
|
103
|
+
const a = (t) => n = t, h = () => n, p = /* @__PURE__ */ new Set(), s = (t) => t(f());
|
|
104
104
|
let n = r, c, g = () => {
|
|
105
|
-
}, f = () => h(),
|
|
106
|
-
const t =
|
|
105
|
+
}, f = () => h(), w = null, D = () => {
|
|
106
|
+
const t = B(r, d, i);
|
|
107
107
|
return D = () => t, t;
|
|
108
108
|
};
|
|
109
|
-
const
|
|
109
|
+
const d = new Proxy(this, {
|
|
110
110
|
get: (t, u) => u === "do" ? D() : t[u],
|
|
111
|
-
set:
|
|
112
|
-
}),
|
|
113
|
-
const
|
|
114
|
-
if (!(
|
|
115
|
-
|
|
111
|
+
set: W
|
|
112
|
+
}), S = (t, u) => {
|
|
113
|
+
const b = typeof t == "function" ? t(f()) : t;
|
|
114
|
+
if (!(b === f() || b === void 0 || typeof b == "number" && isNaN(b))) {
|
|
115
|
+
a(b), p.forEach(s, this);
|
|
116
116
|
try {
|
|
117
|
-
P.postMessage({ key:
|
|
117
|
+
P.postMessage({ key: l, value: h() });
|
|
118
118
|
} catch {
|
|
119
119
|
}
|
|
120
|
-
u !== !0 && g(
|
|
120
|
+
u !== !0 && g(b);
|
|
121
121
|
}
|
|
122
122
|
};
|
|
123
|
-
this.set = (t, u) =>
|
|
124
|
-
|
|
123
|
+
this.set = (t, u) => S(t, u), this.get = () => f(), this.initialValue = r, this.isInitialValue = () => r === h(), this.subscribe = (t) => (p.add(t), () => {
|
|
124
|
+
p.delete(t);
|
|
125
125
|
}), this.reset = () => {
|
|
126
|
-
|
|
126
|
+
S(r, !0), p.forEach(s, this);
|
|
127
127
|
};
|
|
128
128
|
const A = (t, u) => {
|
|
129
|
-
|
|
129
|
+
S(t, u), c = void 0;
|
|
130
130
|
};
|
|
131
|
-
if (this.setDeferred = (t, u = 500,
|
|
132
|
-
H && c === void 0 &&
|
|
133
|
-
},
|
|
131
|
+
if (this.setDeferred = (t, u = 500, b, H = !0) => {
|
|
132
|
+
H && c === void 0 && S(t, b), clearTimeout(c), c = setTimeout(A, u, t, b);
|
|
133
|
+
}, i == null) return d;
|
|
134
134
|
let y = null, T = null, I = !0, E = !0, C = !1, z = -1, $ = r instanceof Set ? (t) => new Set(t) : (t) => t, j = r instanceof Set ? (t) => {
|
|
135
135
|
if (t instanceof Set) return Array.from(t);
|
|
136
136
|
throw console.error(t), "The value is not Set instance";
|
|
137
137
|
} : (t) => t;
|
|
138
|
-
if (typeof
|
|
139
|
-
y =
|
|
140
|
-
else if ("storeKey" in
|
|
141
|
-
I =
|
|
142
|
-
else return
|
|
143
|
-
const
|
|
138
|
+
if (typeof i == "string")
|
|
139
|
+
y = i;
|
|
140
|
+
else if ("storeKey" in i)
|
|
141
|
+
I = i.warnOnDuplicateStoreKey ?? I, E = i.listenStorageChanges ?? E, y = i.storeKey, $ = i.unzipValue ?? $, j = i.zipValue ?? j, C = i.unchangable ?? C, T = i.exp ?? T;
|
|
142
|
+
else return d;
|
|
143
|
+
const l = `${L}${y}`, V = T === null || !(T(d, l in o) instanceof Date) ? (t) => JSON.stringify([j(t)]) : (t) => (w ?? (w = {}), w.exp = T(d, l in o).getTime() + 0.2866, w.exp - Date.now() < 24 * 60 * 60 * 1e3 && (clearTimeout(z), clearTimeout(M[l]), z = setTimeout(() => this.reset(), w.exp - Date.now())), JSON.stringify([j(t), w])), F = (t) => {
|
|
144
144
|
const u = JSON.parse(t);
|
|
145
|
-
return
|
|
145
|
+
return w = u[1], $(u[0]);
|
|
146
146
|
};
|
|
147
147
|
let J = !0;
|
|
148
|
-
if (N[
|
|
148
|
+
if (N[l] = d, o[`atom/${y}`] && (o[l] || (o[l] = `[${o[`atom/${y}`]}]`), delete o[`atom/${y}`]), f = () => {
|
|
149
149
|
if (f = h, J) {
|
|
150
150
|
J = !1;
|
|
151
151
|
try {
|
|
152
|
-
|
|
152
|
+
a(l in o ? F(o[l]) : r);
|
|
153
153
|
} catch {
|
|
154
|
-
console.warn("Invalid json value", o[
|
|
154
|
+
console.warn("Invalid json value", o[l]);
|
|
155
155
|
}
|
|
156
156
|
}
|
|
157
157
|
return h();
|
|
@@ -160,28 +160,28 @@ class G {
|
|
|
160
160
|
this.reset();
|
|
161
161
|
return;
|
|
162
162
|
}
|
|
163
|
-
o[
|
|
163
|
+
o[l] = V(t);
|
|
164
164
|
}, this.reset = () => {
|
|
165
|
-
delete o[
|
|
166
|
-
}, I && x[
|
|
165
|
+
delete o[l], S(r, !0);
|
|
166
|
+
}, I && x[l] !== void 0 && console.warn("Duplicate Atom key", y), E)
|
|
167
167
|
if (C) {
|
|
168
168
|
let t = !1, u;
|
|
169
|
-
m[
|
|
170
|
-
clearTimeout(u), u = setTimeout(() => t = !1, 10), !t && (t = !0, o[
|
|
169
|
+
m[l] = this, x[l] = () => {
|
|
170
|
+
clearTimeout(u), u = setTimeout(() => t = !1, 10), !t && (t = !0, o[l] = V(h()));
|
|
171
171
|
};
|
|
172
172
|
} else
|
|
173
|
-
x[
|
|
173
|
+
x[l] = (t) => {
|
|
174
174
|
if (t.newValue === null) {
|
|
175
175
|
this.reset();
|
|
176
176
|
return;
|
|
177
177
|
}
|
|
178
178
|
try {
|
|
179
|
-
|
|
179
|
+
S(F(t.newValue));
|
|
180
180
|
} catch {
|
|
181
181
|
console.warn("Invalid json value", t.newValue);
|
|
182
182
|
}
|
|
183
183
|
};
|
|
184
|
-
return
|
|
184
|
+
return d;
|
|
185
185
|
}
|
|
186
186
|
}
|
|
187
187
|
let P;
|
|
@@ -192,26 +192,26 @@ try {
|
|
|
192
192
|
});
|
|
193
193
|
} catch {
|
|
194
194
|
}
|
|
195
|
-
const o = window.localStorage, x = {}, m = {},
|
|
195
|
+
const o = window.localStorage, x = {}, m = {}, W = (e, r) => {
|
|
196
196
|
throw `${r} is readonly property`;
|
|
197
197
|
};
|
|
198
198
|
window.addEventListener("storage", (e) => {
|
|
199
199
|
var r;
|
|
200
200
|
e.key === null || e.newValue === e.oldValue || (r = x[e.key]) == null || r.call(x, e);
|
|
201
201
|
});
|
|
202
|
-
const
|
|
202
|
+
const q = o.setItem.bind(o), Q = o.removeItem.bind(o);
|
|
203
203
|
o.setItem = (e, r) => {
|
|
204
|
-
m[e] === void 0 &&
|
|
204
|
+
m[e] === void 0 && q.call(o, e, r);
|
|
205
205
|
};
|
|
206
206
|
o.removeItem = (e) => {
|
|
207
|
-
m[e] === void 0 &&
|
|
207
|
+
m[e] === void 0 && Q.call(o, e);
|
|
208
208
|
};
|
|
209
|
-
const
|
|
209
|
+
const X = /"exp":(\d+)\.2866/, L = "atom\\", N = {}, M = {};
|
|
210
210
|
setTimeout(() => {
|
|
211
211
|
Object.keys(o).forEach((e) => {
|
|
212
|
-
var
|
|
212
|
+
var i;
|
|
213
213
|
if (!e.startsWith(L) || typeof o[e] != "string") return;
|
|
214
|
-
const r = +((
|
|
214
|
+
const r = +((i = o[e].match(X)) == null ? void 0 : i[1]);
|
|
215
215
|
r && r - Date.now() < 24 * 60 * 60 * 1e3 && (M[e] = setTimeout(() => {
|
|
216
216
|
N[e] ? N[e].reset() : delete o[e];
|
|
217
217
|
}, r - Date.now()));
|
|
@@ -220,15 +220,15 @@ setTimeout(() => {
|
|
|
220
220
|
let R = () => {
|
|
221
221
|
throw "call configureAtomaric() before all!";
|
|
222
222
|
};
|
|
223
|
-
const
|
|
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
224
|
export {
|
|
225
|
-
|
|
225
|
+
U as Atom,
|
|
226
226
|
te as atom,
|
|
227
|
-
|
|
227
|
+
k as configureAtomaric,
|
|
228
228
|
ee as useAtom,
|
|
229
229
|
O as useAtomDo,
|
|
230
230
|
K as useAtomGet,
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
231
|
+
Z as useAtomSet,
|
|
232
|
+
v as useAtomSetDeferred,
|
|
233
|
+
Y as useAtomValue
|
|
234
234
|
};
|
package/build/atomaric.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(d,D){typeof exports=="object"&&typeof module<"u"?D(exports):typeof define=="function"&&define.amd?define(["exports"],D):(d=typeof globalThis<"u"?globalThis:d||self,D(d.atomaric={}))})(this,function(d){"use strict";const D=(e,r,i)=>{let g=null;typeof e=="number"?g=j(r,
|
|
1
|
+
(function(d,D){typeof exports=="object"&&typeof module<"u"?D(exports):typeof define=="function"&&define.amd?define(["exports"],D):(d=typeof globalThis<"u"?globalThis:d||self,D(d.atomaric={}))})(this,function(d){"use strict";const D=(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 c=n.get().slice();s(c),n.set(c)},filter:s=>{n.set(n.get().filter(s??Q))},toggle:(s,c)=>{const h=n.get().slice(),f=h.indexOf(s);f<0?c?h.unshift(s):h.push(s):h.splice(f,1),n.set(h)}})):e instanceof Set?g=j(r,n=>({add:s=>{n.set(new Set(n.get()).add(s))},delete:s=>{const c=new Set(n.get());c.delete(s),n.set(c)},toggle:s=>{const c=new Set(n.get());c.has(s)?c.delete(s):c.add(s),n.set(c)},clear:()=>{n.set(new Set)},update:s=>{const c=new Set(n.get());s(c),n.set(c)}})):e instanceof Object&&(g=j(r,n=>({setPartial:s=>n.set(c=>({...c,...typeof s=="function"?s(n.get()):s})),update:s=>{const c={...n.get()};s(c),n.set(c)},setDeepPartial:(s,c,h=".")=>{if(s.includes(h)){let f=s.split(h);const S=f[f.length-1];f=f.slice(0,-1);const _={...n.get()};let a=_;for(const A of f){const m=a[A];if(m==null||typeof m!="object"){n.do.setPartial({[s]:typeof c=="function"?c(void 0):c});return}a=a[A]=Array.isArray(m)?[...m]:{...m}}a[S]=typeof c=="function"?c(a[S]):c,n.set(_)}else n.do.setPartial({[s]:c})}})));const w=typeof i=="object"&&i!=null&&"do"in i?i.do((n,s)=>r.set(n,s),()=>r.get(),r,(n,s,c)=>r.setDeferred(n,s,c)):null,p={};return w&&Object.keys(w).forEach(n=>Object.defineProperty(p,n,{get:()=>w[n]})),g&&Object.keys(g).forEach(n=>Object.defineProperty(p,n,{get:()=>g[n]})),p},Q=e=>e,j=(e,r,i)=>r(e);class F{constructor(r,i){const g=t=>s=t,w=()=>s,p=new Set,n=t=>t(f());let s=r,c,h=()=>{},f=()=>w(),S=null,_=()=>{const t=D(r,a,i);return _=()=>t,t};const a=new Proxy(this,{get:(t,u)=>u==="do"?_():t[u],set:X}),A=(t,u)=>{const b=typeof t=="function"?t(f()):t;if(!(b===f()||b===void 0||typeof b=="number"&&isNaN(b))){g(b),p.forEach(n,this);try{C.postMessage({key:l,value:w()})}catch{}u!==!0&&h(b)}};this.set=(t,u)=>A(t,u),this.get=()=>f(),this.initialValue=r,this.isInitialValue=()=>r===w(),this.subscribe=t=>(p.add(t),()=>{p.delete(t)}),this.reset=()=>{A(r,!0),p.forEach(n,this)};const m=(t,u)=>{A(t,u),c=void 0};if(this.setDeferred=(t,u=500,b,q=!0)=>{q&&c===void 0&&A(t,b),clearTimeout(c),c=setTimeout(m,u,t,b)},i==null)return a;let y=null,I=null,$=!0,N=!0,V=!1,R=-1,z=r instanceof Set?t=>new Set(t):t=>t,E=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")y=i;else if("storeKey"in i)$=i.warnOnDuplicateStoreKey??$,N=i.listenStorageChanges??N,y=i.storeKey,z=i.unzipValue??z,E=i.zipValue??E,V=i.unchangable??V,I=i.exp??I;else return a;const l=`${J}${y}`,B=I===null||!(I(a,l in o)instanceof Date)?t=>JSON.stringify([E(t)]):t=>(S??(S={}),S.exp=I(a,l in o).getTime()+.2866,S.exp-Date.now()<24*60*60*1e3&&(clearTimeout(R),clearTimeout(M[l]),R=setTimeout(()=>this.reset(),S.exp-Date.now())),JSON.stringify([E(t),S])),U=t=>{const u=JSON.parse(t);return S=u[1],z(u[0])};let W=!0;if(P[l]=a,o[`atom/${y}`]&&(o[l]||(o[l]=`[${o[`atom/${y}`]}]`),delete o[`atom/${y}`]),f=()=>{if(f=w,W){W=!1;try{g(l in o?U(o[l]):r)}catch{console.warn("Invalid json value",o[l])}}return w()},h=t=>{if(t===r){this.reset();return}o[l]=B(t)},this.reset=()=>{delete o[l],A(r,!0)},$&&T[l]!==void 0&&console.warn("Duplicate Atom key",y),N)if(V){let t=!1,u;x[l]=this,T[l]=()=>{clearTimeout(u),u=setTimeout(()=>t=!1,10),!t&&(t=!0,o[l]=B(w()))}}else T[l]=t=>{if(t.newValue===null){this.reset();return}try{A(U(t.newValue))}catch{console.warn("Invalid json value",t.newValue)}};return a}}let C;try{C=new BroadcastChannel("updateHere"),C.addEventListener("message",e=>{var r;(r=x[e.data.key])==null||r.set(e.data.value,!0)})}catch{}const o=window.localStorage,T={},x={},X=(e,r)=>{throw`${r} is readonly property`};window.addEventListener("storage",e=>{var r;e.key===null||e.newValue===e.oldValue||(r=T[e.key])==null||r.call(T,e)});const Y=o.setItem.bind(o),Z=o.removeItem.bind(o);o.setItem=(e,r)=>{x[e]===void 0&&Y.call(o,e,r)},o.removeItem=e=>{x[e]===void 0&&Z.call(o,e)};const v=/"exp":(\d+)\.2866/,J="atom\\",P={},M={};setTimeout(()=>{Object.keys(o).forEach(e=>{var i;if(!e.startsWith(J)||typeof o[e]!="string")return;const r=+((i=o[e].match(v))==null?void 0:i[1]);r&&r-Date.now()<24*60*60*1e3&&(M[e]=setTimeout(()=>{P[e]?P[e].reset():delete o[e]},r-Date.now()))})},1e3);let G=()=>{throw"call configureAtomaric() before all!"};const k=e=>G=e.useSyncExternalStore,H=e=>G(e.subscribe,e.get),L=e=>e.set,K=e=>e.setDeferred,O=e=>e.get,ee=e=>e.do,te=e=>[H(e),L(e)],ne=(e,r)=>new F(e,r);d.Atom=F,d.atom=ne,d.configureAtomaric=k,d.useAtom=te,d.useAtomDo=ee,d.useAtomGet=O,d.useAtomSet=L,d.useAtomSetDeferred=K,d.useAtomValue=H,Object.defineProperty(d,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.35",
|
|
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;
|