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