atomaric 0.0.16 → 0.0.17

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,161 @@
1
- class C {
2
- constructor(s, o) {
3
- let _ = s, g, p = () => {
1
+ class k {
2
+ constructor(n, r) {
3
+ let v = n, d, C = () => {
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 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 = () => {
8
8
  let e = null;
9
- typeof s == "number" ? e = w(
9
+ typeof n == "number" ? e = b(
10
10
  {
11
- increment: (n) => {
12
- i(+this.get() + (n ?? 0));
11
+ increment: (s) => {
12
+ i(+this.get() + (s ?? 0));
13
13
  }
14
14
  }
15
- ) : typeof s == "boolean" ? e = w(
15
+ ) : typeof n == "boolean" ? e = b(
16
16
  {
17
17
  toggle: () => {
18
18
  i(!this.get());
19
19
  }
20
20
  }
21
- ) : Array.isArray(s) ? e = w(
21
+ ) : Array.isArray(n) ? e = b(
22
22
  {
23
- push: (...n) => {
24
- i([this.get()].flat().concat(n));
23
+ push: (...s) => {
24
+ i([this.get()].flat().concat(s));
25
25
  },
26
- unshift: (...n) => {
27
- i(n.concat(this.get()));
26
+ unshift: (...s) => {
27
+ i(s.concat(this.get()));
28
28
  },
29
- update: (n) => {
30
- const r = [...this.get()];
31
- n(r), i(r);
29
+ update: (s) => {
30
+ const c = [...this.get()];
31
+ s(c), i(c);
32
32
  },
33
- filter: (n) => {
34
- i(this.get().filter(n ?? I));
33
+ filter: (s) => {
34
+ i(this.get().filter(s ?? J));
35
35
  }
36
36
  }
37
- ) : s instanceof Set && (e = w(
37
+ ) : n instanceof Set && (e = b(
38
38
  {
39
- add: (n) => {
40
- const r = new Set(this.get());
41
- r.add(n), i(r);
39
+ add: (s) => {
40
+ const c = new Set(this.get());
41
+ c.add(s), i(c);
42
42
  },
43
- delete: (n) => {
44
- const r = new Set(this.get());
45
- r.delete(n), i(r);
43
+ delete: (s) => {
44
+ const c = new Set(this.get());
45
+ c.delete(s), i(c);
46
46
  },
47
47
  clear: () => {
48
48
  i(/* @__PURE__ */ new Set());
49
49
  },
50
- update: (n) => {
51
- const r = new Set(this.get());
52
- n(r), i(r);
50
+ update: (s) => {
51
+ const c = new Set(this.get());
52
+ s(c), i(c);
53
53
  }
54
54
  }
55
55
  ));
56
- const c = { ...typeof o == "object" && o != null && "do" in o ? o.do(
56
+ const a = { ...typeof r == "object" && r != null && "do" in r ? r.do(
57
57
  () => this.get(),
58
- (n, r) => i(n, r)
58
+ (s, c) => i(s, c)
59
59
  ) : {}, ...e };
60
- return N = () => c, c;
60
+ return E = () => a, a;
61
61
  };
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));
62
+ const m = /* @__PURE__ */ new Set(), N = (e) => e(this.get());
63
+ 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);
68
+ try {
69
+ p.postMessage({ key: l, value: h() });
70
+ } catch {
71
+ }
72
+ u !== !0 && C(a);
73
+ }
67
74
  };
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;
73
- }, 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) => {
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) => {
76
83
  if (e instanceof Set) return JSON.stringify(Array.from(e));
77
84
  throw console.error(e), "The value is not Set instance";
78
85
  } : (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;
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;
87
+ const l = `atom/${g}`;
88
+ let T = !0;
89
+ if (this.get = () => {
90
+ if (this.get = () => h(), T) {
91
+ T = !1;
85
92
  try {
86
- D(l in f ? d(f[l]) : s);
93
+ D(l in o ? S(o[l]) : n);
87
94
  } catch {
88
- console.warn("Invalid json value", f[l]);
95
+ console.warn("Invalid json value", o[l]);
89
96
  }
90
97
  }
91
- return m();
92
- }, p = (e) => {
93
- if (e === s) {
98
+ return h();
99
+ }, C = (e) => {
100
+ if (e === n) {
94
101
  this.reset();
95
102
  return;
96
103
  }
97
- f[l] = b(e);
104
+ o[l] = w(e);
98
105
  }, 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
- });
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()));
112
+ };
113
+ } else
114
+ f[l] = (e) => {
115
+ if (e.newValue === null) {
116
+ this.reset();
117
+ return;
118
+ }
119
+ try {
120
+ i(S(e.newValue));
121
+ } catch {
122
+ console.warn("Invalid json value", e.newValue);
123
+ }
124
+ };
111
125
  }
112
126
  }
113
- const f = window.localStorage, h = {}, I = (t) => t, w = (t, s) => t;
127
+ let p;
128
+ 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
+ });
133
+ } catch {
134
+ }
135
+ const o = window.localStorage, f = {}, A = {}, J = (t) => t, b = (t, n) => t;
114
136
  window.addEventListener("storage", (t) => {
115
- var s;
116
- t.key === null || t.newValue === t.oldValue || (s = h[t.key]) == null || s.call(h, t);
137
+ var n;
138
+ t.key === null || t.newValue === t.oldValue || (n = f[t.key]) == null || n.call(f, t);
117
139
  });
118
- let E = () => {
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
+ };
144
+ o.removeItem = (t) => {
145
+ A[t] === void 0 && V.call(o, t);
146
+ };
147
+ let j = () => {
119
148
  throw "call configureAtomaric() before all!";
120
149
  };
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);
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);
122
151
  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
152
+ k as Atom,
153
+ 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
132
161
  };
@@ -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(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"})});
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.17",
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 */