@yakocloud/state-vocab 3.0.4 → 3.0.5
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/dist/state-vocab.cjs.js +3 -3
- package/dist/state-vocab.es.js +143 -141
- package/dist/types/state.types.d.ts +1 -0
- package/dist/types/store.d.ts +16 -0
- package/package.json +1 -1
package/dist/state-vocab.cjs.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("react"),
|
|
2
|
-
`),
|
|
3
|
-
`),...i,t)}const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("react"),L=Symbol("state-def"),I=Symbol("state-path"),x=Symbol("state-verbose"),A=Symbol("state-verbose-path"),M=Symbol("state-ssr");function $(t,n,e){if(!n)return t;const i=n.split(".");let o=t;for(const c of i)if(o!==null&&typeof o=="object"&&c in o)o=o[c];else return e;return o===void 0?e:o}function F(t,n,e){const i=n.replace(/\[(\d+)\]/g,".$1").split(".");let o=t;for(let c=0;c<i.length-1;c++){const l=i[c],s=i[c+1];(o[l]===void 0||o[l]===null)&&(o[l]=/^\d+$/.test(s)?[]:{}),o=o[l]}return o[i[i.length-1]]=e,t}function J(t,n=0){let e;return function(...i){e!==void 0&&clearTimeout(e),e=setTimeout(()=>{e=void 0,t.apply(this,i)},n)}}function H(t,n,e=[]){return d.useMemo(()=>J(t,n),e)}function _(t){const n=JSON.stringify(t,null,2).split(`
|
|
2
|
+
`),e=[],i=[];for(const o of n){const c=o.match(/^(\s*)"([^"]+)"(\s*:\s*)(.+)$/);if(c){const[,l,s,p,b]=c;e.push(`${l}%c"${s}"%c${p}%c${b}`),i.push("color: #9cdcfe; font-weight: bold","color: #cccccc","color: #ce9178")}else e.push(`%c${o}`),i.push("color: #cccccc")}console.log(e.join(`
|
|
3
|
+
`),...i,t)}const N=t=>typeof t=="function",K=t=>typeof t=="function",T=t=>typeof t<"u",C=t=>K(t)?t():t;class W{#e;#t;constructor(){this.#e={},this.#t=new Set}subscribe(n){return this.#t.add(n),()=>this.#t.delete(n)}getClientSnapshot(){return this.#e}getServerSnapshot(){return this.#e}get(n){return $(this.#e,n)}set(n,e){const i=$(this.#e,n),o=N(e)?e(i):e,c={...this.#e};F(c,n,o),this.#e=c,this.#t.forEach(l=>l())}}const O=()=>new W,q=d.cache(O);let R=null;const G=()=>(R||(R=O()),R),Q=()=>typeof window>"u"?q():G(),j=typeof window>"u",U=j?d.useEffect:d.useLayoutEffect;function X(t={}){return{[L]:!0,[I]:"",[x]:!1,[A]:"",[M]:!1,useState(n){const e=j?void 0:C(t.storage),i=C(t.defaultValue),o=t.bidirectional;n??={};const c=C(n.defaultValue)??i,l=n.bidirectional??o,s=this[I],p=this[x],b=this[A],V=this[M],a=Q(),u=t.serialize??JSON.stringify,g=t.deserialize??JSON.parse,h=(r,S,P)=>{const E=S.getItem(r);E===null?T(P)&&S.setItem(r,u(P)):a.set(r,g(E))},f=H(n.onSet??(()=>{}),n.delayedSet,[]),y=d.useRef(void 0),k=d.useRef(!1);if(!k.current){k.current=!0;let r=a.get(s);T(r)||(r=c,T(r)&&a.set(s,r)),!V&&e&&h(s,e,r)}const w=d.useSyncExternalStore(a.subscribe.bind(a),a.getClientSnapshot.bind(a),a.getServerSnapshot.bind(a));if(p)if(b){const r=$(w,b);r&&_(r)}else _(w);const m=$(w,s,c);y.current=m,U(()=>{!V||!e||h(s,e,m)},[]);const v=d.useEffectEvent(r=>{if(r.key!==s)return;const S=r.newValue,E=(S===null?null:g(S))??c;T(E)&&(a.set(s,E),f(E,y.current))});d.useEffect(()=>{if(l)return window.addEventListener("storage",v),()=>window.removeEventListener("storage",v)},[l]);const z=d.useCallback(r=>{const S=N(r)?r(y.current):r;a.set(s,S),f(S,y.current),e&&e.setItem(s,u(S))},[s,e,f]),B=d.useCallback(()=>{const r=c;if(!T(r)){e?.removeItem(s);return}a.set(s,r),f(r,y.current),e&&e.setItem(s,u(r))},[s,c,e,f]);return[m,z,B]},toString(){return this[I]}}}function D(t,n){const{path:e="",verbose:i,verbosePath:o,ssr:c,cache:l}=n;let s=l.proxy.get(t);s||(s=new Map,l.proxy.set(t,s));const p=s.get(e);if(p)return p;const b=new Proxy(t,{get(V,a){const u=V[a],g=e?`${e}.${String(a)}`:String(a);if(u&&typeof u=="object"&&L in u){const h=u;let f=l.leaf.get(h);f||(f=new Map,l.leaf.set(h,f));const y=f.get(g);if(y)return y;const k=Reflect.ownKeys(h).filter(v=>typeof h[v]=="function"),w=Object.fromEntries(k.map(v=>[v,(...z)=>h[v].call({...h,[I]:g,[x]:i,[A]:o,[M]:c},...z)])),m={...h,...w};return f.set(g,m),m}return u&&typeof u=="object"?D(u,{...n,path:g}):u}});return s.set(e,b),b}function Y(t,n){return D(t,{...n,verbosePath:n?.verbosePath??"",cache:{proxy:new WeakMap,leaf:new WeakMap}})}exports.defineState=X;exports.setupStorage=Y;
|
package/dist/state-vocab.es.js
CHANGED
|
@@ -1,68 +1,68 @@
|
|
|
1
|
-
import { useMemo as
|
|
2
|
-
const
|
|
3
|
-
function
|
|
4
|
-
if (!
|
|
1
|
+
import { useMemo as H, cache as K, useRef as _, useSyncExternalStore as W, useEffect as N, useLayoutEffect as q, useEffectEvent as G, useCallback as L } from "react";
|
|
2
|
+
const D = Symbol("state-def"), I = Symbol("state-path"), A = Symbol("state-verbose"), C = Symbol("state-verbose-path"), R = Symbol("state-ssr");
|
|
3
|
+
function $(t, n, e) {
|
|
4
|
+
if (!n)
|
|
5
5
|
return t;
|
|
6
|
-
const i =
|
|
6
|
+
const i = n.split(".");
|
|
7
7
|
let o = t;
|
|
8
|
-
for (const
|
|
9
|
-
if (o !== null && typeof o == "object" &&
|
|
10
|
-
o = o[
|
|
8
|
+
for (const c of i)
|
|
9
|
+
if (o !== null && typeof o == "object" && c in o)
|
|
10
|
+
o = o[c];
|
|
11
11
|
else
|
|
12
|
-
return
|
|
13
|
-
return o === void 0 ?
|
|
12
|
+
return e;
|
|
13
|
+
return o === void 0 ? e : o;
|
|
14
14
|
}
|
|
15
|
-
function
|
|
16
|
-
const i =
|
|
15
|
+
function Q(t, n, e) {
|
|
16
|
+
const i = n.replace(/\[(\d+)\]/g, ".$1").split(".");
|
|
17
17
|
let o = t;
|
|
18
|
-
for (let
|
|
19
|
-
const
|
|
20
|
-
(o[
|
|
18
|
+
for (let c = 0; c < i.length - 1; c++) {
|
|
19
|
+
const l = i[c], s = i[c + 1];
|
|
20
|
+
(o[l] === void 0 || o[l] === null) && (o[l] = /^\d+$/.test(s) ? [] : {}), o = o[l];
|
|
21
21
|
}
|
|
22
|
-
return o[i[i.length - 1]] =
|
|
22
|
+
return o[i[i.length - 1]] = e, t;
|
|
23
23
|
}
|
|
24
|
-
function
|
|
25
|
-
let
|
|
24
|
+
function U(t, n = 0) {
|
|
25
|
+
let e;
|
|
26
26
|
return function(...i) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
},
|
|
27
|
+
e !== void 0 && clearTimeout(e), e = setTimeout(() => {
|
|
28
|
+
e = void 0, t.apply(this, i);
|
|
29
|
+
}, n);
|
|
30
30
|
};
|
|
31
31
|
}
|
|
32
|
-
function
|
|
33
|
-
return
|
|
34
|
-
() =>
|
|
32
|
+
function X(t, n, e = []) {
|
|
33
|
+
return H(
|
|
34
|
+
() => U(t, n),
|
|
35
35
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
36
|
-
|
|
36
|
+
e
|
|
37
37
|
);
|
|
38
38
|
}
|
|
39
|
-
function
|
|
40
|
-
const
|
|
41
|
-
`),
|
|
42
|
-
for (const o of
|
|
43
|
-
const
|
|
44
|
-
if (
|
|
45
|
-
const [,
|
|
46
|
-
|
|
39
|
+
function M(t) {
|
|
40
|
+
const n = JSON.stringify(t, null, 2).split(`
|
|
41
|
+
`), e = [], i = [];
|
|
42
|
+
for (const o of n) {
|
|
43
|
+
const c = o.match(/^(\s*)"([^"]+)"(\s*:\s*)(.+)$/);
|
|
44
|
+
if (c) {
|
|
45
|
+
const [, l, s, v, y] = c;
|
|
46
|
+
e.push(`${l}%c"${s}"%c${v}%c${y}`), i.push(
|
|
47
47
|
"color: #9cdcfe; font-weight: bold",
|
|
48
48
|
"color: #cccccc",
|
|
49
49
|
"color: #ce9178"
|
|
50
50
|
);
|
|
51
51
|
} else
|
|
52
|
-
|
|
52
|
+
e.push(`%c${o}`), i.push("color: #cccccc");
|
|
53
53
|
}
|
|
54
|
-
console.log(
|
|
54
|
+
console.log(e.join(`
|
|
55
55
|
`), ...i, t);
|
|
56
56
|
}
|
|
57
|
-
const
|
|
58
|
-
class
|
|
57
|
+
const O = (t) => typeof t == "function", Y = (t) => typeof t == "function", w = (t) => typeof t < "u", x = (t) => Y(t) ? t() : t;
|
|
58
|
+
class Z {
|
|
59
59
|
#e;
|
|
60
60
|
#t;
|
|
61
61
|
constructor() {
|
|
62
62
|
this.#e = {}, this.#t = /* @__PURE__ */ new Set();
|
|
63
63
|
}
|
|
64
|
-
subscribe(
|
|
65
|
-
return this.#t.add(
|
|
64
|
+
subscribe(n) {
|
|
65
|
+
return this.#t.add(n), () => this.#t.delete(n);
|
|
66
66
|
}
|
|
67
67
|
getClientSnapshot() {
|
|
68
68
|
return this.#e;
|
|
@@ -70,146 +70,148 @@ class U {
|
|
|
70
70
|
getServerSnapshot() {
|
|
71
71
|
return this.#e;
|
|
72
72
|
}
|
|
73
|
-
get(
|
|
74
|
-
return
|
|
73
|
+
get(n) {
|
|
74
|
+
return $(this.#e, n);
|
|
75
75
|
}
|
|
76
|
-
set(
|
|
77
|
-
const i =
|
|
78
|
-
|
|
76
|
+
set(n, e) {
|
|
77
|
+
const i = $(this.#e, n), o = O(e) ? e(i) : e, c = { ...this.#e };
|
|
78
|
+
Q(c, n, o), this.#e = c, this.#t.forEach((l) => l());
|
|
79
79
|
}
|
|
80
80
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
};
|
|
81
|
+
const j = () => new Z(), ee = K(j);
|
|
82
|
+
let P = null;
|
|
83
|
+
const te = () => (P || (P = j()), P), ne = () => typeof window > "u" ? ee() : te(), B = typeof window > "u", se = B ? N : q;
|
|
84
|
+
function oe(t = {}) {
|
|
86
85
|
return {
|
|
87
|
-
[
|
|
86
|
+
[D]: !0,
|
|
88
87
|
// marks this object as a leaf in the router tree
|
|
89
|
-
[
|
|
88
|
+
[I]: "",
|
|
90
89
|
// placeholder; injected at runtime by injectPaths()
|
|
91
|
-
[
|
|
90
|
+
[A]: !1,
|
|
92
91
|
// placeholder
|
|
93
|
-
[
|
|
92
|
+
[C]: "",
|
|
94
93
|
// placeholder
|
|
95
|
-
[
|
|
94
|
+
[R]: !1,
|
|
96
95
|
// placeholder
|
|
97
|
-
useState(
|
|
98
|
-
const
|
|
99
|
-
|
|
100
|
-
const
|
|
101
|
-
|
|
96
|
+
useState(n) {
|
|
97
|
+
const e = B ? void 0 : x(t.storage), i = x(t.defaultValue), o = t.bidirectional;
|
|
98
|
+
n ??= {};
|
|
99
|
+
const c = x(n.defaultValue) ?? i, l = n.bidirectional ?? o, s = this[I], v = this[A], y = this[C], V = this[R], a = ne(), u = t.serialize ?? JSON.stringify, b = t.deserialize ?? JSON.parse, d = (r, h, z) => {
|
|
100
|
+
const m = h.getItem(r);
|
|
101
|
+
m === null ? w(z) && h.setItem(r, u(z)) : a.set(r, b(m));
|
|
102
|
+
}, f = X(
|
|
103
|
+
n.onSet ?? (() => {
|
|
102
104
|
}),
|
|
103
|
-
|
|
105
|
+
n.delayedSet,
|
|
104
106
|
[]
|
|
105
|
-
),
|
|
107
|
+
), S = _(void 0), T = _(!1);
|
|
106
108
|
if (!T.current) {
|
|
107
109
|
T.current = !0;
|
|
108
|
-
let
|
|
109
|
-
|
|
110
|
+
let r = a.get(s);
|
|
111
|
+
w(r) || (r = c, w(r) && a.set(s, r)), !V && e && d(s, e, r);
|
|
110
112
|
}
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
113
|
+
const E = W(
|
|
114
|
+
a.subscribe.bind(a),
|
|
115
|
+
a.getClientSnapshot.bind(a),
|
|
116
|
+
a.getServerSnapshot.bind(a)
|
|
115
117
|
);
|
|
116
|
-
if (
|
|
117
|
-
if (
|
|
118
|
-
const
|
|
119
|
-
|
|
118
|
+
if (v)
|
|
119
|
+
if (y) {
|
|
120
|
+
const r = $(E, y);
|
|
121
|
+
r && M(r);
|
|
120
122
|
} else
|
|
121
|
-
|
|
122
|
-
const
|
|
123
|
-
|
|
124
|
-
!
|
|
123
|
+
M(E);
|
|
124
|
+
const p = $(E, s, c);
|
|
125
|
+
S.current = p, se(() => {
|
|
126
|
+
!V || !e || d(s, e, p);
|
|
125
127
|
}, []);
|
|
126
|
-
const
|
|
127
|
-
if (
|
|
128
|
+
const g = G((r) => {
|
|
129
|
+
if (r.key !== s)
|
|
128
130
|
return;
|
|
129
|
-
const
|
|
130
|
-
|
|
131
|
+
const h = r.newValue, m = (h === null ? null : b(h)) ?? c;
|
|
132
|
+
w(m) && (a.set(s, m), f(m, S.current));
|
|
131
133
|
});
|
|
132
|
-
|
|
133
|
-
if (
|
|
134
|
-
return window.addEventListener("storage",
|
|
135
|
-
}, [
|
|
136
|
-
const
|
|
137
|
-
const
|
|
138
|
-
|
|
139
|
-
}, [
|
|
140
|
-
const
|
|
141
|
-
if (!
|
|
142
|
-
|
|
134
|
+
N(() => {
|
|
135
|
+
if (l)
|
|
136
|
+
return window.addEventListener("storage", g), () => window.removeEventListener("storage", g);
|
|
137
|
+
}, [l]);
|
|
138
|
+
const k = L((r) => {
|
|
139
|
+
const h = O(r) ? r(S.current) : r;
|
|
140
|
+
a.set(s, h), f(h, S.current), e && e.setItem(s, u(h));
|
|
141
|
+
}, [s, e, f]), J = L(() => {
|
|
142
|
+
const r = c;
|
|
143
|
+
if (!w(r)) {
|
|
144
|
+
e?.removeItem(s);
|
|
143
145
|
return;
|
|
144
146
|
}
|
|
145
|
-
|
|
146
|
-
}, [
|
|
147
|
+
a.set(s, r), f(r, S.current), e && e.setItem(s, u(r));
|
|
148
|
+
}, [s, c, e, f]);
|
|
147
149
|
return [
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
150
|
+
p,
|
|
151
|
+
k,
|
|
152
|
+
J
|
|
151
153
|
];
|
|
152
154
|
},
|
|
153
155
|
/** Returns the fully qualified job name (dot-separated path). */
|
|
154
156
|
toString() {
|
|
155
|
-
return this[
|
|
157
|
+
return this[I];
|
|
156
158
|
}
|
|
157
159
|
};
|
|
158
160
|
}
|
|
159
|
-
function
|
|
161
|
+
function F(t, n) {
|
|
160
162
|
const {
|
|
161
|
-
path:
|
|
163
|
+
path: e = "",
|
|
162
164
|
verbose: i,
|
|
163
165
|
verbosePath: o,
|
|
164
|
-
ssr:
|
|
165
|
-
cache:
|
|
166
|
-
} =
|
|
167
|
-
let
|
|
168
|
-
|
|
169
|
-
const
|
|
170
|
-
if (
|
|
171
|
-
return
|
|
172
|
-
const
|
|
173
|
-
get(
|
|
174
|
-
const
|
|
175
|
-
if (
|
|
176
|
-
const
|
|
177
|
-
let
|
|
178
|
-
|
|
179
|
-
const
|
|
180
|
-
if (
|
|
181
|
-
return
|
|
182
|
-
const T = Reflect.ownKeys(
|
|
183
|
-
(
|
|
184
|
-
),
|
|
185
|
-
T.map((
|
|
186
|
-
|
|
187
|
-
(
|
|
166
|
+
ssr: c,
|
|
167
|
+
cache: l
|
|
168
|
+
} = n;
|
|
169
|
+
let s = l.proxy.get(t);
|
|
170
|
+
s || (s = /* @__PURE__ */ new Map(), l.proxy.set(t, s));
|
|
171
|
+
const v = s.get(e);
|
|
172
|
+
if (v)
|
|
173
|
+
return v;
|
|
174
|
+
const y = new Proxy(t, {
|
|
175
|
+
get(V, a) {
|
|
176
|
+
const u = V[a], b = e ? `${e}.${String(a)}` : String(a);
|
|
177
|
+
if (u && typeof u == "object" && D in u) {
|
|
178
|
+
const d = u;
|
|
179
|
+
let f = l.leaf.get(d);
|
|
180
|
+
f || (f = /* @__PURE__ */ new Map(), l.leaf.set(d, f));
|
|
181
|
+
const S = f.get(b);
|
|
182
|
+
if (S)
|
|
183
|
+
return S;
|
|
184
|
+
const T = Reflect.ownKeys(d).filter(
|
|
185
|
+
(g) => typeof d[g] == "function"
|
|
186
|
+
), E = Object.fromEntries(
|
|
187
|
+
T.map((g) => [
|
|
188
|
+
g,
|
|
189
|
+
(...k) => d[g].call(
|
|
188
190
|
{
|
|
189
|
-
...
|
|
190
|
-
[
|
|
191
|
-
[
|
|
192
|
-
[
|
|
193
|
-
[
|
|
191
|
+
...d,
|
|
192
|
+
[I]: b,
|
|
193
|
+
[A]: i,
|
|
194
|
+
[C]: o,
|
|
195
|
+
[R]: c
|
|
194
196
|
},
|
|
195
|
-
|
|
197
|
+
...k
|
|
196
198
|
)
|
|
197
199
|
])
|
|
198
|
-
),
|
|
199
|
-
return
|
|
200
|
+
), p = { ...d, ...E };
|
|
201
|
+
return f.set(b, p), p;
|
|
200
202
|
}
|
|
201
|
-
return
|
|
202
|
-
...
|
|
203
|
-
path:
|
|
204
|
-
}) :
|
|
203
|
+
return u && typeof u == "object" ? F(u, {
|
|
204
|
+
...n,
|
|
205
|
+
path: b
|
|
206
|
+
}) : u;
|
|
205
207
|
}
|
|
206
208
|
});
|
|
207
|
-
return
|
|
209
|
+
return s.set(e, y), y;
|
|
208
210
|
}
|
|
209
|
-
function
|
|
210
|
-
return
|
|
211
|
-
...
|
|
212
|
-
verbosePath:
|
|
211
|
+
function ce(t, n) {
|
|
212
|
+
return F(t, {
|
|
213
|
+
...n,
|
|
214
|
+
verbosePath: n?.verbosePath ?? "",
|
|
213
215
|
cache: {
|
|
214
216
|
proxy: /* @__PURE__ */ new WeakMap(),
|
|
215
217
|
leaf: /* @__PURE__ */ new WeakMap()
|
|
@@ -217,6 +219,6 @@ function ee(t, e) {
|
|
|
217
219
|
});
|
|
218
220
|
}
|
|
219
221
|
export {
|
|
220
|
-
|
|
221
|
-
|
|
222
|
+
oe as defineState,
|
|
223
|
+
ce as setupStorage
|
|
222
224
|
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ValueOrTransformer, Vocab } from "./state.types";
|
|
2
|
+
type Listener = () => void;
|
|
3
|
+
/***
|
|
4
|
+
* https://react.dev/reference/react/useSyncExternalStore
|
|
5
|
+
*/
|
|
6
|
+
declare class VocabStore {
|
|
7
|
+
#private;
|
|
8
|
+
constructor();
|
|
9
|
+
subscribe(listener: Listener): () => boolean;
|
|
10
|
+
getClientSnapshot<V>(): Vocab<V>;
|
|
11
|
+
getServerSnapshot<V>(): Vocab<V>;
|
|
12
|
+
get<V>(statePath: string): V | undefined;
|
|
13
|
+
set<V>(statePath: string, value: ValueOrTransformer<V>): void;
|
|
14
|
+
}
|
|
15
|
+
export declare const getVocabStore: () => VocabStore;
|
|
16
|
+
export {};
|