atomaric 0.0.5 → 0.0.7
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 +37 -25
- package/build/atomaric.umd.cjs +1 -1
- package/package.json +1 -1
- package/types/model.d.ts +19 -5
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,38 +1,49 @@
|
|
|
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
|
|
5
|
+
class m {
|
|
6
6
|
constructor(s, i) {
|
|
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, l) => {
|
|
11
|
+
const r = typeof t == "function" ? t(this.value) : t;
|
|
12
|
+
r === this.value || r === void 0 || typeof r == "number" && isNaN(r) || (this.value = r, this.subscribers.forEach(this.invokeSubscriber, this), l !== !0 && this.save(r));
|
|
13
|
+
}, this.setDeferred = (t, l = 500, r, f = !0) => {
|
|
14
|
+
f && this.debounceTimeout === void 0 && this.set(t, r), clearTimeout(this.debounceTimeout), this.debounceTimeout = setTimeout(() => {
|
|
15
|
+
this.set(t, r), delete this.debounceTimeout;
|
|
16
|
+
}, l);
|
|
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
21
|
}, i == null) return;
|
|
18
|
-
let
|
|
19
|
-
if (typeof i == "string" ?
|
|
20
|
-
const
|
|
21
|
-
this.
|
|
22
|
+
let u = null, c = !0, g = !0, n = (t) => JSON.parse(t), a = (t) => JSON.stringify(t);
|
|
23
|
+
if (typeof i == "string" ? u = i : i.storeKey !== void 0 && (c = i.warnOnDuplicateStoreKey ?? !0, g = i.listenStorageChanges ?? !0, u = i.storeKey, n = i.parseValue ?? n, a = i.stringifyValue ?? a), u === null) return;
|
|
24
|
+
const o = `atom/${u}`;
|
|
25
|
+
this.get = () => {
|
|
26
|
+
try {
|
|
27
|
+
this.value = o in localStorage ? n(localStorage[o]) : s, this.get = () => this.value;
|
|
28
|
+
} catch {
|
|
29
|
+
console.warn("Invalid json value", localStorage[o]);
|
|
30
|
+
}
|
|
31
|
+
return this.value;
|
|
32
|
+
}, this.save = (t) => {
|
|
22
33
|
if (t === s) {
|
|
23
34
|
this.reset();
|
|
24
35
|
return;
|
|
25
36
|
}
|
|
26
|
-
localStorage[
|
|
37
|
+
localStorage[o] = a(t);
|
|
27
38
|
}, this.reset = () => {
|
|
28
|
-
delete localStorage[
|
|
29
|
-
},
|
|
39
|
+
delete localStorage[o], this.set(s, !0);
|
|
40
|
+
}, c && h[o] !== void 0 && console.warn("Duplicate Atom key", i), g && (h[o] = (t) => {
|
|
30
41
|
if (t.newValue === null) {
|
|
31
42
|
this.reset();
|
|
32
43
|
return;
|
|
33
44
|
}
|
|
34
45
|
try {
|
|
35
|
-
this.set(
|
|
46
|
+
this.set(n(t.newValue));
|
|
36
47
|
} catch {
|
|
37
48
|
console.warn("Invalid json value", t.newValue);
|
|
38
49
|
}
|
|
@@ -42,18 +53,19 @@ class g {
|
|
|
42
53
|
return this._defaultValue;
|
|
43
54
|
}
|
|
44
55
|
}
|
|
45
|
-
let
|
|
56
|
+
let b = () => {
|
|
46
57
|
throw "call configureAtomaric() before all!";
|
|
47
58
|
};
|
|
48
|
-
const
|
|
59
|
+
const v = (e) => b = e.useSyncExternalStore, S = (e) => b(e.subscribe, e.get), d = (e) => e.set, w = (e) => e.setDeferred, y = (e) => e.get, k = (e) => e.toggle, A = (e) => e.inkrement, T = (e) => [S(e), d(e)], D = (e, s) => new m(e, s);
|
|
49
60
|
export {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
61
|
+
m as Atom,
|
|
62
|
+
D as atom,
|
|
63
|
+
v as configureAtomaric,
|
|
64
|
+
T as useAtom,
|
|
65
|
+
y as useAtomGet,
|
|
66
|
+
A as useAtomInkrement,
|
|
67
|
+
d as useAtomSet,
|
|
68
|
+
w as useAtomSetDeferred,
|
|
69
|
+
k as useAtomToggle,
|
|
70
|
+
S as useAtomValue
|
|
59
71
|
};
|
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 f{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,c)=>{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),c!==!0&&this.save(u))},this.setDeferred=(t,c=500,u,E=!0)=>{E&&this.debounceTimeout===void 0&&this.set(t,u),clearTimeout(this.debounceTimeout),this.debounceTimeout=setTimeout(()=>{this.set(t,u),delete this.debounceTimeout},c)},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,b=!0,S=!0,a=t=>JSON.parse(t),h=t=>JSON.stringify(t);if(typeof o=="string"?l=o:o.storeKey!==void 0&&(b=o.warnOnDuplicateStoreKey??!0,S=o.listenStorageChanges??!0,l=o.storeKey,a=o.parseValue??a,h=o.stringifyValue??h),l===null)return;const r=`atom/${l}`;this.get=()=>{try{this.value=r in localStorage?a(localStorage[r]):i,this.get=()=>this.value}catch{console.warn("Invalid json value",localStorage[r])}return this.value},this.save=t=>{if(t===i){this.reset();return}localStorage[r]=h(t)},this.reset=()=>{delete localStorage[r],this.set(i,!0)},b&&n[r]!==void 0&&console.warn("Duplicate Atom key",o),S&&(n[r]=t=>{if(t.newValue===null){this.reset();return}try{this.set(a(t.newValue))}catch{console.warn("Invalid json value",t.newValue)}})}get defaultValue(){return this._defaultValue}}let m=()=>{throw"call configureAtomaric() before all!"};const A=e=>m=e.useSyncExternalStore,g=e=>m(e.subscribe,e.get),d=e=>e.set,v=e=>e.setDeferred,y=e=>e.get,k=e=>e.toggle,w=e=>e.inkrement,T=e=>[g(e),d(e)],D=(e,i)=>new f(e,i);s.Atom=f,s.atom=D,s.configureAtomaric=A,s.useAtom=T,s.useAtomGet=y,s.useAtomInkrement=w,s.useAtomSet=d,s.useAtomSetDeferred=v,s.useAtomToggle=k,s.useAtomValue=g,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
package/types/model.d.ts
CHANGED
|
@@ -2,28 +2,42 @@ type Sunscriber<Value> = (value: Value) => void;
|
|
|
2
2
|
|
|
3
3
|
export type AtomStoreKey = `${string}${string}:${string}${string}`;
|
|
4
4
|
|
|
5
|
-
export type AtomOptions = {
|
|
5
|
+
export type AtomOptions<Value> = {
|
|
6
6
|
storeKey?: AtomStoreKey;
|
|
7
7
|
/** **default: true** */
|
|
8
8
|
warnOnDuplicateStoreKey?: boolean;
|
|
9
9
|
/** **default: true** */
|
|
10
10
|
listenStorageChanges?: boolean;
|
|
11
|
+
parseValue?: (stringifiedValue: string) => Value;
|
|
12
|
+
stringifyValue?: (value: Value) => string;
|
|
11
13
|
};
|
|
12
14
|
|
|
15
|
+
export type AtomSetMethod<Value> = (value: Value | ((prev: Value) => Value), isPreventSave?: boolean) => void;
|
|
16
|
+
export type AtomSetDeferredMethod<Value> = (
|
|
17
|
+
value: Value | ((prev: Value) => Value),
|
|
18
|
+
debounceMs?: number,
|
|
19
|
+
isPreventSave?: boolean,
|
|
20
|
+
isInitInvoke?: boolean,
|
|
21
|
+
) => void;
|
|
22
|
+
|
|
23
|
+
export type AtomSubscribeMethod<Value> = (subscriber: Sunscriber<Value>) => () => void;
|
|
24
|
+
|
|
13
25
|
export class Atom<Value> {
|
|
14
|
-
constructor(defaultValue: Value, storeKeyOrOptions: AtomStoreKey | undefined | AtomOptions);
|
|
26
|
+
constructor(defaultValue: Value, storeKeyOrOptions: AtomStoreKey | undefined | AtomOptions<Value>);
|
|
15
27
|
|
|
16
28
|
readonly defaultValue: Value;
|
|
17
29
|
readonly get: () => Value;
|
|
30
|
+
readonly set: AtomSetMethod<Value>;
|
|
31
|
+
readonly setDeferred: AtomSetDeferredMethod<Value>;
|
|
18
32
|
readonly reset: () => void;
|
|
19
33
|
readonly toggle: () => void;
|
|
20
34
|
readonly inkrement: (delta: number) => void;
|
|
21
|
-
readonly subscribe:
|
|
22
|
-
readonly set: (value: Value | ((prev: Value) => Value), isPreventSave?: boolean) => void;
|
|
35
|
+
readonly subscribe: AtomSubscribeMethod<Value>;
|
|
23
36
|
}
|
|
24
37
|
|
|
25
38
|
export function useAtomValue<Value>(atom: Atom<Value>): Value;
|
|
26
39
|
export function useAtomSet<Value>(atom: Atom<Value>): (typeof atom)['set'];
|
|
40
|
+
export function useAtomSetDeferred<Value>(atom: Atom<Value>): (typeof atom)['setDeferred'];
|
|
27
41
|
export function useAtomGet<Value>(atom: Atom<Value>): (typeof atom)['get'];
|
|
28
42
|
export function useAtomToggle(atom: Atom<boolean>): (typeof atom)['toggle'];
|
|
29
43
|
export function useAtomInkrement(atom: Atom<number>): (typeof atom)['inkrement'];
|
|
@@ -32,7 +46,7 @@ export function useAtom<Value>(atom: Atom<Value>): [Value, (typeof atom)['set']]
|
|
|
32
46
|
|
|
33
47
|
export function atom<Value>(
|
|
34
48
|
value: Value,
|
|
35
|
-
storeKeyOrOptions?: `${string}${string}:${string}${string}` | AtomOptions
|
|
49
|
+
storeKeyOrOptions?: `${string}${string}:${string}${string}` | AtomOptions<Value>,
|
|
36
50
|
): Atom<Value>;
|
|
37
51
|
|
|
38
52
|
export function configureAtomaric(hooks: { useSyncExternalStore: typeof useSyncExternalStore }): void;
|