atomaric 0.0.20 → 0.0.22

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