atomaric 0.0.18 → 0.0.20

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