@yakocloud/state-vocab 3.0.6 → 3.0.8

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