@yakocloud/state-vocab 3.0.6 → 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 +58 -54
- package/dist/types/index.d.ts +1 -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 S=require("react")
|
|
2
|
-
`),t=[],i=[];for(const o of n){const c=o.match(/^(\s*)"([^"]+)"(\s*:\s*)(.+)$/);if(c){const[,
|
|
3
|
-
`),...i,e)}const
|
|
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,5 +1,4 @@
|
|
|
1
|
-
import { useMemo as
|
|
2
|
-
const D = Symbol("state-def"), $ = Symbol("state-path"), P = Symbol("state-verbose"), C = Symbol("state-verbose-path"), R = Symbol("state-ssr");
|
|
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";
|
|
3
2
|
function z(e, n, t) {
|
|
4
3
|
if (!n)
|
|
5
4
|
return e;
|
|
@@ -16,8 +15,8 @@ function Q(e, n, t) {
|
|
|
16
15
|
const i = n.replace(/\[(\d+)\]/g, ".$1").split(".");
|
|
17
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
21
|
return o[i[i.length - 1]] = t, e;
|
|
23
22
|
}
|
|
@@ -30,20 +29,20 @@ function U(e, n = 0) {
|
|
|
30
29
|
};
|
|
31
30
|
}
|
|
32
31
|
function X(e, n, t = []) {
|
|
33
|
-
return
|
|
32
|
+
return q(
|
|
34
33
|
() => U(e, n),
|
|
35
34
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
36
35
|
t
|
|
37
36
|
);
|
|
38
37
|
}
|
|
39
|
-
function
|
|
38
|
+
function D(e) {
|
|
40
39
|
const n = JSON.stringify(e, null, 2).split(`
|
|
41
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
|
-
t.push(`${
|
|
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"
|
|
@@ -54,8 +53,8 @@ function M(e) {
|
|
|
54
53
|
console.log(t.join(`
|
|
55
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() {
|
|
@@ -74,23 +73,27 @@ class j {
|
|
|
74
73
|
return z(this.#e, n);
|
|
75
74
|
}
|
|
76
75
|
set(n, t) {
|
|
77
|
-
const i = z(this.#e, n), o =
|
|
78
|
-
Q(c, n, o), this.#e = c, this.#t.forEach((
|
|
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
|
-
let
|
|
82
|
-
typeof window > "u" && (
|
|
80
|
+
let _;
|
|
81
|
+
typeof window > "u" && (_ = new AsyncLocalStorage());
|
|
83
82
|
const Z = () => {
|
|
84
|
-
const e =
|
|
83
|
+
const e = _.getStore();
|
|
85
84
|
if (!e)
|
|
86
|
-
throw new Error(`${
|
|
85
|
+
throw new Error(`${L.name} must be initialized for this request`);
|
|
87
86
|
return e;
|
|
88
87
|
};
|
|
89
88
|
let A = null;
|
|
90
|
-
const ee = () => (A || (A = new
|
|
91
|
-
function re(e
|
|
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 = {}) {
|
|
92
95
|
return {
|
|
93
|
-
[
|
|
96
|
+
[B]: !0,
|
|
94
97
|
// marks this object as a leaf in the router tree
|
|
95
98
|
[$]: "",
|
|
96
99
|
// placeholder; injected at runtime by injectPaths()
|
|
@@ -103,31 +106,31 @@ function re(e = {}) {
|
|
|
103
106
|
useState(n) {
|
|
104
107
|
const t = F ? void 0 : x(e.storage), i = x(e.defaultValue), o = e.bidirectional;
|
|
105
108
|
n ??= {};
|
|
106
|
-
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) => {
|
|
107
110
|
const m = h.getItem(r);
|
|
108
|
-
m === null ? E(k) && h.setItem(r,
|
|
111
|
+
m === null ? E(k) && h.setItem(r, a(k)) : l.set(r, g(m));
|
|
109
112
|
}, f = X(
|
|
110
113
|
n.onSet ?? (() => {
|
|
111
114
|
}),
|
|
112
115
|
n.delayedSet,
|
|
113
116
|
[]
|
|
114
|
-
), S =
|
|
117
|
+
), S = M(void 0), T = M(!1);
|
|
115
118
|
if (!T.current) {
|
|
116
119
|
T.current = !0;
|
|
117
|
-
let r =
|
|
118
|
-
E(r) || (r = c, E(r) &&
|
|
120
|
+
let r = l.get(s);
|
|
121
|
+
E(r) || (r = c, E(r) && l.set(s, r)), !V && t && d(s, t, r);
|
|
119
122
|
}
|
|
120
|
-
const w =
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
123
|
+
const w = H(
|
|
124
|
+
l.subscribe.bind(l),
|
|
125
|
+
l.getClientSnapshot.bind(l),
|
|
126
|
+
l.getServerSnapshot.bind(l)
|
|
124
127
|
);
|
|
125
128
|
if (v)
|
|
126
129
|
if (y) {
|
|
127
130
|
const r = z(w, y);
|
|
128
|
-
r &&
|
|
131
|
+
r && D(r);
|
|
129
132
|
} else
|
|
130
|
-
|
|
133
|
+
D(w);
|
|
131
134
|
const p = z(w, s, c);
|
|
132
135
|
S.current = p, ne(() => {
|
|
133
136
|
!V || !t || d(s, t, p);
|
|
@@ -136,27 +139,27 @@ function re(e = {}) {
|
|
|
136
139
|
if (r.key !== s)
|
|
137
140
|
return;
|
|
138
141
|
const h = r.newValue, m = (h === null ? null : g(h)) ?? c;
|
|
139
|
-
E(m) && (
|
|
142
|
+
E(m) && (l.set(s, m), f(m, S.current));
|
|
140
143
|
});
|
|
141
|
-
|
|
142
|
-
if (
|
|
144
|
+
O(() => {
|
|
145
|
+
if (u)
|
|
143
146
|
return window.addEventListener("storage", b), () => window.removeEventListener("storage", b);
|
|
144
|
-
}, [
|
|
145
|
-
const I =
|
|
146
|
-
const h =
|
|
147
|
-
|
|
148
|
-
}, [s, t, f]),
|
|
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(() => {
|
|
149
152
|
const r = c;
|
|
150
153
|
if (!E(r)) {
|
|
151
154
|
t?.removeItem(s);
|
|
152
155
|
return;
|
|
153
156
|
}
|
|
154
|
-
|
|
157
|
+
l.set(s, r), f(r, S.current), t && t.setItem(s, a(r));
|
|
155
158
|
}, [s, c, t, f]);
|
|
156
159
|
return [
|
|
157
160
|
p,
|
|
158
161
|
I,
|
|
159
|
-
|
|
162
|
+
W
|
|
160
163
|
];
|
|
161
164
|
},
|
|
162
165
|
/** Returns the fully qualified job name (dot-separated path). */
|
|
@@ -171,20 +174,20 @@ function J(e, n) {
|
|
|
171
174
|
verbose: i,
|
|
172
175
|
verbosePath: o,
|
|
173
176
|
ssr: c,
|
|
174
|
-
cache:
|
|
177
|
+
cache: u
|
|
175
178
|
} = n;
|
|
176
|
-
let s =
|
|
177
|
-
s || (s = /* @__PURE__ */ new Map(),
|
|
179
|
+
let s = u.proxy.get(e);
|
|
180
|
+
s || (s = /* @__PURE__ */ new Map(), u.proxy.set(e, s));
|
|
178
181
|
const v = s.get(t);
|
|
179
182
|
if (v)
|
|
180
183
|
return v;
|
|
181
184
|
const y = new Proxy(e, {
|
|
182
|
-
get(V,
|
|
183
|
-
const
|
|
184
|
-
if (
|
|
185
|
-
const d =
|
|
186
|
-
let f =
|
|
187
|
-
f || (f = /* @__PURE__ */ new Map(),
|
|
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));
|
|
188
191
|
const S = f.get(g);
|
|
189
192
|
if (S)
|
|
190
193
|
return S;
|
|
@@ -207,15 +210,15 @@ function J(e, n) {
|
|
|
207
210
|
), p = { ...d, ...w };
|
|
208
211
|
return f.set(g, p), p;
|
|
209
212
|
}
|
|
210
|
-
return
|
|
213
|
+
return a && typeof a == "object" ? J(a, {
|
|
211
214
|
...n,
|
|
212
215
|
path: g
|
|
213
|
-
}) :
|
|
216
|
+
}) : a;
|
|
214
217
|
}
|
|
215
218
|
});
|
|
216
219
|
return s.set(t, y), y;
|
|
217
220
|
}
|
|
218
|
-
function
|
|
221
|
+
function ce(e, n) {
|
|
219
222
|
return J(e, {
|
|
220
223
|
...n,
|
|
221
224
|
verbosePath: n?.verbosePath ?? "",
|
|
@@ -226,6 +229,7 @@ function oe(e, n) {
|
|
|
226
229
|
});
|
|
227
230
|
}
|
|
228
231
|
export {
|
|
229
|
-
|
|
230
|
-
|
|
232
|
+
oe as defineState,
|
|
233
|
+
re as runWithStateVocab,
|
|
234
|
+
ce as setupStorage
|
|
231
235
|
};
|
package/dist/types/index.d.ts
CHANGED