atomaric 0.0.38 → 0.0.39

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
@@ -4,35 +4,35 @@ let R = () => {
4
4
  const V = {}, K = (e) => {
5
5
  V.useSyncExternalStore = R = e.useSyncExternalStore, V.keyPathSeparator = e.keyPathSeparator;
6
6
  }, U = (e) => R(e.subscribe, e.get), W = (e) => e.set, ee = (e) => e.setDeferred, te = (e) => e.get, re = (e) => e.do, ne = (e) => [U(e), W(e)], se = (e, s) => new Y(e, s), q = (e, s) => {
7
- const o = (d, g, w) => new Proxy(d, {
7
+ const i = (d, g, w) => new Proxy(d, {
8
8
  get: (t, r, c) => {
9
- const l = w === 0 && s.get != null ? s.get(t, r, c) : t[r];
10
- return typeof l == "object" && l !== null ? o(
11
- Array.isArray(l) ? l.slice(0) : { ...l },
9
+ const o = w === 0 && s.get != null ? s.get(t, r, c) : t[r];
10
+ return typeof o == "object" && o !== null ? i(
11
+ Array.isArray(o) ? o.slice(0) : { ...o },
12
12
  g.concat(Array.isArray(t) ? +r : r),
13
13
  w + 1
14
- ) : l;
14
+ ) : o;
15
15
  },
16
16
  set: (t, r, c) => (s.onSet(t, g, r, c, t[r]) && (t[r] = c), !0)
17
17
  });
18
- return o(e, [], 0);
19
- }, Q = (e, s, o) => {
18
+ return i(e, [], 0);
19
+ }, Q = (e, s, i) => {
20
20
  let d = null;
21
- typeof e == "number" ? d = T(
21
+ typeof e == "number" ? d = j(
22
22
  s,
23
23
  (t) => ({
24
24
  increment: (r) => {
25
25
  t.set(+t.get() + (r ?? 0));
26
26
  }
27
27
  })
28
- ) : typeof e == "boolean" ? d = T(
28
+ ) : typeof e == "boolean" ? d = j(
29
29
  s,
30
30
  (t) => ({
31
31
  toggle: () => {
32
32
  t.set(!t.get());
33
33
  }
34
34
  })
35
- ) : Array.isArray(e) ? d = T(
35
+ ) : Array.isArray(e) ? d = j(
36
36
  s,
37
37
  (t) => ({
38
38
  push: (...r) => {
@@ -42,18 +42,24 @@ const V = {}, K = (e) => {
42
42
  t.set(r.concat(t.get()));
43
43
  },
44
44
  update: (r) => {
45
- const c = t.get(), l = M(c, r);
46
- l !== c && t.set(l);
45
+ const c = t.get(), o = M(c, r);
46
+ o !== c && t.set(o);
47
47
  },
48
48
  filter: (r) => {
49
49
  t.set(t.get().filter(r ?? X));
50
50
  },
51
+ remove: (r) => {
52
+ const c = t.get().indexOf(r);
53
+ if (c < 0) return;
54
+ const o = t.get().slice(0);
55
+ o.splice(c, 1), t.set(o);
56
+ },
51
57
  toggle: (r, c) => {
52
- const l = t.get().slice(), f = l.indexOf(r);
53
- f < 0 ? c ? l.unshift(r) : l.push(r) : l.splice(f, 1), t.set(l);
58
+ const o = t.get().slice(), f = o.indexOf(r);
59
+ f < 0 ? c ? o.unshift(r) : o.push(r) : o.splice(f, 1), t.set(o);
54
60
  }
55
61
  })
56
- ) : e instanceof Set ? d = T(
62
+ ) : e instanceof Set ? d = j(
57
63
  s,
58
64
  (t) => ({
59
65
  add: (r) => {
@@ -71,7 +77,7 @@ const V = {}, K = (e) => {
71
77
  t.set(/* @__PURE__ */ new Set());
72
78
  }
73
79
  })
74
- ) : e instanceof Object && (d = T(
80
+ ) : e instanceof Object && (d = j(
75
81
  s,
76
82
  (t) => ({
77
83
  setPartial: (r) => t.set((c) => ({
@@ -79,31 +85,31 @@ const V = {}, K = (e) => {
79
85
  ...typeof r == "function" ? r(t.get()) : r
80
86
  })),
81
87
  update: (r) => {
82
- const c = t.get(), l = M(c, r);
83
- l !== c && t.set(l);
88
+ const c = t.get(), o = M(c, r);
89
+ o !== c && t.set(o);
84
90
  },
85
- setDeepPartial: (r, c, l, f = V.keyPathSeparator ?? ".") => {
91
+ setDeepPartial: (r, c, o, f = V.keyPathSeparator ?? ".") => {
86
92
  if (r.includes(f)) {
87
93
  let h = r.split(f);
88
94
  const b = h[h.length - 1];
89
95
  h = h.slice(0, -1);
90
96
  const y = { ...t.get() };
91
- let S = y, m = l;
92
- for (const A of h) {
93
- let p = S[A];
94
- const _ = m = m == null ? void 0 : m[Array.isArray(m) ? "0" : A];
95
- if (p == null && (p = Array.isArray(_) ? [] : {}), p == null || typeof p != "object") {
97
+ let A = y, D = o;
98
+ for (const S of h) {
99
+ let p = A[S];
100
+ const T = D = D == null ? void 0 : D[Array.isArray(D) ? "0" : S];
101
+ if (p == null && (p = Array.isArray(T) ? [] : {}), p == null || typeof p != "object") {
96
102
  t.do.setPartial({ [r]: typeof c == "function" ? c(void 0) : c });
97
103
  return;
98
104
  }
99
- S = S[A] = Array.isArray(p) ? [...p] : { ...p };
105
+ A = A[S] = Array.isArray(p) ? [...p] : { ...p };
100
106
  }
101
- S[b] = typeof c == "function" ? c(S[b]) : c, t.set(y);
107
+ A[b] = typeof c == "function" ? c(A[b]) : c, t.set(y);
102
108
  } else t.do.setPartial({ [r]: c });
103
109
  }
104
110
  })
105
111
  ));
106
- const g = typeof o == "object" && o != null && "do" in o ? o.do(
112
+ const g = typeof i == "object" && i != null && "do" in i ? i.do(
107
113
  (t, r) => s.set(t, r),
108
114
  () => s.get(),
109
115
  s,
@@ -112,13 +118,13 @@ const V = {}, K = (e) => {
112
118
  return g && Object.keys(g).forEach((t) => Object.defineProperty(w, t, { get: () => g[t] })), d && Object.keys(d).forEach(
113
119
  (t) => Object.defineProperty(w, t, { get: () => d[t] })
114
120
  ), w;
115
- }, X = (e) => e, T = (e, s, o) => s(e), M = (e, s) => {
116
- const o = Array.isArray(e) ? e.slice(0) : { ...e };
121
+ }, X = (e) => e, j = (e, s, i) => s(e), M = (e, s) => {
122
+ const i = Array.isArray(e) ? e.slice(0) : { ...e };
117
123
  let d = !1;
118
124
  const g = q(e, {
119
- onSet: (w, t, r, c, l) => {
120
- if (c === l) return !0;
121
- let f = o;
125
+ onSet: (w, t, r, c, o) => {
126
+ if (c === o) return !0;
127
+ let f = i;
122
128
  d = !0;
123
129
  for (const h of t) {
124
130
  const b = f[h];
@@ -127,20 +133,20 @@ const V = {}, K = (e) => {
127
133
  return f[r] = c, !0;
128
134
  }
129
135
  });
130
- return s(g), d ? o : e;
136
+ return s(g), d ? i : e;
131
137
  };
132
138
  class Y {
133
- constructor(s, o) {
139
+ constructor(s, i) {
134
140
  const d = (n) => r = n, g = () => r, w = /* @__PURE__ */ new Set(), t = (n) => n(f());
135
- let r = s, c, l = () => {
141
+ let r = s, c, o = () => {
136
142
  }, f = () => g(), h = null, b = () => {
137
- const n = Q(s, y, o);
143
+ const n = Q(s, y, i);
138
144
  return b = () => n, n;
139
145
  };
140
146
  const y = new Proxy(this, {
141
147
  get: (n, a) => a === "do" ? b() : n[a],
142
148
  set: Z
143
- }), S = (n, a) => {
149
+ }), A = (n, a) => {
144
150
  const x = typeof n == "function" ? n(f()) : n;
145
151
  if (!(x === f() || x === void 0 || typeof x == "number" && isNaN(x))) {
146
152
  d(x), w.forEach(t, this);
@@ -148,66 +154,66 @@ class Y {
148
154
  $.postMessage({ key: u, value: g() });
149
155
  } catch {
150
156
  }
151
- a !== !0 && l(x);
157
+ a !== !0 && o(x);
152
158
  }
153
159
  };
154
- this.set = (n, a) => S(n, a), this.get = () => f(), this.initialValue = s, this.isInitialValue = () => s === g(), this.subscribe = (n) => (w.add(n), () => {
160
+ this.set = (n, a) => A(n, a), this.get = () => f(), this.initialValue = s, this.isInitialValue = () => s === g(), this.subscribe = (n) => (w.add(n), () => {
155
161
  w.delete(n);
156
162
  }), this.reset = () => {
157
- S(s, !0), w.forEach(t, this);
163
+ A(s, !0), w.forEach(t, this);
158
164
  };
159
- const m = (n, a) => {
160
- S(n, a), c = void 0;
165
+ const D = (n, a) => {
166
+ A(n, a), c = void 0;
161
167
  };
162
168
  if (this.setDeferred = (n, a = 500, x, L = !0) => {
163
- L && c === void 0 && S(n, x), clearTimeout(c), c = setTimeout(m, a, n, x);
164
- }, o == null) return y;
165
- let A = null, p = null, _ = !0, P = !0, E = !1, z = -1, C = s instanceof Set ? (n) => new Set(n) : (n) => n, j = s instanceof Set ? (n) => {
169
+ L && c === void 0 && A(n, x), clearTimeout(c), c = setTimeout(D, a, n, x);
170
+ }, i == null) return y;
171
+ let S = null, p = null, T = !0, P = !0, E = !1, z = -1, C = s instanceof Set ? (n) => new Set(n) : (n) => n, m = s instanceof Set ? (n) => {
166
172
  if (n instanceof Set) return Array.from(n);
167
173
  throw console.error(n), "The value is not Set instance";
168
174
  } : (n) => n;
169
- if (typeof o == "string")
170
- A = o;
171
- else if ("storeKey" in o)
172
- _ = o.warnOnDuplicateStoreKey ?? _, P = o.listenStorageChanges ?? P, A = o.storeKey, C = o.unzipValue ?? C, j = o.zipValue ?? j, E = o.unchangable ?? E, p = o.exp ?? p;
175
+ if (typeof i == "string")
176
+ S = i;
177
+ else if ("storeKey" in i)
178
+ T = i.warnOnDuplicateStoreKey ?? T, P = i.listenStorageChanges ?? P, S = i.storeKey, C = i.unzipValue ?? C, m = i.zipValue ?? m, E = i.unchangable ?? E, p = i.exp ?? p;
173
179
  else return y;
174
- const u = `${B}${A}`, F = p === null || !(p(y, u in i) instanceof Date) ? (n) => JSON.stringify([j(n)]) : (n) => (h ?? (h = {}), h.exp = p(y, u in i).getTime() + 0.2866, h.exp - Date.now() < 24 * 60 * 60 * 1e3 && (clearTimeout(z), clearTimeout(G[u]), z = setTimeout(() => this.reset(), h.exp - Date.now())), JSON.stringify([j(n), h])), J = (n) => {
180
+ const u = `${B}${S}`, F = p === null || !(p(y, u in l) instanceof Date) ? (n) => JSON.stringify([m(n)]) : (n) => (h ?? (h = {}), h.exp = p(y, u in l).getTime() + 0.2866, h.exp - Date.now() < 24 * 60 * 60 * 1e3 && (clearTimeout(z), clearTimeout(G[u]), z = setTimeout(() => this.reset(), h.exp - Date.now())), JSON.stringify([m(n), h])), J = (n) => {
175
181
  const a = JSON.parse(n);
176
182
  return h = a[1], C(a[0]);
177
183
  };
178
184
  let H = !0;
179
- if (N[u] = y, i[`atom/${A}`] && (i[u] || (i[u] = `[${i[`atom/${A}`]}]`), delete i[`atom/${A}`]), f = () => {
185
+ if (N[u] = y, l[`atom/${S}`] && (l[u] || (l[u] = `[${l[`atom/${S}`]}]`), delete l[`atom/${S}`]), f = () => {
180
186
  if (f = g, H) {
181
187
  H = !1;
182
188
  try {
183
- d(u in i ? J(i[u]) : s);
189
+ d(u in l ? J(l[u]) : s);
184
190
  } catch {
185
- console.warn("Invalid json value", i[u]);
191
+ console.warn("Invalid json value", l[u]);
186
192
  }
187
193
  }
188
194
  return g();
189
- }, l = (n) => {
195
+ }, o = (n) => {
190
196
  if (n === s) {
191
197
  this.reset();
192
198
  return;
193
199
  }
194
- i[u] = F(n);
200
+ l[u] = F(n);
195
201
  }, this.reset = () => {
196
- delete i[u], S(s, !0);
197
- }, _ && D[u] !== void 0 && console.warn("Duplicate Atom key", A), P)
202
+ delete l[u], A(s, !0);
203
+ }, T && _[u] !== void 0 && console.warn("Duplicate Atom key", S), P)
198
204
  if (E) {
199
205
  let n = !1, a;
200
- I[u] = this, D[u] = () => {
201
- clearTimeout(a), a = setTimeout(() => n = !1, 10), !n && (n = !0, i[u] = F(g()));
206
+ I[u] = this, _[u] = () => {
207
+ clearTimeout(a), a = setTimeout(() => n = !1, 10), !n && (n = !0, l[u] = F(g()));
202
208
  };
203
209
  } else
204
- D[u] = (n) => {
210
+ _[u] = (n) => {
205
211
  if (n.newValue === null) {
206
212
  this.reset();
207
213
  return;
208
214
  }
209
215
  try {
210
- S(J(n.newValue));
216
+ A(J(n.newValue));
211
217
  } catch {
212
218
  console.warn("Invalid json value", n.newValue);
213
219
  }
@@ -223,28 +229,28 @@ try {
223
229
  });
224
230
  } catch {
225
231
  }
226
- const i = window.localStorage, D = {}, I = {}, Z = (e, s) => {
232
+ const l = window.localStorage, _ = {}, I = {}, Z = (e, s) => {
227
233
  throw `${s} is readonly property`;
228
234
  };
229
235
  window.addEventListener("storage", (e) => {
230
236
  var s;
231
- e.key === null || e.newValue === e.oldValue || (s = D[e.key]) == null || s.call(D, e);
237
+ e.key === null || e.newValue === e.oldValue || (s = _[e.key]) == null || s.call(_, e);
232
238
  });
233
- const v = i.setItem.bind(i), k = i.removeItem.bind(i);
234
- i.setItem = (e, s) => {
235
- I[e] === void 0 && v.call(i, e, s);
239
+ const v = l.setItem.bind(l), k = l.removeItem.bind(l);
240
+ l.setItem = (e, s) => {
241
+ I[e] === void 0 && v.call(l, e, s);
236
242
  };
237
- i.removeItem = (e) => {
238
- I[e] === void 0 && k.call(i, e);
243
+ l.removeItem = (e) => {
244
+ I[e] === void 0 && k.call(l, e);
239
245
  };
240
246
  const O = /"exp":(\d+)\.2866/, B = "atom\\", N = {}, G = {};
241
247
  setTimeout(() => {
242
- Object.keys(i).forEach((e) => {
243
- var o;
244
- if (!e.startsWith(B) || typeof i[e] != "string") return;
245
- const s = +((o = i[e].match(O)) == null ? void 0 : o[1]);
248
+ Object.keys(l).forEach((e) => {
249
+ var i;
250
+ if (!e.startsWith(B) || typeof l[e] != "string") return;
251
+ const s = +((i = l[e].match(O)) == null ? void 0 : i[1]);
246
252
  s && s - Date.now() < 24 * 60 * 60 * 1e3 && (G[e] = setTimeout(() => {
247
- N[e] ? N[e].reset() : delete i[e];
253
+ N[e] ? N[e].reset() : delete l[e];
248
254
  }, s - Date.now()));
249
255
  });
250
256
  }, 1e3);
@@ -1 +1 @@
1
- (function(g,j){typeof exports=="object"&&typeof module<"u"?j(exports):typeof define=="function"&&define.amd?define(["exports"],j):(g=typeof globalThis<"u"?globalThis:g||self,j(g.atomaric={}))})(this,function(g){"use strict";let j=()=>{throw"call configureAtomaric() before all!"};const I={},X=e=>{I.useSyncExternalStore=j=e.useSyncExternalStore,I.keyPathSeparator=e.keyPathSeparator},J=e=>j(e.subscribe,e.get),M=e=>e.set,Y=e=>e.setDeferred,Z=e=>e.get,v=e=>e.do,k=e=>[J(e),M(e)],O=(e,r)=>new H(e,r),K=(e,r)=>{const c=(d,h,w)=>new Proxy(d,{get:(t,n,o)=>{const u=w===0&&r.get!=null?r.get(t,n,o):t[n];return typeof u=="object"&&u!==null?c(Array.isArray(u)?u.slice(0):{...u},h.concat(Array.isArray(t)?+n:n),w+1):u},set:(t,n,o)=>(r.onSet(t,h,n,o,t[n])&&(t[n]=o),!0)});return c(e,[],0)},ee=(e,r,c)=>{let d=null;typeof e=="number"?d=x(r,t=>({increment:n=>{t.set(+t.get()+(n??0))}})):typeof e=="boolean"?d=x(r,t=>({toggle:()=>{t.set(!t.get())}})):Array.isArray(e)?d=x(r,t=>({push:(...n)=>{t.set(t.get().concat(n))},unshift:(...n)=>{t.set(n.concat(t.get()))},update:n=>{const o=t.get(),u=G(o,n);u!==o&&t.set(u)},filter:n=>{t.set(t.get().filter(n??te))},toggle:(n,o)=>{const u=t.get().slice(),f=u.indexOf(n);f<0?o?u.unshift(n):u.push(n):u.splice(f,1),t.set(u)}})):e instanceof Set?d=x(r,t=>({add:n=>{t.set(new Set(t.get()).add(n))},delete:n=>{const o=new Set(t.get());o.delete(n),t.set(o)},toggle:n=>{const o=new Set(t.get());o.has(n)?o.delete(n):o.add(n),t.set(o)},clear:()=>{t.set(new Set)}})):e instanceof Object&&(d=x(r,t=>({setPartial:n=>t.set(o=>({...o,...typeof n=="function"?n(t.get()):n})),update:n=>{const o=t.get(),u=G(o,n);u!==o&&t.set(u)},setDeepPartial:(n,o,u,f=I.keyPathSeparator??".")=>{if(n.includes(f)){let A=n.split(f);const m=A[A.length-1];A=A.slice(0,-1);const b={...t.get()};let y=b,T=u;for(const p of A){let S=y[p];const P=T=T==null?void 0:T[Array.isArray(T)?"0":p];if(S==null&&(S=Array.isArray(P)?[]:{}),S==null||typeof S!="object"){t.do.setPartial({[n]:typeof o=="function"?o(void 0):o});return}y=y[p]=Array.isArray(S)?[...S]:{...S}}y[m]=typeof o=="function"?o(y[m]):o,t.set(b)}else t.do.setPartial({[n]:o})}})));const h=typeof c=="object"&&c!=null&&"do"in c?c.do((t,n)=>r.set(t,n),()=>r.get(),r,(t,n,o)=>r.setDeferred(t,n,o)):null,w={};return h&&Object.keys(h).forEach(t=>Object.defineProperty(w,t,{get:()=>h[t]})),d&&Object.keys(d).forEach(t=>Object.defineProperty(w,t,{get:()=>d[t]})),w},te=e=>e,x=(e,r,c)=>r(e),G=(e,r)=>{const c=Array.isArray(e)?e.slice(0):{...e};let d=!1;const h=K(e,{onSet:(w,t,n,o,u)=>{if(o===u)return!0;let f=c;d=!0;for(const A of t){const m=f[A];f=f[A]=Array.isArray(m)?m.slice(0):{...m}}return f[n]=o,!0}});return r(h),d?c:e};class H{constructor(r,c){const d=s=>n=s,h=()=>n,w=new Set,t=s=>s(f());let n=r,o,u=()=>{},f=()=>h(),A=null,m=()=>{const s=ee(r,b,c);return m=()=>s,s};const b=new Proxy(this,{get:(s,a)=>a==="do"?m():s[a],set:ne}),y=(s,a)=>{const D=typeof s=="function"?s(f()):s;if(!(D===f()||D===void 0||typeof D=="number"&&isNaN(D))){d(D),w.forEach(t,this);try{C.postMessage({key:l,value:h()})}catch{}a!==!0&&u(D)}};this.set=(s,a)=>y(s,a),this.get=()=>f(),this.initialValue=r,this.isInitialValue=()=>r===h(),this.subscribe=s=>(w.add(s),()=>{w.delete(s)}),this.reset=()=>{y(r,!0),w.forEach(t,this)};const T=(s,a)=>{y(s,a),o=void 0};if(this.setDeferred=(s,a=500,D,Q=!0)=>{Q&&o===void 0&&y(s,D),clearTimeout(o),o=setTimeout(T,a,s,D)},c==null)return b;let p=null,S=null,P=!0,N=!0,z=!1,B=-1,F=r instanceof Set?s=>new Set(s):s=>s,V=r instanceof Set?s=>{if(s instanceof Set)return Array.from(s);throw console.error(s),"The value is not Set instance"}:s=>s;if(typeof c=="string")p=c;else if("storeKey"in c)P=c.warnOnDuplicateStoreKey??P,N=c.listenStorageChanges??N,p=c.storeKey,F=c.unzipValue??F,V=c.zipValue??V,z=c.unchangable??z,S=c.exp??S;else return b;const l=`${L}${p}`,U=S===null||!(S(b,l in i)instanceof Date)?s=>JSON.stringify([V(s)]):s=>(A??(A={}),A.exp=S(b,l in i).getTime()+.2866,A.exp-Date.now()<24*60*60*1e3&&(clearTimeout(B),clearTimeout(R[l]),B=setTimeout(()=>this.reset(),A.exp-Date.now())),JSON.stringify([V(s),A])),W=s=>{const a=JSON.parse(s);return A=a[1],F(a[0])};let q=!0;if($[l]=b,i[`atom/${p}`]&&(i[l]||(i[l]=`[${i[`atom/${p}`]}]`),delete i[`atom/${p}`]),f=()=>{if(f=h,q){q=!1;try{d(l in i?W(i[l]):r)}catch{console.warn("Invalid json value",i[l])}}return h()},u=s=>{if(s===r){this.reset();return}i[l]=U(s)},this.reset=()=>{delete i[l],y(r,!0)},P&&_[l]!==void 0&&console.warn("Duplicate Atom key",p),N)if(z){let s=!1,a;E[l]=this,_[l]=()=>{clearTimeout(a),a=setTimeout(()=>s=!1,10),!s&&(s=!0,i[l]=U(h()))}}else _[l]=s=>{if(s.newValue===null){this.reset();return}try{y(W(s.newValue))}catch{console.warn("Invalid json value",s.newValue)}};return b}}let C;try{C=new BroadcastChannel("updateHere"),C.addEventListener("message",e=>{var r;(r=E[e.data.key])==null||r.set(e.data.value,!0)})}catch{}const i=window.localStorage,_={},E={},ne=(e,r)=>{throw`${r} is readonly property`};window.addEventListener("storage",e=>{var r;e.key===null||e.newValue===e.oldValue||(r=_[e.key])==null||r.call(_,e)});const se=i.setItem.bind(i),re=i.removeItem.bind(i);i.setItem=(e,r)=>{E[e]===void 0&&se.call(i,e,r)},i.removeItem=e=>{E[e]===void 0&&re.call(i,e)};const oe=/"exp":(\d+)\.2866/,L="atom\\",$={},R={};setTimeout(()=>{Object.keys(i).forEach(e=>{var c;if(!e.startsWith(L)||typeof i[e]!="string")return;const r=+((c=i[e].match(oe))==null?void 0:c[1]);r&&r-Date.now()<24*60*60*1e3&&(R[e]=setTimeout(()=>{$[e]?$[e].reset():delete i[e]},r-Date.now()))})},1e3),g.Atom=H,g.atom=O,g.configureAtomaric=X,g.configuredOptions=I,g.useAtom=k,g.useAtomDo=v,g.useAtomGet=Z,g.useAtomSet=M,g.useAtomSetDeferred=Y,g.useAtomValue=J,Object.defineProperty(g,Symbol.toStringTag,{value:"Module"})});
1
+ (function(g,j){typeof exports=="object"&&typeof module<"u"?j(exports):typeof define=="function"&&define.amd?define(["exports"],j):(g=typeof globalThis<"u"?globalThis:g||self,j(g.atomaric={}))})(this,function(g){"use strict";let j=()=>{throw"call configureAtomaric() before all!"};const I={},Q=e=>{I.useSyncExternalStore=j=e.useSyncExternalStore,I.keyPathSeparator=e.keyPathSeparator},J=e=>j(e.subscribe,e.get),M=e=>e.set,X=e=>e.setDeferred,Y=e=>e.get,Z=e=>e.do,k=e=>[J(e),M(e)],O=(e,r)=>new H(e,r),K=(e,r)=>{const i=(d,h,w)=>new Proxy(d,{get:(t,n,c)=>{const o=w===0&&r.get!=null?r.get(t,n,c):t[n];return typeof o=="object"&&o!==null?i(Array.isArray(o)?o.slice(0):{...o},h.concat(Array.isArray(t)?+n:n),w+1):o},set:(t,n,c)=>(r.onSet(t,h,n,c,t[n])&&(t[n]=c),!0)});return i(e,[],0)},ee=(e,r,i)=>{let d=null;typeof e=="number"?d=x(r,t=>({increment:n=>{t.set(+t.get()+(n??0))}})):typeof e=="boolean"?d=x(r,t=>({toggle:()=>{t.set(!t.get())}})):Array.isArray(e)?d=x(r,t=>({push:(...n)=>{t.set(t.get().concat(n))},unshift:(...n)=>{t.set(n.concat(t.get()))},update:n=>{const c=t.get(),o=G(c,n);o!==c&&t.set(o)},filter:n=>{t.set(t.get().filter(n??te))},remove:n=>{const c=t.get().indexOf(n);if(c<0)return;const o=t.get().slice(0);o.splice(c,1),t.set(o)},toggle:(n,c)=>{const o=t.get().slice(),f=o.indexOf(n);f<0?c?o.unshift(n):o.push(n):o.splice(f,1),t.set(o)}})):e instanceof Set?d=x(r,t=>({add:n=>{t.set(new Set(t.get()).add(n))},delete:n=>{const c=new Set(t.get());c.delete(n),t.set(c)},toggle:n=>{const c=new Set(t.get());c.has(n)?c.delete(n):c.add(n),t.set(c)},clear:()=>{t.set(new Set)}})):e instanceof Object&&(d=x(r,t=>({setPartial:n=>t.set(c=>({...c,...typeof n=="function"?n(t.get()):n})),update:n=>{const c=t.get(),o=G(c,n);o!==c&&t.set(o)},setDeepPartial:(n,c,o,f=I.keyPathSeparator??".")=>{if(n.includes(f)){let A=n.split(f);const m=A[A.length-1];A=A.slice(0,-1);const b={...t.get()};let y=b,T=o;for(const p of A){let S=y[p];const P=T=T==null?void 0:T[Array.isArray(T)?"0":p];if(S==null&&(S=Array.isArray(P)?[]:{}),S==null||typeof S!="object"){t.do.setPartial({[n]:typeof c=="function"?c(void 0):c});return}y=y[p]=Array.isArray(S)?[...S]:{...S}}y[m]=typeof c=="function"?c(y[m]):c,t.set(b)}else t.do.setPartial({[n]:c})}})));const h=typeof i=="object"&&i!=null&&"do"in i?i.do((t,n)=>r.set(t,n),()=>r.get(),r,(t,n,c)=>r.setDeferred(t,n,c)):null,w={};return h&&Object.keys(h).forEach(t=>Object.defineProperty(w,t,{get:()=>h[t]})),d&&Object.keys(d).forEach(t=>Object.defineProperty(w,t,{get:()=>d[t]})),w},te=e=>e,x=(e,r,i)=>r(e),G=(e,r)=>{const i=Array.isArray(e)?e.slice(0):{...e};let d=!1;const h=K(e,{onSet:(w,t,n,c,o)=>{if(c===o)return!0;let f=i;d=!0;for(const A of t){const m=f[A];f=f[A]=Array.isArray(m)?m.slice(0):{...m}}return f[n]=c,!0}});return r(h),d?i:e};class H{constructor(r,i){const d=s=>n=s,h=()=>n,w=new Set,t=s=>s(f());let n=r,c,o=()=>{},f=()=>h(),A=null,m=()=>{const s=ee(r,b,i);return m=()=>s,s};const b=new Proxy(this,{get:(s,a)=>a==="do"?m():s[a],set:ne}),y=(s,a)=>{const D=typeof s=="function"?s(f()):s;if(!(D===f()||D===void 0||typeof D=="number"&&isNaN(D))){d(D),w.forEach(t,this);try{C.postMessage({key:l,value:h()})}catch{}a!==!0&&o(D)}};this.set=(s,a)=>y(s,a),this.get=()=>f(),this.initialValue=r,this.isInitialValue=()=>r===h(),this.subscribe=s=>(w.add(s),()=>{w.delete(s)}),this.reset=()=>{y(r,!0),w.forEach(t,this)};const T=(s,a)=>{y(s,a),c=void 0};if(this.setDeferred=(s,a=500,D,v=!0)=>{v&&c===void 0&&y(s,D),clearTimeout(c),c=setTimeout(T,a,s,D)},i==null)return b;let p=null,S=null,P=!0,N=!0,z=!1,B=-1,F=r instanceof Set?s=>new Set(s):s=>s,V=r instanceof Set?s=>{if(s instanceof Set)return Array.from(s);throw console.error(s),"The value is not Set instance"}:s=>s;if(typeof i=="string")p=i;else if("storeKey"in i)P=i.warnOnDuplicateStoreKey??P,N=i.listenStorageChanges??N,p=i.storeKey,F=i.unzipValue??F,V=i.zipValue??V,z=i.unchangable??z,S=i.exp??S;else return b;const l=`${L}${p}`,U=S===null||!(S(b,l in u)instanceof Date)?s=>JSON.stringify([V(s)]):s=>(A??(A={}),A.exp=S(b,l in u).getTime()+.2866,A.exp-Date.now()<24*60*60*1e3&&(clearTimeout(B),clearTimeout(R[l]),B=setTimeout(()=>this.reset(),A.exp-Date.now())),JSON.stringify([V(s),A])),W=s=>{const a=JSON.parse(s);return A=a[1],F(a[0])};let q=!0;if($[l]=b,u[`atom/${p}`]&&(u[l]||(u[l]=`[${u[`atom/${p}`]}]`),delete u[`atom/${p}`]),f=()=>{if(f=h,q){q=!1;try{d(l in u?W(u[l]):r)}catch{console.warn("Invalid json value",u[l])}}return h()},o=s=>{if(s===r){this.reset();return}u[l]=U(s)},this.reset=()=>{delete u[l],y(r,!0)},P&&_[l]!==void 0&&console.warn("Duplicate Atom key",p),N)if(z){let s=!1,a;E[l]=this,_[l]=()=>{clearTimeout(a),a=setTimeout(()=>s=!1,10),!s&&(s=!0,u[l]=U(h()))}}else _[l]=s=>{if(s.newValue===null){this.reset();return}try{y(W(s.newValue))}catch{console.warn("Invalid json value",s.newValue)}};return b}}let C;try{C=new BroadcastChannel("updateHere"),C.addEventListener("message",e=>{var r;(r=E[e.data.key])==null||r.set(e.data.value,!0)})}catch{}const u=window.localStorage,_={},E={},ne=(e,r)=>{throw`${r} is readonly property`};window.addEventListener("storage",e=>{var r;e.key===null||e.newValue===e.oldValue||(r=_[e.key])==null||r.call(_,e)});const se=u.setItem.bind(u),re=u.removeItem.bind(u);u.setItem=(e,r)=>{E[e]===void 0&&se.call(u,e,r)},u.removeItem=e=>{E[e]===void 0&&re.call(u,e)};const ce=/"exp":(\d+)\.2866/,L="atom\\",$={},R={};setTimeout(()=>{Object.keys(u).forEach(e=>{var i;if(!e.startsWith(L)||typeof u[e]!="string")return;const r=+((i=u[e].match(ce))==null?void 0:i[1]);r&&r-Date.now()<24*60*60*1e3&&(R[e]=setTimeout(()=>{$[e]?$[e].reset():delete u[e]},r-Date.now()))})},1e3),g.Atom=H,g.atom=O,g.configureAtomaric=Q,g.configuredOptions=I,g.useAtom=k,g.useAtomDo=Z,g.useAtomGet=Y,g.useAtomSet=M,g.useAtomSetDeferred=X,g.useAtomValue=J,Object.defineProperty(g,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.38",
4
+ "version": "0.0.39",
5
5
  "type": "module",
6
6
  "main": "./build/atomaric.umd.cjs",
7
7
  "module": "./build/atomaric.js",
package/types/index.d.ts CHANGED
@@ -109,6 +109,8 @@ export type ArrayActions<Value> = UpdateAction<Value[]> & {
109
109
  filter: (filter?: (value: Value, index: number, array: Value[]) => any) => void;
110
110
  /** will add value if it doesn't exist, otherwise delete */
111
111
  toggle: (value: Value, isAddInStart?: boolean) => void;
112
+ /** will delete item from array */
113
+ remove: (value: Value) => void;
112
114
  };
113
115
 
114
116
  export type DefaultActions<Value> = Value extends Set<infer Val>