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