atomaric 0.0.6 → 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/build/atomaric.js +35 -28
- package/build/atomaric.umd.cjs +1 -1
- package/package.json +1 -1
- package/types/model.d.ts +5 -3
package/build/atomaric.js
CHANGED
|
@@ -2,41 +2,48 @@ const h = {};
|
|
|
2
2
|
window.addEventListener("storage", (e) => {
|
|
3
3
|
e.key === null || h[e.key] === void 0 || h[e.key](e);
|
|
4
4
|
});
|
|
5
|
-
class
|
|
6
|
-
constructor(s,
|
|
5
|
+
class m {
|
|
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
|
-
|
|
13
|
-
}, this.setDeferred = (t,
|
|
14
|
-
|
|
15
|
-
this.set(t,
|
|
16
|
-
},
|
|
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);
|
|
17
17
|
}, this.value = s, typeof s != "boolean" && (this.toggle = () => {
|
|
18
18
|
}), typeof s != "number" && (this.inkrement = () => {
|
|
19
19
|
}), this.reset = () => {
|
|
20
20
|
this.set(s, !0), this.subscribers.forEach(this.invokeSubscriber, this);
|
|
21
|
-
},
|
|
22
|
-
let
|
|
23
|
-
if (typeof
|
|
24
|
-
const o = `atom/${
|
|
25
|
-
this.
|
|
21
|
+
}, i == null) return;
|
|
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) => {
|
|
26
33
|
if (t === s) {
|
|
27
34
|
this.reset();
|
|
28
35
|
return;
|
|
29
36
|
}
|
|
30
|
-
localStorage[o] =
|
|
37
|
+
localStorage[o] = a(t);
|
|
31
38
|
}, this.reset = () => {
|
|
32
39
|
delete localStorage[o], this.set(s, !0);
|
|
33
|
-
}, c && h[o] !== void 0 && console.warn("Duplicate Atom key",
|
|
40
|
+
}, c && h[o] !== void 0 && console.warn("Duplicate Atom key", i), g && (h[o] = (t) => {
|
|
34
41
|
if (t.newValue === null) {
|
|
35
42
|
this.reset();
|
|
36
43
|
return;
|
|
37
44
|
}
|
|
38
45
|
try {
|
|
39
|
-
this.set(
|
|
46
|
+
this.set(n(t.newValue));
|
|
40
47
|
} catch {
|
|
41
48
|
console.warn("Invalid json value", t.newValue);
|
|
42
49
|
}
|
|
@@ -46,19 +53,19 @@ class f {
|
|
|
46
53
|
return this._defaultValue;
|
|
47
54
|
}
|
|
48
55
|
}
|
|
49
|
-
let
|
|
56
|
+
let b = () => {
|
|
50
57
|
throw "call configureAtomaric() before all!";
|
|
51
58
|
};
|
|
52
|
-
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);
|
|
53
60
|
export {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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,
|
|
62
69
|
k as useAtomToggle,
|
|
63
|
-
|
|
70
|
+
S as useAtomValue
|
|
64
71
|
};
|
package/build/atomaric.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(s,
|
|
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,12 +2,14 @@ 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
|
|
|
13
15
|
export type AtomSetMethod<Value> = (value: Value | ((prev: Value) => Value), isPreventSave?: boolean) => void;
|
|
@@ -21,7 +23,7 @@ export type AtomSetDeferredMethod<Value> = (
|
|
|
21
23
|
export type AtomSubscribeMethod<Value> = (subscriber: Sunscriber<Value>) => () => void;
|
|
22
24
|
|
|
23
25
|
export class Atom<Value> {
|
|
24
|
-
constructor(defaultValue: Value, storeKeyOrOptions: AtomStoreKey | undefined | AtomOptions);
|
|
26
|
+
constructor(defaultValue: Value, storeKeyOrOptions: AtomStoreKey | undefined | AtomOptions<Value>);
|
|
25
27
|
|
|
26
28
|
readonly defaultValue: Value;
|
|
27
29
|
readonly get: () => Value;
|
|
@@ -44,7 +46,7 @@ export function useAtom<Value>(atom: Atom<Value>): [Value, (typeof atom)['set']]
|
|
|
44
46
|
|
|
45
47
|
export function atom<Value>(
|
|
46
48
|
value: Value,
|
|
47
|
-
storeKeyOrOptions?: `${string}${string}:${string}${string}` | AtomOptions
|
|
49
|
+
storeKeyOrOptions?: `${string}${string}:${string}${string}` | AtomOptions<Value>,
|
|
48
50
|
): Atom<Value>;
|
|
49
51
|
|
|
50
52
|
export function configureAtomaric(hooks: { useSyncExternalStore: typeof useSyncExternalStore }): void;
|