@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.
@@ -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 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,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 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 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 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 H(
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 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 [, 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 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 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 = 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 = 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 B;
82
- typeof window > "u" && (B = new AsyncLocalStorage());
80
+ let _;
81
+ typeof window > "u" && (_ = new AsyncLocalStorage());
83
82
  const Z = () => {
84
- const e = B.getStore();
83
+ const e = _.getStore();
85
84
  if (!e)
86
- throw new Error(`${j.name} must be initialized for this request`);
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 j()), A), te = () => typeof window > "u" ? Z() : ee(), F = typeof window > "u", ne = F ? N : W;
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
- [D]: !0,
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, 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) => {
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, u(k)) : a.set(r, g(m));
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 = L(void 0), T = L(!1);
117
+ ), S = M(void 0), T = M(!1);
115
118
  if (!T.current) {
116
119
  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);
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 = K(
121
- a.subscribe.bind(a),
122
- a.getClientSnapshot.bind(a),
123
- a.getServerSnapshot.bind(a)
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 && M(r);
131
+ r && D(r);
129
132
  } else
130
- M(w);
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) && (a.set(s, m), f(m, S.current));
142
+ E(m) && (l.set(s, m), f(m, S.current));
140
143
  });
141
- N(() => {
142
- if (l)
144
+ O(() => {
145
+ if (u)
143
146
  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 = _(() => {
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
- a.set(s, r), f(r, S.current), t && t.setItem(s, u(r));
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
- q
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: l
177
+ cache: u
175
178
  } = n;
176
- let s = l.proxy.get(e);
177
- s || (s = /* @__PURE__ */ new Map(), l.proxy.set(e, s));
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, a) {
183
- const u = V[a], g = t ? `${t}.${String(a)}` : String(a);
184
- if (u && typeof u == "object" && D in u) {
185
- const d = u;
186
- let f = l.leaf.get(d);
187
- f || (f = /* @__PURE__ */ new Map(), l.leaf.set(d, f));
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 u && typeof u == "object" ? J(u, {
213
+ return a && typeof a == "object" ? J(a, {
211
214
  ...n,
212
215
  path: g
213
- }) : u;
216
+ }) : a;
214
217
  }
215
218
  });
216
219
  return s.set(t, y), y;
217
220
  }
218
- function oe(e, n) {
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
- re as defineState,
230
- oe as setupStorage
232
+ oe as defineState,
233
+ re as runWithStateVocab,
234
+ ce as setupStorage
231
235
  };
@@ -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.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",