@yakocloud/state-vocab 3.0.7 → 3.0.9

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