atomaric 0.0.5 → 0.0.6

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/README.md CHANGED
@@ -1,10 +1,10 @@
1
- ## 🕹️Installation and usage
1
+ ## 🕹️Installation
2
2
 
3
3
  ```sh
4
4
  npm install atomaric
5
5
  ```
6
6
 
7
- ### usage
7
+ ## usage
8
8
 
9
9
  ```tsx
10
10
  import { atom, useAtom, configureAtomaric } from 'atomaric';
@@ -14,7 +14,7 @@ configureAtomaric({ useSyncExternalStore }); // do this before all
14
14
 
15
15
  const nameAtom = atom(
16
16
  'World',
17
- 'greats:name', // optional locakStorage key
17
+ 'greats:name', // optional localStorage key
18
18
  );
19
19
 
20
20
  function App() {
package/build/atomaric.js CHANGED
@@ -1,32 +1,36 @@
1
- const u = {};
1
+ const h = {};
2
2
  window.addEventListener("storage", (e) => {
3
- e.key === null || u[e.key] === void 0 || u[e.key](e);
3
+ e.key === null || h[e.key] === void 0 || h[e.key](e);
4
4
  });
5
- class g {
6
- constructor(s, i) {
5
+ class f {
6
+ constructor(s, r) {
7
7
  if (this._defaultValue = s, this.subscribers = /* @__PURE__ */ new Set(), this.save = () => {
8
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
9
  this.subscribers.delete(t);
10
- }), this.set = (t, c) => {
11
- const n = typeof t == "function" ? t(this.value) : t;
12
- n === this.value || n === void 0 || typeof n == "number" && isNaN(n) || (this.value = n, this.subscribers.forEach(this.invokeSubscriber, this), c !== !0 && this.save(n));
10
+ }), this.set = (t, u) => {
11
+ const i = typeof t == "function" ? t(this.value) : t;
12
+ i === this.value || i === void 0 || typeof i == "number" && isNaN(i) || (this.value = i, this.subscribers.forEach(this.invokeSubscriber, this), u !== !0 && this.save(i));
13
+ }, this.setDeferred = (t, u = 500, i, b = !0) => {
14
+ b && this.debounceTimeout === void 0 && this.set(t, i), clearTimeout(this.debounceTimeout), this.debounceTimeout = setTimeout(() => {
15
+ this.set(t, i), delete this.debounceTimeout;
16
+ }, u);
13
17
  }, this.value = s, typeof s != "boolean" && (this.toggle = () => {
14
18
  }), typeof s != "number" && (this.inkrement = () => {
15
19
  }), this.reset = () => {
16
20
  this.set(s, !0), this.subscribers.forEach(this.invokeSubscriber, this);
17
- }, i == null) return;
18
- let o = null, l = !0, h = !0;
19
- if (typeof i == "string" ? o = i : i.storeKey !== void 0 && (l = i.warnOnDuplicateStoreKey ?? !0, h = i.listenStorageChanges ?? !0, o = i.storeKey), o === null) return;
20
- const r = `atom/${o}`;
21
- this.value = r in localStorage ? JSON.parse(localStorage[r]) : s, this.save = (t) => {
21
+ }, r == null) return;
22
+ let n = null, c = !0, l = !0;
23
+ if (typeof r == "string" ? n = r : r.storeKey !== void 0 && (c = r.warnOnDuplicateStoreKey ?? !0, l = r.listenStorageChanges ?? !0, n = r.storeKey), n === null) return;
24
+ const o = `atom/${n}`;
25
+ this.value = o in localStorage ? JSON.parse(localStorage[o]) : s, this.save = (t) => {
22
26
  if (t === s) {
23
27
  this.reset();
24
28
  return;
25
29
  }
26
- localStorage[r] = JSON.stringify(t);
30
+ localStorage[o] = JSON.stringify(t);
27
31
  }, this.reset = () => {
28
- delete localStorage[r], this.set(s, !0);
29
- }, l && u[r] !== void 0 && console.warn("Duplicate Atom key", i), h && (u[r] = (t) => {
32
+ delete localStorage[o], this.set(s, !0);
33
+ }, c && h[o] !== void 0 && console.warn("Duplicate Atom key", r), l && (h[o] = (t) => {
30
34
  if (t.newValue === null) {
31
35
  this.reset();
32
36
  return;
@@ -45,15 +49,16 @@ class g {
45
49
  let a = () => {
46
50
  throw "call configureAtomaric() before all!";
47
51
  };
48
- const S = (e) => a = e.useSyncExternalStore, b = (e) => a(e.subscribe, e.get), f = (e) => e.set, m = (e) => e.get, v = (e) => e.toggle, k = (e) => e.inkrement, w = (e) => [b(e), f(e)], y = (e, s) => new g(e, s);
52
+ const S = (e) => a = e.useSyncExternalStore, g = (e) => a(e.subscribe, e.get), m = (e) => e.set, d = (e) => e.setDeferred, v = (e) => e.get, k = (e) => e.toggle, w = (e) => e.inkrement, y = (e) => [g(e), m(e)], A = (e, s) => new f(e, s);
49
53
  export {
50
- g as Atom,
51
- y as atom,
54
+ f as Atom,
55
+ A as atom,
52
56
  S as configureAtomaric,
53
- w as useAtom,
54
- m as useAtomGet,
55
- k as useAtomInkrement,
56
- f as useAtomSet,
57
- v as useAtomToggle,
58
- b as useAtomValue
57
+ y as useAtom,
58
+ v as useAtomGet,
59
+ w as useAtomInkrement,
60
+ m as useAtomSet,
61
+ d as useAtomSetDeferred,
62
+ k as useAtomToggle,
63
+ g as useAtomValue
59
64
  };
@@ -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 c{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,b)=>{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),b!==!0&&this.save(u))},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,g=!0,m=!0;if(typeof o=="string"?l=o:o.storeKey!==void 0&&(g=o.warnOnDuplicateStoreKey??!0,m=o.listenStorageChanges??!0,l=o.storeKey),l===null)return;const r=`atom/${l}`;this.value=r in localStorage?JSON.parse(localStorage[r]):i,this.save=t=>{if(t===i){this.reset();return}localStorage[r]=JSON.stringify(t)},this.reset=()=>{delete localStorage[r],this.set(i,!0)},g&&n[r]!==void 0&&console.warn("Duplicate Atom key",o),m&&(n[r]=t=>{if(t.newValue===null){this.reset();return}try{this.set(JSON.parse(t.newValue))}catch{console.warn("Invalid json value",t.newValue)}})}get defaultValue(){return this._defaultValue}}let a=()=>{throw"call configureAtomaric() before all!"};const S=e=>a=e.useSyncExternalStore,h=e=>a(e.subscribe,e.get),f=e=>e.set,d=e=>e.get,A=e=>e.toggle,v=e=>e.inkrement,y=e=>[h(e),f(e)],k=(e,i)=>new c(e,i);s.Atom=c,s.atom=k,s.configureAtomaric=S,s.useAtom=y,s.useAtomGet=d,s.useAtomInkrement=v,s.useAtomSet=f,s.useAtomToggle=A,s.useAtomValue=h,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",e=>{e.key===null||r[e.key]===void 0||r[e.key](e)});class l{constructor(i,n){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,h)=>{const o=typeof t=="function"?t(this.value):t;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=(t,h=500,o,T=!0)=>{T&&this.debounceTimeout===void 0&&this.set(t,o),clearTimeout(this.debounceTimeout),this.debounceTimeout=setTimeout(()=>{this.set(t,o),delete this.debounceTimeout},h)},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 c=null,b=!0,d=!0;if(typeof n=="string"?c=n:n.storeKey!==void 0&&(b=n.warnOnDuplicateStoreKey??!0,d=n.listenStorageChanges??!0,c=n.storeKey),c===null)return;const u=`atom/${c}`;this.value=u in localStorage?JSON.parse(localStorage[u]):i,this.save=t=>{if(t===i){this.reset();return}localStorage[u]=JSON.stringify(t)},this.reset=()=>{delete localStorage[u],this.set(i,!0)},b&&r[u]!==void 0&&console.warn("Duplicate Atom key",n),d&&(r[u]=t=>{if(t.newValue===null){this.reset();return}try{this.set(JSON.parse(t.newValue))}catch{console.warn("Invalid json value",t.newValue)}})}get defaultValue(){return this._defaultValue}}let a=()=>{throw"call configureAtomaric() before all!"};const g=e=>a=e.useSyncExternalStore,f=e=>a(e.subscribe,e.get),m=e=>e.set,S=e=>e.setDeferred,A=e=>e.get,y=e=>e.toggle,v=e=>e.inkrement,k=e=>[f(e),m(e)],w=(e,i)=>new l(e,i);s.Atom=l,s.atom=w,s.configureAtomaric=g,s.useAtom=k,s.useAtomGet=A,s.useAtomInkrement=v,s.useAtomSet=m,s.useAtomSetDeferred=S,s.useAtomToggle=y,s.useAtomValue=f,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.5",
4
+ "version": "0.0.6",
5
5
  "type": "module",
6
6
  "main": "./build/atomaric.umd.cjs",
7
7
  "module": "./build/atomaric.js",
package/types/model.d.ts CHANGED
@@ -10,20 +10,32 @@ export type AtomOptions = {
10
10
  listenStorageChanges?: boolean;
11
11
  };
12
12
 
13
+ export type AtomSetMethod<Value> = (value: Value | ((prev: Value) => Value), isPreventSave?: boolean) => void;
14
+ export type AtomSetDeferredMethod<Value> = (
15
+ value: Value | ((prev: Value) => Value),
16
+ debounceMs?: number,
17
+ isPreventSave?: boolean,
18
+ isInitInvoke?: boolean,
19
+ ) => void;
20
+
21
+ export type AtomSubscribeMethod<Value> = (subscriber: Sunscriber<Value>) => () => void;
22
+
13
23
  export class Atom<Value> {
14
24
  constructor(defaultValue: Value, storeKeyOrOptions: AtomStoreKey | undefined | AtomOptions);
15
25
 
16
26
  readonly defaultValue: Value;
17
27
  readonly get: () => Value;
28
+ readonly set: AtomSetMethod<Value>;
29
+ readonly setDeferred: AtomSetDeferredMethod<Value>;
18
30
  readonly reset: () => void;
19
31
  readonly toggle: () => void;
20
32
  readonly inkrement: (delta: number) => void;
21
- readonly subscribe: (sub: Sunscriber<Value>) => () => void;
22
- readonly set: (value: Value | ((prev: Value) => Value), isPreventSave?: boolean) => void;
33
+ readonly subscribe: AtomSubscribeMethod<Value>;
23
34
  }
24
35
 
25
36
  export function useAtomValue<Value>(atom: Atom<Value>): Value;
26
37
  export function useAtomSet<Value>(atom: Atom<Value>): (typeof atom)['set'];
38
+ export function useAtomSetDeferred<Value>(atom: Atom<Value>): (typeof atom)['setDeferred'];
27
39
  export function useAtomGet<Value>(atom: Atom<Value>): (typeof atom)['get'];
28
40
  export function useAtomToggle(atom: Atom<boolean>): (typeof atom)['toggle'];
29
41
  export function useAtomInkrement(atom: Atom<number>): (typeof atom)['inkrement'];