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 +3 -3
- package/build/atomaric.js +29 -24
- package/build/atomaric.umd.cjs +1 -1
- package/package.json +1 -1
- package/types/model.d.ts +14 -2
package/README.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
## 🕹️Installation
|
|
1
|
+
## 🕹️Installation
|
|
2
2
|
|
|
3
3
|
```sh
|
|
4
4
|
npm install atomaric
|
|
5
5
|
```
|
|
6
6
|
|
|
7
|
-
|
|
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
|
|
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
|
|
1
|
+
const h = {};
|
|
2
2
|
window.addEventListener("storage", (e) => {
|
|
3
|
-
e.key === null ||
|
|
3
|
+
e.key === null || h[e.key] === void 0 || h[e.key](e);
|
|
4
4
|
});
|
|
5
|
-
class
|
|
6
|
-
constructor(s,
|
|
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,
|
|
11
|
-
const
|
|
12
|
-
|
|
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
|
-
},
|
|
18
|
-
let
|
|
19
|
-
if (typeof
|
|
20
|
-
const
|
|
21
|
-
this.value =
|
|
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[
|
|
30
|
+
localStorage[o] = JSON.stringify(t);
|
|
27
31
|
}, this.reset = () => {
|
|
28
|
-
delete localStorage[
|
|
29
|
-
},
|
|
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,
|
|
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
|
-
|
|
51
|
-
|
|
54
|
+
f as Atom,
|
|
55
|
+
A as atom,
|
|
52
56
|
S as configureAtomaric,
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
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
|
};
|
package/build/atomaric.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(s,
|
|
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
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:
|
|
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'];
|