atomaric 0.0.10 → 0.0.12

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