atomaric 0.0.12 → 0.0.13

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