atomaric 0.0.4 → 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,19 +1,29 @@
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
- ```ts
10
- import { atom, useAtomValue } from 'atomaric';
9
+ ```tsx
10
+ import { atom, useAtom, configureAtomaric } from 'atomaric';
11
+ import { useSyncExternalStore } from 'react';
11
12
 
12
- const nameAtom = atom('World');
13
+ configureAtomaric({ useSyncExternalStore }); // do this before all
14
+
15
+ const nameAtom = atom(
16
+ 'World',
17
+ 'greats:name', // optional localStorage key
18
+ );
13
19
 
14
20
  function App() {
15
- const [name, setIsOpen] = useAtomValue(nameAtom);
21
+ const [name, setIsOpen] = useAtom(nameAtom);
16
22
 
17
- return <div onClick={() => setIsOpen(isOpen => (name === 'World' ? 'Man' : 'World'))}>Hello, {name}</div>;
23
+ return (
24
+ <div onClick={() => setIsOpen(isOpen => (name === 'World' ? 'Man' : 'World'))}>
25
+ Hello <span className="color-accent">{name}</span>
26
+ </div>
27
+ );
18
28
  }
19
29
  ```
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, l) => {
11
- const o = typeof t == "function" ? t(this.value) : t;
12
- o === this.value || o === void 0 || typeof o == "number" && isNaN(o) || (this.value = o, this.subscribers.forEach(this.invokeSubscriber, this), l !== !0 && this.save(o));
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 n = null, a = !0, h = !0;
19
- if (typeof i == "string" ? n = i : i.storeKey !== void 0 && (a = i.warnOnDuplicateStoreKey ?? !0, h = i.listenStorageChanges ?? !0, n = i.storeKey), n === null) return;
20
- const r = `atom/${n}`;
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
- }, a && 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;
@@ -42,18 +46,19 @@ class g {
42
46
  return this._defaultValue;
43
47
  }
44
48
  }
45
- let c = () => {
46
- throw "you meed pass react useSyncExternalStore hook func in registerReactHooks() before all actions";
49
+ let a = () => {
50
+ throw "call configureAtomaric() before all!";
47
51
  };
48
- const f = (e) => c = e.useSyncExternalStore, b = (e) => c(e.subscribe, e.get), S = (e) => e.set, k = (e) => e.get, m = (e) => e.toggle, v = (e) => e.inkrement, y = (e) => [b(e), S(e)], w = (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
- w as atom,
52
- f as registerReactHooks,
54
+ f as Atom,
55
+ A as atom,
56
+ S as configureAtomaric,
53
57
  y as useAtom,
54
- k as useAtomGet,
55
- v as useAtomInkrement,
56
- S as useAtomSet,
57
- m as useAtomToggle,
58
- b as useAtomValue
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 a{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 c=()=>{throw"you meed pass react useSyncExternalStore hook func in registerReactHooks() before all actions"};const S=e=>c=e.useSyncExternalStore,h=e=>c(e.subscribe,e.get),f=e=>e.set,d=e=>e.get,y=e=>e.toggle,k=e=>e.inkrement,v=e=>[h(e),f(e)],A=(e,i)=>new a(e,i);s.Atom=a,s.atom=A,s.registerReactHooks=S,s.useAtom=v,s.useAtomGet=d,s.useAtomInkrement=k,s.useAtomSet=f,s.useAtomToggle=y,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.4",
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'];
@@ -35,4 +47,4 @@ export function atom<Value>(
35
47
  storeKeyOrOptions?: `${string}${string}:${string}${string}` | AtomOptions,
36
48
  ): Atom<Value>;
37
49
 
38
- export function registerReactHooks(hooks: { useSyncExternalStore: typeof useSyncExternalStore }): void;
50
+ export function configureAtomaric(hooks: { useSyncExternalStore: typeof useSyncExternalStore }): void;