atomaric 0.0.17 → 0.0.18

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