atomaric 0.0.13 → 0.0.14

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