atomaric 0.0.8 → 0.0.10

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,38 +1,51 @@
1
- const a = {};
2
- window.addEventListener("storage", (t) => {
3
- t.key === null || a[t.key] === void 0 || a[t.key](t);
4
- });
5
- class m {
6
- constructor(s, i) {
1
+ class v {
2
+ constructor(s, n) {
7
3
  if (this._defaultValue = s, 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), () => {
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), () => {
9
5
  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, S = !0) => {
14
- S && 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 = s, typeof s != "boolean" && (this.toggle = () => {
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 = () => {
18
31
  }), typeof s != "number" && (this.inkrement = () => {
19
32
  }), this.reset = () => {
20
33
  this.set(s, !0), this.subscribers.forEach(this.invokeSubscriber, this);
21
- }, i == null) return;
22
- let o = null, h = !0, f = !0, u = s instanceof Set ? (e) => new Set(JSON.parse(e)) : (e) => JSON.parse(e), c = s instanceof Set ? (e) => {
34
+ }, 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) => {
23
36
  if (e instanceof Set) return JSON.stringify(Array.from(e));
24
37
  throw console.error(e), "The value is not Set instance";
25
38
  } : (e) => JSON.stringify(e);
26
- if (typeof i == "string" ? o = i : i.storeKey !== void 0 && (h = i.warnOnDuplicateStoreKey ?? !0, f = i.listenStorageChanges ?? !0, o = i.storeKey, u = i.parseValue ?? u, c = i.stringifyValue ?? c), o === null) return;
27
- const n = `atom/${o}`;
28
- let g = !0;
29
- this.get = () => {
30
- if (this.get = () => this.value, g) {
31
- g = !1;
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;
42
+ if (this.get = () => {
43
+ if (this.get = () => this.value, m) {
44
+ m = !1;
32
45
  try {
33
- this.value = n in localStorage ? u(localStorage[n]) : s;
46
+ this.value = l in i ? f(i[l]) : s;
34
47
  } catch {
35
- console.warn("Invalid json value", localStorage[n]);
48
+ console.warn("Invalid json value", i[l]);
36
49
  }
37
50
  }
38
51
  return this.value;
@@ -41,41 +54,87 @@ class m {
41
54
  this.reset();
42
55
  return;
43
56
  }
44
- localStorage[n] = c(e);
57
+ i[l] = d(e);
45
58
  }, this.reset = () => {
46
- delete localStorage[n], this.set(s, !0);
47
- }, h && a[n] !== void 0 && console.warn(
48
- "Duplicate Atom key",
49
- typeof i == "string" ? i : i.storeKey
50
- ), f && (a[n] = (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
- });
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();
66
+ } else
67
+ a[l].updatable = (e) => {
68
+ if (e.newValue === null) {
69
+ this.reset();
70
+ return;
71
+ }
72
+ try {
73
+ this.set(f(e.newValue));
74
+ } catch {
75
+ console.warn("Invalid json value", e.newValue);
76
+ }
77
+ };
61
78
  }
62
79
  get defaultValue() {
63
80
  return this._defaultValue;
64
81
  }
65
82
  }
66
- let b = () => {
83
+ const i = window.localStorage, a = {};
84
+ 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));
87
+ });
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);
92
+ };
93
+ i.removeItem = (t) => {
94
+ var s;
95
+ ((s = a[t]) == null ? void 0 : s.unchangable) === void 0 && I.call(i, t);
96
+ };
97
+ const k = /* @__PURE__ */ (() => {
98
+ let t = !1;
99
+ return () => {
100
+ if (t) return;
101
+ t = !0;
102
+ let s;
103
+ 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);
112
+ };
113
+ window.addEventListener("focus", () => clearInterval(s)), window.addEventListener("blur", n);
114
+ };
115
+ })();
116
+ (() => {
117
+ 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);
121
+ Object.defineProperty(window, "localStorage", {
122
+ get: () => (Promise.resolve().then(s), i)
123
+ });
124
+ })();
125
+ let S = () => {
67
126
  throw "call configureAtomaric() before all!";
68
127
  };
