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