atomaric 0.0.4 → 0.0.5

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
@@ -6,14 +6,24 @@ npm install atomaric
6
6
 
7
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 locakStorage 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
@@ -7,17 +7,17 @@ class g {
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, 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));
13
13
  }, this.value = s, typeof s != "boolean" && (this.toggle = () => {
14
14
  }), typeof s != "number" && (this.inkrement = () => {
15
15
  }), this.reset = () => {
16
16
  this.set(s, !0), this.subscribers.forEach(this.invokeSubscriber, this);
17
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}`;
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
21
  this.value = r in localStorage ? JSON.parse(localStorage[r]) : s, this.save = (t) => {
22
22
  if (t === s) {
23
23
  this.reset();
@@ -26,7 +26,7 @@ class g {
26
26
  localStorage[r] = JSON.stringify(t);
27
27
  }, this.reset = () => {
28
28
  delete localStorage[r], this.set(s, !0);
29
- }, a && u[r] !== void 0 && console.warn("Duplicate Atom key", i), h && (u[r] = (t) => {
29
+ }, l && u[r] !== void 0 && console.warn("Duplicate Atom key", i), h && (u[r] = (t) => {
30
30
  if (t.newValue === null) {
31
31
  this.reset();
32
32
  return;
@@ -42,18 +42,18 @@ class g {
42
42
  return this._defaultValue;
43
43
  }
44
44
  }
45
- let c = () => {
46
- throw "you meed pass react useSyncExternalStore hook func in registerReactHooks() before all actions";
45
+ let a = () => {
46
+ throw "call configureAtomaric() before all!";
47
47
  };
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);
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);
49
49
  export {
50
50
  g as Atom,
51
- w as atom,
52
- f as registerReactHooks,
53
- y as useAtom,
54
- k as useAtomGet,
55
- v as useAtomInkrement,
56
- S as useAtomSet,
57
- m as useAtomToggle,
51
+ y as atom,
52
+ S as configureAtomaric,
53
+ w as useAtom,
54
+ m as useAtomGet,
55
+ k as useAtomInkrement,
56
+ f as useAtomSet,
57
+ v as useAtomToggle,
58
58
  b as useAtomValue
59
59
  };
@@ -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,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"})});
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.5",
5
5
  "type": "module",
6
6
  "main": "./build/atomaric.umd.cjs",
7
7
  "module": "./build/atomaric.js",
package/types/model.d.ts CHANGED
@@ -35,4 +35,4 @@ export function atom<Value>(
35
35
  storeKeyOrOptions?: `${string}${string}:${string}${string}` | AtomOptions,
36
36
  ): Atom<Value>;
37
37
 
38
- export function registerReactHooks(hooks: { useSyncExternalStore: typeof useSyncExternalStore }): void;
38
+ export function configureAtomaric(hooks: { useSyncExternalStore: typeof useSyncExternalStore }): void;