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