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