atomaric 0.0.9 → 0.0.11

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