atomaric 0.0.8 → 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,53 +1,53 @@
1
- const a = {};
1
+ const c = {};
2
2
  window.addEventListener("storage", (t) => {
3
- t.key === null || a[t.key] === void 0 || a[t.key](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 = () => {
5
+ class d {
6
+ constructor(i, s) {
7
+ if (this._defaultValue = i, this.subscribers = /* @__PURE__ */ new Set(), this.save = () => {
8
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
9
  this.subscribers.delete(e);
10
10
  }), this.set = (e, l) => {
11
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 = (e, l = 500, r, S = !0) => {
14
- S && this.debounceTimeout === void 0 && this.set(e, r), clearTimeout(this.debounceTimeout), this.debounceTimeout = setTimeout(() => {
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
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 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) => {
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
23
  if (e instanceof Set) return JSON.stringify(Array.from(e));
24
24
  throw console.error(e), "The value is not Set instance";
25
25
  } : (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;
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
27
  const n = `atom/${o}`;
28
- let g = !0;
28
+ let b = !0;
29
29
  this.get = () => {
30
- if (this.get = () => this.value, g) {
31
- g = !1;
30
+ if (this.get = () => this.value, b) {
31
+ b = !1;
32
32
  try {
33
- this.value = n in localStorage ? u(localStorage[n]) : s;
33
+ this.value = n in localStorage ? u(localStorage[n]) : i;
34
34
  } catch {
35
35
  console.warn("Invalid json value", localStorage[n]);
36
36
  }
37
37
  }
38
38
  return this.value;
39
39
  }, this.save = (e) => {
40
- if (e === s) {
40
+ if (e === i) {
41
41
  this.reset();
42
42
  return;
43
43
  }
44
- localStorage[n] = c(e);
44
+ localStorage[n] = a(e);
45
45
  }, this.reset = () => {
46
- delete localStorage[n], this.set(s, !0);
47
- }, h && a[n] !== void 0 && console.warn(
46
+ delete localStorage[n], this.set(i, !0);
47
+ }, f && c[n] !== void 0 && console.warn(
48
48
  "Duplicate Atom key",
49
- typeof i == "string" ? i : i.storeKey
50
- ), f && (a[n] = (e) => {
49
+ typeof s == "string" ? s : s.storeKey
50
+ ), g && (c[n] = h ? () => localStorage[n] = a(this.value) : (e) => {
51
51
  if (e.newValue === null) {
52
52
  this.reset();
53
53
  return;
@@ -63,19 +63,19 @@ class m {
63
63
  return this._defaultValue;
64
64
  }
65
65
  }
66
- let b = () => {
66
+ let S = () => {
67
67
  throw "call configureAtomaric() before all!";
68
68
  };
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);
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);
70
70
  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
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
81
81
  };
@@ -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(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.8",
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
- "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
@@ -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;