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