atomaric 0.0.13 → 0.0.15

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,134 @@
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(s, o) {
3
+ let _ = s, g, p = () => {
4
+ };
5
+ const D = (e) => _ = e, m = () => _;
6
+ this.get = () => m();
7
+ let N = () => {
8
+ let e = null;
9
+ typeof s == "number" ? e = w(
10
+ {
11
+ increment: (n) => {
12
+ i(+this.get() + (n ?? 0));
13
+ }
33
14
  }
34
- })), typeof t == "boolean" && (a = s(t, {
35
- toggle: () => {
36
- this.set(!this.get());
15
+ ) : typeof s == "boolean" ? e = w(
16
+ {
17
+ toggle: () => {
18
+ i(!this.get());
19
+ }
37
20
  }
38
- })), Array.isArray(t) && (a = s(t, {
39
- push: (...r) => {
40
- this.set([this.get()].flat().concat(r));
41
- },
42
- unshift: (...r) => {
43
- this.set(r.concat(this.get()));
44
- },
45
- update: (r) => {
46
- const o = [...this.get()];
47
- r(o), this.set(o);
48
- },
49
- filter: (r) => {
50
- this.set(this.get().filter(r ?? j));
21
+ ) : Array.isArray(s) ? e = w(
22
+ {
23
+ push: (...n) => {
24
+ i([this.get()].flat().concat(n));
25
+ },
26
+ unshift: (...n) => {
27
+ i(n.concat(this.get()));
28
+ },
29
+ update: (n) => {
30
+ const r = [...this.get()];
31
+ n(r), i(r);
32
+ },
33
+ filter: (n) => {
34
+ i(this.get().filter(n ?? I));
35
+ }
51
36
  }
52
- })), t instanceof Set && (a = s(t, {
53
- add: (r) => {
54
- const o = new Set(this.get());
55
- o.add(r), this.set(o);
56
- },
57
- delete: (r) => {
58
- const o = new Set(this.get());
59
- o.delete(r), this.set(o);
60
- },
61
- clear: () => {
62
- this.set(/* @__PURE__ */ new Set());
63
- },
64
- update: (r) => {
65
- const o = new Set(this.get());
66
- r(o), this.set(o);
37
+ ) : s instanceof Set && (e = w(
38
+ {
39
+ add: (n) => {
40
+ const r = new Set(this.get());
41
+ r.add(n), i(r);
42
+ },
43
+ delete: (n) => {
44
+ const r = new Set(this.get());
45
+ r.delete(n), i(r);
46
+ },
47
+ clear: () => {
48
+ i(/* @__PURE__ */ new Set());
49
+ },
50
+ update: (n) => {
51
+ const r = new Set(this.get());
52
+ n(r), i(r);
53
+ }
67
54
  }
68
- }));
69
- const b = {
70
- ...typeof n == "object" && "do" in n ? n.do(
71
- () => this.get(),
72
- (r, o) => this.set(r, o)
73
- ) : {},
74
- ...a
75
- };
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;
55
+ ));
56
+ const c = { ...typeof o == "object" && o != null && "do" in o ? o.do(
57
+ () => this.get(),
58
+ (n, r) => i(n, r)
59
+ ) : {}, ...e };
60
+ return N = () => c, c;
61
+ };
62
+ Object.defineProperty(this, "do", { get: () => N() }), Object.defineProperty(this, "defaultValue", { get: () => s });
63
+ const S = /* @__PURE__ */ new Set(), j = (e) => e(this.get());
64
+ this.subscribe = (e) => (S.add(e), () => (S.delete(e), void 0));
65
+ const i = (e, a) => {
66
+ const c = typeof e == "function" ? e(this.get()) : e;
67
+ c === this.get() || c === void 0 || typeof c == "number" && isNaN(c) || (D(c), S.forEach(j, this), a !== !0 && p(c));
68
+ };
69
+ if (this.set = (e, a) => i(e, a), this.reset = () => {
70
+ i(s, !0), S.forEach(j, this);
71
+ }, this.setDeferred = (e, a = 500, c, n = !0) => {
72
+ n && g === void 0 && i(e, c), clearTimeout(g), g = setTimeout(() => {
73
+ i(e, c), g = void 0;
74
+ }, a);
75
+ }, o == null) return;
76
+ 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) => {
77
+ if (e instanceof Set) return JSON.stringify(Array.from(e));
78
+ throw console.error(e), "The value is not Set instance";
79
+ } : (e) => JSON.stringify(e);
80
+ 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;
81
+ const l = `atom/${u}`;
82
+ let k = !0;
83
+ this.get = () => {
84
+ if (this.get = () => m(), k) {
85
+ k = !1;
90
86
  try {
91
- super.setValue(c in i ? g(i[c]) : t);
87
+ D(l in f ? d(f[l]) : s);
92
88
  } catch {
93
- console.warn("Invalid json value", i[c]);
89
+ console.warn("Invalid json value", f[l]);
94
90
  }
95
91
  }
96
- return super.getValue();
97
- }, this.save = (s) => {
98
- if (s === t) {
92
+ return m();
93
+ }, p = (e) => {
94
+ if (e === s) {
99
95
  this.reset();
100
96
  return;
101
97
  }
102
- i[c] = f(s), y(c);
98
+ f[l] = b(e);
103
99
  }, 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();
100
+ delete f[l], i(s, !0);
101
+ }, y && h[l] !== void 0 && console.warn("Duplicate Atom key", u), A && (h[l] = (e) => {
102
+ if (e.newValue === null) {
103
+ this.reset();
104
+ return;
105
+ }
106
+ try {
107
+ i(d(e.newValue));
108
+ } catch {
109
+ console.warn("Invalid json value", e.newValue);
110
+ }
111
+ });
134
112
  }
135
113
  }
136
- const i = window.localStorage, d = {}, h = {};
137
- 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));
114
+ const f = window.localStorage, h = {}, I = (t) => t;
115
+ window.addEventListener("storage", (t) => {
116
+ var s;
117
+ t.key === null || t.newValue === t.oldValue || (s = h[t.key]) == null || s.call(h, t);
140
118
  });
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;
119
+ const w = (t, s) => t;
185
120
  let E = () => {
186
121
  throw "call configureAtomaric() before all!";
187
122
  };
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);
123
+ 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);
189
124
  export {
190
- k as Atom,
191
- G as atom,
125
+ C as Atom,
126
+ L as atom,
192
127
  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
128
+ G as useAtom,
129
+ F as useAtomDo,
130
+ v as useAtomGet,
131
+ T as useAtomSet,
132
+ V as useAtomSetDeferred,
133
+ J as useAtomValue
199
134
  };
@@ -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(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();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};Object.defineProperty(this,"do",{get:()=>C()}),Object.defineProperty(this,"defaultValue",{get:()=>i});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;window.addEventListener("storage",t=>{var i;t.key===null||t.newValue===t.oldValue||(i=g[t.key])==null||i.call(g,t)});const S=(t,i)=>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"})});
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.15",
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 */