atomaric 0.0.14 → 0.0.16
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 +93 -90
- package/build/atomaric.umd.cjs +1 -1
- package/package.json +1 -1
- package/types/model.d.ts +21 -11
package/build/atomaric.js
CHANGED
|
@@ -1,129 +1,132 @@
|
|
|
1
1
|
class C {
|
|
2
|
-
constructor(
|
|
3
|
-
let
|
|
2
|
+
constructor(s, o) {
|
|
3
|
+
let _ = s, g, p = () => {
|
|
4
4
|
};
|
|
5
|
-
const
|
|
6
|
-
this.get = () =>
|
|
7
|
-
let
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
5
|
+
const D = (e) => _ = e, m = () => _;
|
|
6
|
+
this.get = () => m(), Object.defineProperty(this, "do", { get: () => N() }), Object.defineProperty(this, "defaultValue", { get: () => s });
|
|
7
|
+
let N = () => {
|
|
8
|
+
let e = null;
|
|
9
|
+
typeof s == "number" ? e = w(
|
|
10
|
+
{
|
|
11
|
+
increment: (n) => {
|
|
12
|
+
i(+this.get() + (n ?? 0));
|
|
13
|
+
}
|
|
13
14
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
) : typeof s == "boolean" ? e = w(
|
|
16
|
+
{
|
|
17
|
+
toggle: () => {
|
|
18
|
+
i(!this.get());
|
|
19
|
+
}
|
|
17
20
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
21
|
+
) : Array.isArray(s) ? e = w(
|
|
22
|
+
{
|
|
23
|
+
push: (...n) => {
|
|
24
|
+
i([this.get()].flat().concat(n));
|
|
25
|
+
},
|
|
26
|
+
unshift: (...n) => {
|
|
27
|
+
i(n.concat(this.get()));
|
|
28
|
+
},
|
|
29
|
+
update: (n) => {
|
|
30
|
+
const r = [...this.get()];
|
|
31
|
+
n(r), i(r);
|
|
32
|
+
},
|
|
33
|
+
filter: (n) => {
|
|
34
|
+
i(this.get().filter(n ?? I));
|
|
35
|
+
}
|
|
31
36
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
37
|
+
) : s instanceof Set && (e = w(
|
|
38
|
+
{
|
|
39
|
+
add: (n) => {
|
|
40
|
+
const r = new Set(this.get());
|
|
41
|
+
r.add(n), i(r);
|
|
42
|
+
},
|
|
43
|
+
delete: (n) => {
|
|
44
|
+
const r = new Set(this.get());
|
|
45
|
+
r.delete(n), i(r);
|
|
46
|
+
},
|
|
47
|
+
clear: () => {
|
|
48
|
+
i(/* @__PURE__ */ new Set());
|
|
49
|
+
},
|
|
50
|
+
update: (n) => {
|
|
51
|
+
const r = new Set(this.get());
|
|
52
|
+
n(r), i(r);
|
|
53
|
+
}
|
|
47
54
|
}
|
|
48
|
-
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
...c
|
|
55
|
-
};
|
|
56
|
-
return p = () => d, d;
|
|
55
|
+
));
|
|
56
|
+
const c = { ...typeof o == "object" && o != null && "do" in o ? o.do(
|
|
57
|
+
() => this.get(),
|
|
58
|
+
(n, r) => i(n, r)
|
|
59
|
+
) : {}, ...e };
|
|
60
|
+
return N = () => c, c;
|
|
57
61
|
};
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
l === this.get() || l === void 0 || typeof l == "number" && isNaN(l) || (_(l), S.forEach(D, this), c !== !0 && b(l));
|
|
62
|
+
const S = /* @__PURE__ */ new Set(), j = (e) => e(this.get());
|
|
63
|
+
this.subscribe = (e) => (S.add(e), () => (S.delete(e), void 0));
|
|
64
|
+
const i = (e, a) => {
|
|
65
|
+
const c = typeof e == "function" ? e(this.get()) : e;
|
|
66
|
+
c === this.get() || c === void 0 || typeof c == "number" && isNaN(c) || (D(c), S.forEach(j, this), a !== !0 && p(c));
|
|
64
67
|
};
|
|
65
|
-
if (this.set = (
|
|
66
|
-
|
|
67
|
-
}, this.setDeferred = (
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
},
|
|
68
|
+
if (this.set = (e, a) => i(e, a), this.reset = () => {
|
|
69
|
+
i(s, !0), S.forEach(j, this);
|
|
70
|
+
}, this.setDeferred = (e, a = 500, c, n = !0) => {
|
|
71
|
+
n && g === void 0 && i(e, c), clearTimeout(g), g = setTimeout(() => {
|
|
72
|
+
i(e, c), g = void 0;
|
|
73
|
+
}, a);
|
|
71
74
|
}, o == null) return;
|
|
72
|
-
let u = null,
|
|
73
|
-
if (
|
|
74
|
-
throw console.error(
|
|
75
|
-
} : (
|
|
76
|
-
if (typeof o == "string" ? u = o : "storeKey" in o && (
|
|
77
|
-
const
|
|
75
|
+
let u = null, y = !0, A = !0, d = s instanceof Set ? (e) => new Set(JSON.parse(e)) : (e) => JSON.parse(e), b = s instanceof Set ? (e) => {
|
|
76
|
+
if (e instanceof Set) return JSON.stringify(Array.from(e));
|
|
77
|
+
throw console.error(e), "The value is not Set instance";
|
|
78
|
+
} : (e) => JSON.stringify(e);
|
|
79
|
+
if (typeof o == "string" ? u = o : "storeKey" in o && (y = o.warnOnDuplicateStoreKey ?? y, A = o.listenStorageChanges ?? A, u = o.storeKey, d = o.parseValue ?? d, b = o.stringifyValue ?? b), u === null) return;
|
|
80
|
+
const l = `atom/${u}`;
|
|
78
81
|
let k = !0;
|
|
79
82
|
this.get = () => {
|
|
80
|
-
if (this.get = () =>
|
|
83
|
+
if (this.get = () => m(), k) {
|
|
81
84
|
k = !1;
|
|
82
85
|
try {
|
|
83
|
-
|
|
86
|
+
D(l in f ? d(f[l]) : s);
|
|
84
87
|
} catch {
|
|
85
|
-
console.warn("Invalid json value",
|
|
88
|
+
console.warn("Invalid json value", f[l]);
|
|
86
89
|
}
|
|
87
90
|
}
|
|
88
|
-
return
|
|
89
|
-
},
|
|
90
|
-
if (
|
|
91
|
+
return m();
|
|
92
|
+
}, p = (e) => {
|
|
93
|
+
if (e === s) {
|
|
91
94
|
this.reset();
|
|
92
95
|
return;
|
|
93
96
|
}
|
|
94
|
-
|
|
97
|
+
f[l] = b(e);
|
|
95
98
|
}, this.reset = () => {
|
|
96
|
-
delete
|
|
97
|
-
},
|
|
98
|
-
if (
|
|
99
|
+
delete f[l], i(s, !0);
|
|
100
|
+
}, y && h[l] !== void 0 && console.warn("Duplicate Atom key", u), A && (h[l] = (e) => {
|
|
101
|
+
if (e.newValue === null) {
|
|
99
102
|
this.reset();
|
|
100
103
|
return;
|
|
101
104
|
}
|
|
102
105
|
try {
|
|
103
|
-
|
|
106
|
+
i(d(e.newValue));
|
|
104
107
|
} catch {
|
|
105
|
-
console.warn("Invalid json value",
|
|
108
|
+
console.warn("Invalid json value", e.newValue);
|
|
106
109
|
}
|
|
107
110
|
});
|
|
108
111
|
}
|
|
109
112
|
}
|
|
110
|
-
const
|
|
111
|
-
window.addEventListener("storage", (
|
|
112
|
-
var
|
|
113
|
-
|
|
113
|
+
const f = window.localStorage, h = {}, I = (t) => t, w = (t, s) => t;
|
|
114
|
+
window.addEventListener("storage", (t) => {
|
|
115
|
+
var s;
|
|
116
|
+
t.key === null || t.newValue === t.oldValue || (s = h[t.key]) == null || s.call(h, t);
|
|
114
117
|
});
|
|
115
118
|
let E = () => {
|
|
116
119
|
throw "call configureAtomaric() before all!";
|
|
117
120
|
};
|
|
118
|
-
const x = (
|
|
121
|
+
const x = (t) => E = t.useSyncExternalStore, J = (t) => E(t.subscribe, t.get), T = (t) => t.set, V = (t) => t.setDeferred, v = (t) => t.get, F = (t) => t.do, G = (t) => [J(t), T(t)], L = (t, s) => new C(t, s);
|
|
119
122
|
export {
|
|
120
123
|
C as Atom,
|
|
121
|
-
|
|
124
|
+
L as atom,
|
|
122
125
|
x as configureAtomaric,
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
+
G as useAtom,
|
|
127
|
+
F as useAtomDo,
|
|
128
|
+
v as useAtomGet,
|
|
126
129
|
T as useAtomSet,
|
|
127
|
-
|
|
130
|
+
V as useAtomSetDeferred,
|
|
128
131
|
J as useAtomValue
|
|
129
132
|
};
|
package/build/atomaric.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(s,f){typeof exports=="object"&&typeof module<"u"?f(exports):typeof define=="function"&&define.amd?define(["exports"],f):(s=typeof globalThis<"u"?globalThis:s||self,f(s.atomaric={}))})(this,function(s){"use strict";class f{constructor(i,r){let k=i,m,p=()=>{};const E=e=>k=e,y=()=>k;this.get=()=>y(),Object.defineProperty(this,"do",{get:()=>C()}),Object.defineProperty(this,"defaultValue",{get:()=>i});let C=()=>{let e=null;typeof i=="number"?e=S({increment:n=>{o(+this.get()+(n??0))}}):typeof i=="boolean"?e=S({toggle:()=>{o(!this.get())}}):Array.isArray(i)?e=S({push:(...n)=>{o([this.get()].flat().concat(n))},unshift:(...n)=>{o(n.concat(this.get()))},update:n=>{const c=[...this.get()];n(c),o(c)},filter:n=>{o(this.get().filter(n??V))}}):i instanceof Set&&(e=S({add:n=>{const c=new Set(this.get());c.add(n),o(c)},delete:n=>{const c=new Set(this.get());c.delete(n),o(c)},clear:()=>{o(new Set)},update:n=>{const c=new Set(this.get());n(c),o(c)}}));const l={...typeof r=="object"&&r!=null&&"do"in r?r.do(()=>this.get(),(n,c)=>o(n,c)):{},...e};return C=()=>l,l};const w=new Set,I=e=>e(this.get());this.subscribe=e=>(w.add(e),()=>(w.delete(e),void 0));const o=(e,a)=>{const l=typeof e=="function"?e(this.get()):e;l===this.get()||l===void 0||typeof l=="number"&&isNaN(l)||(E(l),w.forEach(I,this),a!==!0&&p(l))};if(this.set=(e,a)=>o(e,a),this.reset=()=>{o(i,!0),w.forEach(I,this)},this.setDeferred=(e,a=500,l,n=!0)=>{n&&m===void 0&&o(e,l),clearTimeout(m),m=setTimeout(()=>{o(e,l),m=void 0},a)},r==null)return;let d=null,b=!0,_=!0,A=i instanceof Set?e=>new Set(JSON.parse(e)):e=>JSON.parse(e),D=i instanceof Set?e=>{if(e instanceof Set)return JSON.stringify(Array.from(e));throw console.error(e),"The value is not Set instance"}:e=>JSON.stringify(e);if(typeof r=="string"?d=r:"storeKey"in r&&(b=r.warnOnDuplicateStoreKey??b,_=r.listenStorageChanges??_,d=r.storeKey,A=r.parseValue??A,D=r.stringifyValue??D),d===null)return;const u=`atom/${d}`;let J=!0;this.get=()=>{if(this.get=()=>y(),J){J=!1;try{E(u in h?A(h[u]):i)}catch{console.warn("Invalid json value",h[u])}}return y()},p=e=>{if(e===i){this.reset();return}h[u]=D(e)},this.reset=()=>{delete h[u],o(i,!0)},b&&g[u]!==void 0&&console.warn("Duplicate Atom key",d),_&&(g[u]=e=>{if(e.newValue===null){this.reset();return}try{o(A(e.newValue))}catch{console.warn("Invalid json value",e.newValue)}})}}const h=window.localStorage,g={},V=t=>t,S=(t,i)=>t;window.addEventListener("storage",t=>{var i;t.key===null||t.newValue===t.oldValue||(i=g[t.key])==null||i.call(g,t)});let j=()=>{throw"call configureAtomaric() before all!"};const v=t=>j=t.useSyncExternalStore,T=t=>j(t.subscribe,t.get),N=t=>t.set,G=t=>t.setDeferred,P=t=>t.get,F=t=>t.do,L=t=>[T(t),N(t)],M=(t,i)=>new f(t,i);s.Atom=f,s.atom=M,s.configureAtomaric=v,s.useAtom=L,s.useAtomDo=F,s.useAtomGet=P,s.useAtomSet=N,s.useAtomSetDeferred=G,s.useAtomValue=T,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
package/types/model.d.ts
CHANGED
|
@@ -19,6 +19,7 @@ export type AtomOptions<Value, Actions extends Record<string, Function> = {}> =
|
|
|
19
19
|
storeKey: AtomStoreKey;
|
|
20
20
|
}
|
|
21
21
|
| {
|
|
22
|
+
/** declare your custom actions */
|
|
22
23
|
do: (get: () => Value, set: (value: Value, isPreventSave?: boolean) => void) => Actions;
|
|
23
24
|
}
|
|
24
25
|
);
|
|
@@ -57,8 +58,8 @@ export type ArrayActions<Value> = UpdateAction<Value[]> & {
|
|
|
57
58
|
filter: (filter?: (value: Value, index: number, array: Value[]) => any) => void;
|
|
58
59
|
};
|
|
59
60
|
|
|
60
|
-
export type DefaultActions<Value> = Value extends Set<infer
|
|
61
|
-
? SetActions<
|
|
61
|
+
export type DefaultActions<Value> = Value extends Set<infer Val>
|
|
62
|
+
? SetActions<Val>
|
|
62
63
|
: Value extends boolean
|
|
63
64
|
? BooleanActions<Value>
|
|
64
65
|
: Value extends (infer Val)[]
|
|
@@ -68,27 +69,36 @@ export type DefaultActions<Value> = Value extends Set<infer V>
|
|
|
68
69
|
: {};
|
|
69
70
|
|
|
70
71
|
export class Atom<Value, Actions extends Record<string, Function> = {}> {
|
|
71
|
-
constructor(defaultValue: Value, storeKeyOrOptions:
|
|
72
|
+
constructor(defaultValue: Value, storeKeyOrOptions: StoreKeyOrOptions<Value, Actions> | undefined);
|
|
72
73
|
|
|
73
74
|
readonly defaultValue: Value;
|
|
74
75
|
readonly get: () => Value;
|
|
75
76
|
readonly set: AtomSetMethod<Value>;
|
|
76
77
|
readonly setDeferred: AtomSetDeferredMethod<Value>;
|
|
78
|
+
/** set default (initial) value as current */
|
|
77
79
|
readonly reset: () => void;
|
|
80
|
+
/** subscribe on value changes */
|
|
78
81
|
readonly subscribe: AtomSubscribeMethod<Value>;
|
|
79
|
-
|
|
82
|
+
/** your custom actions */
|
|
83
|
+
readonly do: Actions & DefaultActions<Value>;
|
|
80
84
|
}
|
|
81
85
|
|
|
86
|
+
export function useAtom<Value>(atom: Atom<Value>): [Value, AtomSetMethod<Value>];
|
|
87
|
+
|
|
88
|
+
/** observable atom value */
|
|
82
89
|
export function useAtomValue<Value>(atom: Atom<Value>): Value;
|
|
83
|
-
export function useAtomSet<Value>(atom: Atom<Value>): (typeof atom)['set'];
|
|
84
|
-
export function useAtomSetDeferred<Value>(atom: Atom<Value>): (typeof atom)['setDeferred'];
|
|
85
|
-
export function useAtomGet<Value>(atom: Atom<Value>): (typeof atom)['get'];
|
|
86
|
-
export function useAtomDo<Value>(atom: Atom<Value>): (typeof atom)['do'];
|
|
87
90
|
|
|
88
|
-
export function
|
|
91
|
+
export function useAtomSet<Value>(atom: Atom<Value>): AtomSetMethod<Value>;
|
|
92
|
+
export function useAtomSetDeferred<Value>(atom: Atom<Value>): AtomSetDeferredMethod<Value>;
|
|
93
|
+
export function useAtomGet<Value>(atom: Atom<Value>): () => Value;
|
|
94
|
+
|
|
95
|
+
/** get your custom actions */
|
|
96
|
+
export function useAtomDo<Value, Actions extends Record<string, Function> = {}>(
|
|
97
|
+
atom: Atom<Value, Actions>,
|
|
98
|
+
): Actions & DefaultActions<Value>;
|
|
89
99
|
|
|
90
100
|
export type StoreKeyOrOptions<Value, Actions extends Record<string, Function> = {}> =
|
|
91
|
-
|
|
|
101
|
+
| AtomStoreKey
|
|
92
102
|
| AtomOptions<Value, Actions>;
|
|
93
103
|
|
|
94
104
|
export function atom<Value, Actions extends Record<string, Function> = {}>(
|
|
@@ -96,4 +106,4 @@ export function atom<Value, Actions extends Record<string, Function> = {}>(
|
|
|
96
106
|
storeKeyOrOptions?: StoreKeyOrOptions<Value, Actions>,
|
|
97
107
|
): Atom<Value, Actions>;
|
|
98
108
|
|
|
99
|
-
export function configureAtomaric(
|
|
109
|
+
export function configureAtomaric(options: { useSyncExternalStore: typeof useSyncExternalStore }): void;
|