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