atomaric 0.0.3 → 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 +15 -5
- package/build/atomaric.js +17 -17
- package/build/atomaric.umd.cjs +1 -1
- package/package.json +1 -1
- package/types/model.d.ts +6 -3
package/README.md
CHANGED
|
@@ -6,14 +6,24 @@ npm install atomaric
|
|
|
6
6
|
|
|
7
7
|
### usage
|
|
8
8
|
|
|
9
|
-
```
|
|
10
|
-
import { atom,
|
|
9
|
+
```tsx
|
|
10
|
+
import { atom, useAtom, configureAtomaric } from 'atomaric';
|
|
11
|
+
import { useSyncExternalStore } from 'react';
|
|
11
12
|
|
|
12
|
-
|
|
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] =
|
|
21
|
+
const [name, setIsOpen] = useAtom(nameAtom);
|
|
16
22
|
|
|
17
|
-
return
|
|
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,
|
|
11
|
-
const
|
|
12
|
-
|
|
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
|
|
19
|
-
if (typeof i == "string" ?
|
|
20
|
-
const r = `atom/${
|
|
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
|
-
},
|
|
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
|
|
46
|
-
throw "
|
|
45
|
+
let a = () => {
|
|
46
|
+
throw "call configureAtomaric() before all!";
|
|
47
47
|
};
|
|
48
|
-
const
|
|
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
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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
|
};
|
package/build/atomaric.umd.cjs
CHANGED
|
@@ -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
|
|
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
package/types/model.d.ts
CHANGED
|
@@ -11,7 +11,7 @@ export type AtomOptions = {
|
|
|
11
11
|
};
|
|
12
12
|
|
|
13
13
|
export class Atom<Value> {
|
|
14
|
-
constructor(defaultValue: Value,
|
|
14
|
+
constructor(defaultValue: Value, storeKeyOrOptions: AtomStoreKey | undefined | AtomOptions);
|
|
15
15
|
|
|
16
16
|
readonly defaultValue: Value;
|
|
17
17
|
readonly get: () => Value;
|
|
@@ -30,6 +30,9 @@ export function useAtomInkrement(atom: Atom<number>): (typeof atom)['inkrement']
|
|
|
30
30
|
|
|
31
31
|
export function useAtom<Value>(atom: Atom<Value>): [Value, (typeof atom)['set']];
|
|
32
32
|
|
|
33
|
-
export function atom<Value>(
|
|
33
|
+
export function atom<Value>(
|
|
34
|
+
value: Value,
|
|
35
|
+
storeKeyOrOptions?: `${string}${string}:${string}${string}` | AtomOptions,
|
|
36
|
+
): Atom<Value>;
|
|
34
37
|
|
|
35
|
-
export function
|
|
38
|
+
export function configureAtomaric(hooks: { useSyncExternalStore: typeof useSyncExternalStore }): void;
|