atomaric 0.0.7 → 0.0.9

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,51 +1,61 @@
1
- const h = {};
2
- window.addEventListener("storage", (e) => {
3
- e.key === null || h[e.key] === void 0 || h[e.key](e);
1
+ const c = {};
2
+ window.addEventListener("storage", (t) => {
3
+ t.key === null || c[t.key] === void 0 || c[t.key](t);
4
4
  });
5
- class m {
6
- constructor(s, i) {
7
- if (this._defaultValue = s, this.subscribers = /* @__PURE__ */ new Set(), this.save = () => {
8
- }, this.invokeSubscriber = (t) => t(this.value), this.get = () => this.value, this.toggle = () => this.set(!this.value), this.inkrement = (t) => this.set(this.value + t), this.subscribe = (t) => (this.subscribers.add(t), () => {
9
- this.subscribers.delete(t);
10
- }), this.set = (t, l) => {
11
- const r = typeof t == "function" ? t(this.value) : t;
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), () => {
9
+ this.subscribers.delete(e);
10
+ }), this.set = (e, l) => {
11
+ const r = typeof e == "function" ? e(this.value) : e;
12
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 = (t, l = 500, r, f = !0) => {
14
- f && this.debounceTimeout === void 0 && this.set(t, r), clearTimeout(this.debounceTimeout), this.debounceTimeout = setTimeout(() => {
15
- this.set(t, r), delete this.debounceTimeout;
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
16
  }, l);
17
- }, this.value = s, typeof s != "boolean" && (this.toggle = () => {
18
- }), typeof s != "number" && (this.inkrement = () => {
17
+ }, this.value = i, typeof i != "boolean" && (this.toggle = () => {
18
+ }), typeof i != "number" && (this.inkrement = () => {
19
19
  }), this.reset = () => {
20
- this.set(s, !0), this.subscribers.forEach(this.invokeSubscriber, this);
21
- }, i == null) return;
22
- let u = null, c = !0, g = !0, n = (t) => JSON.parse(t), a = (t) => JSON.stringify(t);
23
- if (typeof i == "string" ? u = i : i.storeKey !== void 0 && (c = i.warnOnDuplicateStoreKey ?? !0, g = i.listenStorageChanges ?? !0, u = i.storeKey, n = i.parseValue ?? n, a = i.stringifyValue ?? a), u === null) return;
24
- const o = `atom/${u}`;
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) => {
23
+ if (e instanceof Set) return JSON.stringify(Array.from(e));
24
+ throw console.error(e), "The value is not Set instance";
25
+ } : (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;
25
29
  this.get = () => {
26
- try {
27
- this.value = o in localStorage ? n(localStorage[o]) : s, this.get = () => this.value;
28
- } catch {
29
- console.warn("Invalid json value", localStorage[o]);
30
+ if (this.get = () => this.value, b) {
31
+ b = !1;
32
+ try {
33
+ this.value = n in localStorage ? u(localStorage[n]) : i;
34
+ } catch {
35
+ console.warn("Invalid json value", localStorage[n]);
36
+ }
30
37
  }
31
38
  return this.value;
32
- }, this.save = (t) => {
33
- if (t === s) {
39
+ }, this.save = (e) => {
40
+ if (e === i) {
34
41
  this.reset();
35
42
  return;
36
43
  }
37
- localStorage[o] = a(t);
44
+ localStorage[n] = a(e);
38
45
  }, this.reset = () => {
39
- delete localStorage[o], this.set(s, !0);
40
- }, c && h[o] !== void 0 && console.warn("Duplicate Atom key", i), g && (h[o] = (t) => {
41
- if (t.newValue === null) {
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) {
42
52
  this.reset();
43
53
  return;
44
54
  }
45
55
  try {
46
- this.set(n(t.newValue));
56
+ this.set(u(e.newValue));
47
57
  } catch {
48
- console.warn("Invalid json value", t.newValue);
58
+ console.warn("Invalid json value", e.newValue);
49
59
  }
50
60
  });
51
61
  }
@@ -53,19 +63,19 @@ class m {
53
63
  return this._defaultValue;
54
64
  }
55
65
  }
56
- let b = () => {
66
+ let S = () => {
57
67
  throw "call configureAtomaric() before all!";
58
68
  };
59
- const v = (e) => b = e.useSyncExternalStore, S = (e) => b(e.subscribe, e.get), d = (e) => e.set, w = (e) => e.setDeferred, y = (e) => e.get, k = (e) => e.toggle, A = (e) => e.inkrement, T = (e) => [S(e), d(e)], D = (e, s) => new m(e, s);
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);
60
70
  export {
61
- m as Atom,
62
- D as atom,
63
- v as configureAtomaric,
64
- T as useAtom,
65
- y as useAtomGet,
66
- A as useAtomInkrement,
67
- d as useAtomSet,
68
- w as useAtomSetDeferred,
69
- k as useAtomToggle,
70
- S as useAtomValue
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
71
81
  };
@@ -1 +1 @@
1
- (function(s,n){typeof exports=="object"&&typeof module<"u"?n(exports):typeof define=="function"&&define.amd?define(["exports"],n):(s=typeof globalThis<"u"?globalThis:s||self,n(s.atomaric={}))})(this,function(s){"use strict";const n={};window.addEventListener("storage",e=>{e.key===null||n[e.key]===void 0||n[e.key](e)});class f{constructor(i,o){if(this._defaultValue=i,this.subscribers=new Set,this.save=()=>{},this.invokeSubscriber=t=>t(this.value),this.get=()=>this.value,this.toggle=()=>this.set(!this.value),this.inkrement=t=>this.set(this.value+t),this.subscribe=t=>(this.subscribers.add(t),()=>{this.subscribers.delete(t)}),this.set=(t,c)=>{const u=typeof t=="function"?t(this.value):t;u===this.value||u===void 0||typeof u=="number"&&isNaN(u)||(this.value=u,this.subscribers.forEach(this.invokeSubscriber,this),c!==!0&&this.save(u))},this.setDeferred=(t,c=500,u,E=!0)=>{E&&this.debounceTimeout===void 0&&this.set(t,u),clearTimeout(this.debounceTimeout),this.debounceTimeout=setTimeout(()=>{this.set(t,u),delete this.debounceTimeout},c)},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)},o==null)return;let l=null,b=!0,S=!0,a=t=>JSON.parse(t),h=t=>JSON.stringify(t);if(typeof o=="string"?l=o:o.storeKey!==void 0&&(b=o.warnOnDuplicateStoreKey??!0,S=o.listenStorageChanges??!0,l=o.storeKey,a=o.parseValue??a,h=o.stringifyValue??h),l===null)return;const r=`atom/${l}`;this.get=()=>{try{this.value=r in localStorage?a(localStorage[r]):i,this.get=()=>this.value}catch{console.warn("Invalid json value",localStorage[r])}return this.value},this.save=t=>{if(t===i){this.reset();return}localStorage[r]=h(t)},this.reset=()=>{delete localStorage[r],this.set(i,!0)},b&&n[r]!==void 0&&console.warn("Duplicate Atom key",o),S&&(n[r]=t=>{if(t.newValue===null){this.reset();return}try{this.set(a(t.newValue))}catch{console.warn("Invalid json value",t.newValue)}})}get defaultValue(){return this._defaultValue}}let m=()=>{throw"call configureAtomaric() before all!"};const A=e=>m=e.useSyncExternalStore,g=e=>m(e.subscribe,e.get),d=e=>e.set,v=e=>e.setDeferred,y=e=>e.get,k=e=>e.toggle,w=e=>e.inkrement,T=e=>[g(e),d(e)],D=(e,i)=>new f(e,i);s.Atom=f,s.atom=D,s.configureAtomaric=A,s.useAtom=T,s.useAtomGet=y,s.useAtomInkrement=w,s.useAtomSet=d,s.useAtomSetDeferred=v,s.useAtomToggle=k,s.useAtomValue=g,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
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"})});
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.7",
4
+ "version": "0.0.9",
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
- "dev": "vite",
26
+ "dev": "vite --port 8378 --host",
27
27
  "build": "tsc && vite build",
28
28
  "preview": "vite preview"
29
29
  },
@@ -31,8 +31,11 @@
31
31
  "@types/md5": "^2.3.5",
32
32
  "@types/node": "^22.15.1",
33
33
  "@types/react": "^19.1.2",
34
- "vite": "^6.3.1",
34
+ "@types/react-dom": "^19.1.8",
35
+ "@vitejs/plugin-react": "^4.7.0",
35
36
  "react": "^19.1.0",
37
+ "react-dom": "^19.1.1",
38
+ "vite": "^6.3.1",
36
39
  "vite-plugin-eslint": "^1.8.1"
37
40
  },
38
41
  "dependencies": {
package/types/model.d.ts CHANGED
@@ -3,13 +3,22 @@ type Sunscriber<Value> = (value: Value) => void;
3
3
  export type AtomStoreKey = `${string}${string}:${string}${string}`;
4
4
 
5
5
  export type AtomOptions<Value> = {
6
- storeKey?: AtomStoreKey;
6
+ /** save in localStorage by this key */
7
+ storeKey: AtomStoreKey;
7
8
  /** **default: true** */
8
9
  warnOnDuplicateStoreKey?: boolean;
9
- /** **default: true** */
10
+ /** will update value if localStorage value is changed
11
+ * **default: true**
12
+ */
10
13
  listenStorageChanges?: boolean;
14
+ /** map localStorage string value to Value */
11
15
  parseValue?: (stringifiedValue: string) => Value;
16
+ /** map Value to localStorage string value */
12
17
  stringifyValue?: (value: Value) => string;
18
+ /** can not to change localStorage value in dev tools
19
+ * **default: false**
20
+ */
21
+ unchangable?: boolean;
13
22
  };
14
23
 
15
24
  export type AtomSetMethod<Value> = (value: Value | ((prev: Value) => Value), isPreventSave?: boolean) => void;
@@ -44,9 +53,8 @@ export function useAtomInkrement(atom: Atom<number>): (typeof atom)['inkrement']
44
53
 
45
54
  export function useAtom<Value>(atom: Atom<Value>): [Value, (typeof atom)['set']];
46
55
 
47
- export function atom<Value>(
48
- value: Value,
49
- storeKeyOrOptions?: `${string}${string}:${string}${string}` | AtomOptions<Value>,
50
- ): Atom<Value>;
56
+ export type StoreKeyOrOptions<Value> = `${string}${string}:${string}${string}` | AtomOptions<Value>;
57
+
58
+ export function atom<Value>(value: Value, storeKeyOrOptions?: StoreKeyOrOptions<Value>): Atom<Value>;
51
59
 
52
60
  export function configureAtomaric(hooks: { useSyncExternalStore: typeof useSyncExternalStore }): void;