69
- const w = (t) => b = t.useSyncExternalStore, d = (t) => b(t.subscribe, t.get), v = (t) => t.set, y = (t) => t.setDeferred, k = (t) => t.get, A = (t) => t.toggle, T = (t) => t.inkrement, D = (t) => [d(t), v(t)], N = (t, s) => new m(t, s);
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);
70
129
  export {
71
- m as Atom,
72
- N as atom,
73
- w as configureAtomaric,
74
- D as useAtom,
75
- k as useAtomGet,
76
- T as useAtomInkrement,
77
- v as useAtomSet,
78
- y as useAtomSetDeferred,
79
- A as useAtomToggle,
80
- d as useAtomValue
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
81
140
  };
@@ -1 +1 @@
1
- (function(s,o){typeof exports=="object"&&typeof module<"u"?o(exports):typeof define=="function"&&define.amd?define(["exports"],o):(s=typeof globalThis<"u"?globalThis:s||self,o(s.atomaric={}))})(this,function(s){"use strict";const o={};window.addEventListener("storage",t=>{t.key===null||o[t.key]===void 0||o[t.key](t)});class f{constructor(i,n){if(this._defaultValue=i,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,a)=>{const r=typeof e=="function"?e(this.value):e;r===this.value||r===void 0||typeof r=="number"&&isNaN(r)||(this.value=r,this.subscribers.forEach(this.invokeSubscriber,this),a!==!0&&this.save(r))},this.setDeferred=(e,a=500,r,E=!0)=>{E&&this.debounceTimeout===void 0&&this.set(e,r),clearTimeout(this.debounceTimeout),this.debounceTimeout=setTimeout(()=>{this.set(e,r),delete this.debounceTimeout},a)},this.value=i,typeof i!="boolean"&&(this.toggle=()=>{}),typeof i!="number"&&(this.inkrement=()=>{}),this.reset=()=>{this.set(i,!0),this.subscribers.forEach(this.invokeSubscriber,this)},n==null)return;let l=null,b=!0,d=!0,c=i instanceof Set?e=>new Set(JSON.parse(e)):e=>JSON.parse(e),h=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 n=="string"?l=n:n.storeKey!==void 0&&(b=n.warnOnDuplicateStoreKey??!0,d=n.listenStorageChanges??!0,l=n.storeKey,c=n.parseValue??c,h=n.stringifyValue??h),l===null)return;const u=`atom/${l}`;let A=!0;this.get=()=>{if(this.get=()=>this.value,A){A=!1;try{this.value=u in localStorage?c(localStorage[u]):i}catch{console.warn("Invalid json value",localStorage[u])}}return this.value},this.save=e=>{if(e===i){this.reset();return}localStorage[u]=h(e)},this.reset=()=>{delete localStorage[u],this.set(i,!0)},b&&o[u]!==void 0&&console.warn("Duplicate Atom key",typeof n=="string"?n:n.storeKey),d&&(o[u]=e=>{if(e.newValue===null){this.reset();return}try{this.set(c(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,g=t=>m(t.subscribe,t.get),S=t=>t.set,v=t=>t.setDeferred,w=t=>t.get,k=t=>t.toggle,T=t=>t.inkrement,D=t=>[g(t),S(t)],N=(t,i)=>new f(t,i);s.Atom=f,s.atom=N,s.configureAtomaric=y,s.useAtom=D,s.useAtomGet=w,s.useAtomInkrement=T,s.useAtomSet=S,s.useAtomSetDeferred=v,s.useAtomToggle=k,s.useAtomValue=g,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
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"})});
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.8",
4
+ "version": "0.0.10",
5
5
  "type": "module",
6
6
  "main": "./build/atomaric.umd.cjs",
7
7
  "module": "./build/atomaric.js",
@@ -23,7 +23,7 @@
23
23
  "manager"
24
24
  ],
25
25
  "scripts": {
26
- "start": "vite --port 8378 --host",
26
+ "dev": "vite --port 8378 --host",
27
27
  "build": "tsc && vite build",
28
28
  "preview": "vite preview"
29
29
  },
package/types/model.d.ts CHANGED
@@ -2,15 +2,30 @@ 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
- storeKey?: AtomStoreKey;
5
+ export type AtomOptions<Value, Actions extends Record<string, Function>> = {
7
6
  /** **default: true** */
8
7
  warnOnDuplicateStoreKey?: boolean;
9
- /** **default: true** */
8
+ /** will update value if localStorage value is changed
9
+ * **default: true**
10
+ */
10
11
  listenStorageChanges?: boolean;
12
+ /** map localStorage string value to Value */
11
13
  parseValue?: (stringifiedValue: string) => Value;
14
+ /** map Value to localStorage string value */
12
15
  stringifyValue?: (value: Value) => string;
13
- };
16
+ /** can not to change localStorage value in dev tools
17
+ * **default: false**
18
+ */
19
+ unchangable?: boolean;
20
+ } & (
21
+ | {
22
+ /** save in localStorage by this key */
23
+ storeKey: AtomStoreKey;
24
+ }
25
+ | {
26
+ do: (get: () => Value, set: (value: Value) => void) => Actions;
27
+ }
28
+ );
14
29
 
15
30
  export type AtomSetMethod<Value> = (value: Value | ((prev: Value) => Value), isPreventSave?: boolean) => void;
16
31
  export type AtomSetDeferredMethod<Value> = (
@@ -22,8 +37,14 @@ export type AtomSetDeferredMethod<Value> = (
22
37
 
23
38
  export type AtomSubscribeMethod<Value> = (subscriber: Sunscriber<Value>) => () => void;
24
39
 
25
- export class Atom<Value> {
26
- constructor(defaultValue: Value, storeKeyOrOptions: AtomStoreKey | undefined | AtomOptions<Value>);
40
+ export type SetActions<Value> = {
41
+ add: (value: Value) => Set<Value>;
42
+ delete: (value: Value) => boolean;
43
+ clear: () => void;
44
+ };
45
+
46
+ export class Atom<Value, Actions extends Record<string, Function>> {
47
+ constructor(defaultValue: Value, storeKeyOrOptions: AtomStoreKey | undefined | AtomOptions<Value, Actions>);
27
48
 
28
49
  readonly defaultValue: Value;
29
50
  readonly get: () => Value;
@@ -33,6 +54,7 @@ export class Atom<Value> {
33
54
  readonly toggle: () => void;
34
55
  readonly inkrement: (delta: number) => void;
35
56
  readonly subscribe: AtomSubscribeMethod<Value>;
57
+ do: Actions & (Value extends Set<infer V> ? SetActions<V> : {});
36
58
  }
37
59
 
38
60
  export function useAtomValue<Value>(atom: Atom<Value>): Value;
@@ -44,8 +66,13 @@ export function useAtomInkrement(atom: Atom<number>): (typeof atom)['inkrement']
44
66
 
45
67
  export function useAtom<Value>(atom: Atom<Value>): [Value, (typeof atom)['set']];
46
68
 
47
- export type StoreKeyOrOptions<Value> = `${string}${string}:${string}${string}` | AtomOptions<Value>;
69
+ export type StoreKeyOrOptions<Value, Actions extends Record<string, Function>> =
70
+ | `${string}${string}:${string}${string}`
71
+ | AtomOptions<Value, Actions>;
48
72
 
49
- export function atom<Value>(value: Value, storeKeyOrOptions?: StoreKeyOrOptions<Value>): Atom<Value>;
73
+ export function atom<Value, Actions extends Record<string, Function>>(
74
+ value: Value,
75
+ storeKeyOrOptions?: StoreKeyOrOptions<Value, Actions>,
76
+ ): Atom<Value, Actions>;
50
77
 
51
78
  export function configureAtomaric(hooks: { useSyncExternalStore: typeof useSyncExternalStore }): void;