@inox-tools/request-state 0.7.0 → 0.7.1

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,2 +1,2 @@
1
- import {parse}from'devalue';import {ServerStateLoaded}from'../events.js';const u={URL:t=>new URL(t),Date:t=>new Date(t),GlobalSymbol:t=>Symbol.for(t),WellKnownSymbol:t=>Symbol[t]},r=t=>Array.from(t.querySelectorAll("script.it-astro-state")).map(o=>{if(o?.textContent){const a=parse(o.textContent,u);return o.remove(),a}}).filter(o=>!!o);let i=r(document);const d=()=>{const t="__@it-astro:request-state-data";return t in window||(window[t]=new Map),window[t]},n=d(),f=(t,e)=>{for(const[s,o]of e.entries())t.has(s)||t.set(s,o);return t},c=(t=false)=>()=>{const e=t?new Map:new Map(n);i?.reduce(f,e);const s=new ServerStateLoaded(new Map(n),e,{cancelable:true});if(document.dispatchEvent(s)){n.clear();for(const[o,a]of s.serverState.entries())n.set(o,a);}};c()(),document.addEventListener("astro:before-swap",t=>{i=r(t.newDocument);}),document.addEventListener("astro:after-swap",c(true));const m=(t,e)=>(n.has(t)||e!==void 0&&n.set(t,e),n.get(t)),y=t=>n.has(t),v=(t,e)=>{e===void 0?n.delete(t):n.set(t,e);};export{m as getState,y as hasState,v as setState,n as state};//# sourceMappingURL=clientState.js.map
1
+ import {parse}from'devalue';import {ServerStateLoaded}from'../events.js';const d={undefined:()=>{},URL:t=>new URL(t),Date:t=>new Date(t),GlobalSymbol:t=>Symbol.for(t),WellKnownSymbol:t=>Symbol[t]},r=t=>Array.from(t.querySelectorAll("script.it-astro-state")).map(n=>{if(n?.textContent){const a=parse(n.textContent,d);return n.remove(),a}}).filter(n=>!!n);let i=r(document);const f=()=>{const t="__@it-astro:request-state-data";return t in window||(window[t]=new Map),window[t]},o=f(),l=(t,e)=>{for(const[s,n]of e.entries())t.has(s)||t.set(s,n);return t},c=(t=false)=>()=>{const e=t?new Map:new Map(o);i?.reduce(l,e);const s=new ServerStateLoaded(new Map(o),e,{cancelable:true});if(document.dispatchEvent(s)){o.clear();for(const[n,a]of s.serverState.entries())o.set(n,a);}};c()(),document.addEventListener("astro:before-swap",t=>{i=r(t.newDocument);}),document.addEventListener("astro:after-swap",c(true));const m=(t,e)=>(o.has(t)||e!==void 0&&o.set(t,e),o.get(t)),y=t=>o.has(t),v=(t,e)=>{o.set(t,e);};export{m as getState,y as hasState,v as setState,o as state};//# sourceMappingURL=clientState.js.map
2
2
  //# sourceMappingURL=clientState.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/runtime/clientState.ts"],"names":["revivers","value","loadState","doc","element","state","parse","nextStates","initialiseState","STATE_WINDOW_NAMESPACE","mergeState","oldState","newState","newKey","newValue","applyState","isViewTransition","startingState","event","ServerStateLoaded","key","getState","valueIfMissing","hasState","setState"],"mappings":"yEAGA,MAAMA,CAAAA,CAAgD,CACrD,GAAA,CAAMC,CAAAA,EAAU,IAAI,GAAA,CAAIA,CAAK,CAAA,CAC7B,IAAA,CAAOA,CAAAA,EAAU,IAAI,IAAA,CAAKA,CAAK,CAAA,CAC/B,YAAA,CAAeA,CAAAA,EAAU,MAAA,CAAO,GAAA,CAAIA,CAAK,CAAA,CACzC,eAAA,CAAkBA,GAAU,MAAA,CAAOA,CAA4B,CAChE,CAAA,CAEMC,CAAAA,CAAaC,CAAAA,EACD,KAAA,CAAM,IAAA,CAAKA,CAAAA,CAAI,gBAAA,CAAiB,uBAAuB,CAAC,CAAA,CAEvE,GAAA,CAAKC,CAAAA,EAAY,CACjB,GAAIA,CAAAA,EAAS,WAAA,CAAa,CACzB,MAAMC,CAAAA,CAAQC,KAAAA,CAAMF,CAAAA,CAAQ,WAAA,CAAaJ,CAAQ,CAAA,CACjD,OAAAI,CAAAA,CAAQ,MAAA,GACDC,CACR,CACD,CAAC,CAAA,CACA,MAAA,CAAQA,CAAAA,EAA0B,CAAC,CAACA,CAAK,CAAA,CAI5C,IAAIE,CAAAA,CAAaL,CAAAA,CAAU,Q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CAAAA,CAAM,WAAW,EACzC,CAAC,CAAA,CACD,QAAA,CAAS,gBAAA,CAAiB,kBAAA,CAAoBH,CAAAA,CAAW,IAAI,CAAC,CAAA,CAEvD,MAAMM,CAAAA,CAAW,CAACD,CAAAA,CAAaE,CAAAA,IAChCjB,CAAAA,CAAM,GAAA,CAAIe,CAAG,CAAA,EACbE,CAAAA,GAAmB,MAAA,EACtBjB,CAAAA,CAAM,GAAA,CAAIe,CAAAA,CAAKE,CAAc,CAAA,CAGxBjB,CAAAA,CAAM,GAAA,CAAIe,CAAG,CAAA,CAAA,CAGRG,CAAAA,CAAYH,CAAAA,EAAgBf,CAAAA,CAAM,GAAA,CAAIe,CAAG,CAAA,CAEzCI,CAAAA,CAAW,CAACJ,CAAAA,CAAanB,CAAAA,GAAyB,CAC1DA,CAAAA,GAAU,MAAA,CACbI,CAAAA,CAAM,MAAA,CAAOe,CAAG,CAAA,CAEhBf,CAAAA,CAAM,GAAA,CAAIe,CAAAA,CAAKnB,CAAK,EAEtB","file":"clientState.js","sourcesContent":["import { parse } from 'devalue';\nimport { type State, ServerStateLoaded } from '../events.js';\n\nconst revivers: Record<string, (value: any) => any> = {\n\tURL: (value) => new URL(value),\n\tDate: (value) => new Date(value),\n\tGlobalSymbol: (value) => Symbol.for(value),\n\tWellKnownSymbol: (value) => Symbol[value as keyof typeof Symbol],\n};\n\nconst loadState = (doc: Document): State[] => {\n\tconst elements = Array.from(doc.querySelectorAll('script.it-astro-state'));\n\tconst scripts = elements\n\t\t.map((element) => {\n\t\t\tif (element?.textContent) {\n\t\t\t\tconst state = parse(element.textContent, revivers);\n\t\t\t\telement.remove();\n\t\t\t\treturn state;\n\t\t\t}\n\t\t})\n\t\t.filter((state): state is State => !!state);\n\treturn scripts;\n};\n\nlet nextStates = loadState(document);\n\nconst initialiseState = (): State => {\n\tconst STATE_WINDOW_NAMESPACE = '__@it-astro:request-state-data' as const;\n\tif (!(STATE_WINDOW_NAMESPACE in window)) {\n\t\twindow[STATE_WINDOW_NAMESPACE] = new Map();\n\t}\n\treturn window[STATE_WINDOW_NAMESPACE] as State;\n};\n\nexport const state = initialiseState();\n\nconst mergeState = (oldState: State, newState: State) => {\n\tfor (const [newKey, newValue] of newState.entries()) {\n\t\tconst isHit = oldState.has(newKey);\n\t\tif (isHit) {\n\t\t\tcontinue;\n\t\t}\n\t\toldState.set(newKey, newValue);\n\t}\n\treturn oldState;\n};\n\nconst applyState =\n\t(isViewTransition: boolean = false) =>\n\t() => {\n\t\t/*\n\t\tIf it's an uncancelled view transition, we want a completely new state \n\t\t(otherwise the new view will 'lose' to the old view in the case of a conflict). \n\t\tIf it's not a view transition, we want to shallow clone the state object.\n\t\tmergeState will update the top-level keys of the state passed to it, but if \n\t\tthe user calls preventDefault on the serverStateLoaded we're supposed to abort \n\t\tthe update. So it won't do to have already updated the underlying global state \n\t\tmap. And on the other hand we do a shallow, not deep, clone because the user \n\t\tcan mutate their own stored values any time they want.\n\t\t*/\n\t\tconst startingState = isViewTransition ? new Map() : new Map(state);\n\t\tnextStates?.reduce(mergeState, startingState);\n\t\tconst event = new ServerStateLoaded(new Map(state), startingState, {\n\t\t\tcancelable: true,\n\t\t});\n\n\t\tif (document.dispatchEvent(event)) {\n\t\t\tstate.clear();\n\t\t\tfor (const [key, value] of event.serverState.entries()) {\n\t\t\t\tstate.set(key, value);\n\t\t\t}\n\t\t}\n\t};\n\napplyState()();\n\ndocument.addEventListener('astro:before-swap', (event) => {\n\tnextStates = loadState(event.newDocument);\n});\ndocument.addEventListener('astro:after-swap', applyState(true));\n\nexport const getState = (key: string, valueIfMissing?: unknown): unknown => {\n\tif (!state.has(key)) {\n\t\tif (valueIfMissing !== undefined) {\n\t\t\tstate.set(key, valueIfMissing);\n\t\t}\n\t}\n\treturn state.get(key);\n};\n\nexport const hasState = (key: string) => state.has(key);\n\nexport const setState = (key: string, value: unknown): void => {\n\tif (value === undefined) {\n\t\tstate.delete(key);\n\t} else {\n\t\tstate.set(key, value);\n\t}\n};\n"]}
1
+ {"version":3,"sources":["../../src/runtime/clientState.ts"],"names":["revivers","value","loadState","doc","element","state","parse","nextStates","initialiseState","STATE_WINDOW_NAMESPACE","mergeState","oldState","newState","newKey","newValue","applyState","isViewTransition","startingState","event","ServerStateLoaded","key","getState","valueIfMissing","hasState","setState"],"mappings":"yEAGA,MAAMA,CAAAA,CAAgD,CACrD,SAAA,CAAW,IAAG,CAAA,CAAA,CACd,IAAMC,CAAAA,EAAU,IAAI,GAAA,CAAIA,CAAK,CAAA,CAC7B,IAAA,CAAOA,CAAAA,EAAU,IAAI,IAAA,CAAKA,CAAK,CAAA,CAC/B,YAAA,CAAeA,CAAAA,EAAU,MAAA,CAAO,IAAIA,CAAK,CAAA,CACzC,eAAA,CAAkBA,CAAAA,EAAU,MAAA,CAAOA,CAA4B,CAChE,CAAA,CAEMC,CAAAA,CAAaC,CAAAA,EACD,KAAA,CAAM,IAAA,CAAKA,CAAAA,CAAI,gBAAA,CAAiB,uBAAuB,CAAC,CAAA,CAEvE,GAAA,CAAKC,CAAAA,EAAY,CACjB,GAAIA,CAAAA,EAAS,WAAA,CAAa,CACzB,MAAMC,CAAAA,CAAQC,KAAAA,CAAMF,CAAAA,CAAQ,WAAA,CAAaJ,CAAQ,EACjD,OAAAI,CAAAA,CAAQ,MAAA,EAAO,CACRC,CACR,CACD,CAAC,CAAA,CACA,MAAA,CAAQA,CAAAA,EAA0B,CAAC,CAACA,CAAK,CAAA,CAI5C,IAAIE,CAAAA,CAAaL,CAAAA,CAAU,QAAQ,CAAA,CAEnC,MAAMM,CAAAA,CAAkB,IAAa,CACpC,MAAMC,CAAAA,CAAyB,gCAAA,CAC/B,OAAMA,CAAAA,IAA0B,MAAA,GAC/B,MAAA,CAAOA,CAAsB,CAAA,CAAI,IAAI,GAAA,CAAA,CAE/B,MAAA,CAAOA,CAAsB,CACrC,CAAA,CAEaJ,CAAAA,CAAQG,CAAAA,EAAgB,CAE/BE,CAAAA,CAAa,CAACC,CAAAA,CAAiBC,C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KE,CAAc,CAAA,CAGxBjB,CAAAA,CAAM,GAAA,CAAIe,CAAG,CAAA,CAAA,CAGRG,CAAAA,CAAYH,CAAAA,EAAgBf,CAAAA,CAAM,GAAA,CAAIe,CAAG,CAAA,CAEzCI,CAAAA,CAAW,CAACJ,CAAAA,CAAanB,CAAAA,GAAyB,CAC9DI,CAAAA,CAAM,GAAA,CAAIe,CAAAA,CAAKnB,CAAK,EACrB","file":"clientState.js","sourcesContent":["import { parse } from 'devalue';\nimport { type State, ServerStateLoaded } from '../events.js';\n\nconst revivers: Record<string, (value: any) => any> = {\n\tundefined: () => undefined,\n\tURL: (value) => new URL(value),\n\tDate: (value) => new Date(value),\n\tGlobalSymbol: (value) => Symbol.for(value),\n\tWellKnownSymbol: (value) => Symbol[value as keyof typeof Symbol],\n};\n\nconst loadState = (doc: Document): State[] => {\n\tconst elements = Array.from(doc.querySelectorAll('script.it-astro-state'));\n\tconst scripts = elements\n\t\t.map((element) => {\n\t\t\tif (element?.textContent) {\n\t\t\t\tconst state = parse(element.textContent, revivers);\n\t\t\t\telement.remove();\n\t\t\t\treturn state;\n\t\t\t}\n\t\t})\n\t\t.filter((state): state is State => !!state);\n\treturn scripts;\n};\n\nlet nextStates = loadState(document);\n\nconst initialiseState = (): State => {\n\tconst STATE_WINDOW_NAMESPACE = '__@it-astro:request-state-data' as const;\n\tif (!(STATE_WINDOW_NAMESPACE in window)) {\n\t\twindow[STATE_WINDOW_NAMESPACE] = new Map();\n\t}\n\treturn window[STATE_WINDOW_NAMESPACE] as State;\n};\n\nexport const state = initialiseState();\n\nconst mergeState = (oldState: State, newState: State) => {\n\tfor (const [newKey, newValue] of newState.entries()) {\n\t\tconst isHit = oldState.has(newKey);\n\t\tif (isHit) {\n\t\t\tcontinue;\n\t\t}\n\t\toldState.set(newKey, newValue);\n\t}\n\treturn oldState;\n};\n\nconst applyState =\n\t(isViewTransition: boolean = false) =>\n\t() => {\n\t\t/*\n\t\tIf it's an uncancelled view transition, we want a completely new state \n\t\t(otherwise the new view will 'lose' to the old view in the case of a conflict). \n\t\tIf it's not a view transition, we want to shallow clone the state object.\n\t\tmergeState will update the top-level keys of the state passed to it, but if \n\t\tthe user calls preventDefault on the serverStateLoaded we're supposed to abort \n\t\tthe update. So it won't do to have already updated the underlying global state \n\t\tmap. And on the other hand we do a shallow, not deep, clone because the user \n\t\tcan mutate their own stored values any time they want.\n\t\t*/\n\t\tconst startingState = isViewTransition ? new Map() : new Map(state);\n\t\tnextStates?.reduce(mergeState, startingState);\n\t\tconst event = new ServerStateLoaded(new Map(state), startingState, {\n\t\t\tcancelable: true,\n\t\t});\n\n\t\tif (document.dispatchEvent(event)) {\n\t\t\tstate.clear();\n\t\t\tfor (const [key, value] of event.serverState.entries()) {\n\t\t\t\tstate.set(key, value);\n\t\t\t}\n\t\t}\n\t};\n\napplyState()();\n\ndocument.addEventListener('astro:before-swap', (event) => {\n\tnextStates = loadState(event.newDocument);\n});\ndocument.addEventListener('astro:after-swap', applyState(true));\n\nexport const getState = (key: string, valueIfMissing?: unknown): unknown => {\n\tif (!state.has(key)) {\n\t\tif (valueIfMissing !== undefined) {\n\t\t\tstate.set(key, valueIfMissing);\n\t\t}\n\t}\n\treturn state.get(key);\n};\n\nexport const hasState = (key: string) => state.has(key);\n\nexport const setState = (key: string, value: unknown): void => {\n\tstate.set(key, value);\n};\n"]}
@@ -1,2 +1,2 @@
1
- import {AsyncLocalStorage}from'node:async_hooks';import {stringify}from'devalue';const o=new AsyncLocalStorage,l=(t,e)=>{const n=o.getStore();if(n!==void 0)return n.has(t)||e!==void 0&&n.set(t,e),n.get(t)},p=t=>o.getStore()?.has(t)||false,S=(t,e)=>{const n=o.getStore();e===void 0?n?.delete(t):n?.set(t,e);},i=new Map(Object.entries(Symbol).filter(([t,e])=>typeof e=="symbol"&&typeof t=="string").map(([t,e])=>[e,t])),a={URL:t=>t instanceof URL&&t.href,Date:t=>t instanceof Date&&t.valueOf(),GlobalSymbol:t=>typeof t=="symbol"&&t.description!==void 0&&t===Symbol.for(t.description)&&t.description,WellKnownSymbol:t=>typeof t=="symbol"&&i.get(t)},d=async t=>{const e=new Map;return {result:await o.run(e,t),getState:()=>e.size>0&&stringify(e,a)}};export{d as collectState,l as getState,p as hasState,S as setState};//# sourceMappingURL=serverState.js.map
1
+ import {AsyncLocalStorage}from'node:async_hooks';import {stringify}from'devalue';const o=new AsyncLocalStorage,l=(t,e)=>{const n=o.getStore();if(n!==void 0)return n.has(t)||e!==void 0&&n.set(t,e),n.get(t)},p=t=>o.getStore()?.has(t)||false,S=(t,e)=>{o.getStore()?.set(t,e);},i=new Map(Object.entries(Symbol).filter(([t,e])=>typeof e=="symbol"&&typeof t=="string").map(([t,e])=>[e,t])),a={undefined:t=>t===void 0,URL:t=>t instanceof URL&&t.href,Date:t=>t instanceof Date&&t.valueOf(),GlobalSymbol:t=>typeof t=="symbol"&&t.description!==void 0&&t===Symbol.for(t.description)&&t.description,WellKnownSymbol:t=>typeof t=="symbol"&&i.get(t)},d=async t=>{const e=new Map;return {result:await o.run(e,t),getState:()=>e.size>0&&stringify(e,a)}};export{d as collectState,l as getState,p as hasState,S as setState};//# sourceMappingURL=serverState.js.map
2
2
  //# sourceMappingURL=serverState.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/runtime/serverState.ts"],"names":["store","AsyncLocalStorage","getState","key","valueIfMissing","state","hasState","setState","value","wellKnownSymbols","reducers","collectState","cb","stringify"],"mappings":"iFAKA,MAAMA,CAAAA,CAAQ,IAAIC,kBAELC,CAAAA,CAAW,CAACC,EAAaC,CAAAA,GAAsC,CAC3E,MAAMC,CAAAA,CAAQL,CAAAA,CAAM,QAAA,EAAS,CAC7B,GAAIK,CAAAA,GAAU,MAAA,CAEd,OAAKA,CAAAA,CAAM,GAAA,CAAIF,CAAG,CAAA,EACbC,CAAAA,GAAmB,MAAA,EACtBC,CAAAA,CAAM,IAAIF,CAAAA,CAAKC,CAAc,EAGxBC,CAAAA,CAAM,GAAA,CAAIF,CAAG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IAAMP,EAAM,IAAA,CAAO,CAAA,EAAKQ,UAAUR,CAAAA,CAAOK,CAAQ,CAC5D,CACD","file":"serverState.js","sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks';\nimport { stringify } from 'devalue';\n\ntype State = Map<string, unknown>;\n\nconst store = new AsyncLocalStorage<State>();\n\nexport const getState = (key: string, valueIfMissing?: unknown): unknown => {\n\tconst state = store.getStore();\n\tif (state === undefined) return;\n\n\tif (!state.has(key)) {\n\t\tif (valueIfMissing !== undefined) {\n\t\t\tstate.set(key, valueIfMissing);\n\t\t}\n\t}\n\treturn state.get(key);\n};\n\nexport const hasState = (key: string) => store.getStore()?.has(key) || false;\n\nexport const setState = (key: string, value: unknown): void => {\n\tconst state = store.getStore();\n\tif (value === undefined) {\n\t\tstate?.delete(key);\n\t} else {\n\t\tstate?.set(key, value);\n\t}\n};\n\nexport type CollectedState<T> = {\n\tgetState: () => string | false;\n\tresult: T;\n};\n\nconst wellKnownSymbols = new Map(\n\tObject.entries(Symbol)\n\t\t.filter(([key, value]) => typeof value === 'symbol' && typeof key === 'string')\n\t\t.map(([key, value]) => [value, key])\n);\n\nconst reducers: Record<string, (value: any) => any> = {\n\tURL: (value) => value instanceof URL && value.href,\n\tDate: (value) => value instanceof Date && value.valueOf(),\n\tGlobalSymbol: (value) =>\n\t\ttypeof value === 'symbol' &&\n\t\tvalue.description !== undefined &&\n\t\tvalue === Symbol.for(value.description) &&\n\t\tvalue.description,\n\tWellKnownSymbol: (value) => typeof value === 'symbol' && wellKnownSymbols.get(value),\n};\n\nexport const collectState = async <R>(cb: () => Promise<R>): Promise<CollectedState<R>> => {\n\tconst state = new Map();\n\tconst result = await store.run(state, cb);\n\treturn {\n\t\tresult,\n\t\tgetState: () => state.size > 0 && stringify(state, reducers),\n\t};\n};\n"]}
1
+ {"version":3,"sources":["../../src/runtime/serverState.ts"],"names":["store","AsyncLocalStorage","getState","key","valueIfMissing","state","hasState","setState","value","wellKnownSymbols","reducers","collectState","cb","st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AAA,CACP,eAAA,CAAkBA,CAAAA,EAAU,OAAOA,CAAAA,EAAU,QAAA,EAAYC,CAAAA,CAAiB,GAAA,CAAID,CAAK,CACpF,CAAA,CAEaG,EAAe,MAAUC,CAAAA,EAAqD,CAC1F,MAAMP,CAAAA,CAAQ,IAAI,GAAA,CAElB,OAAO,CACN,MAAA,CAFc,MAAML,CAAAA,CAAM,GAAA,CAAIK,EAAOO,CAAE,CAAA,CAGvC,QAAA,CAAU,IAAMP,EAAM,IAAA,CAAO,CAAA,EAAKQ,UAAUR,CAAAA,CAAOK,CAAQ,CAC5D,CACD","file":"serverState.js","sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks';\nimport { stringify } from 'devalue';\n\ntype State = Map<string, unknown>;\n\nconst store = new AsyncLocalStorage<State>();\n\nexport const getState = (key: string, valueIfMissing?: unknown): unknown => {\n\tconst state = store.getStore();\n\tif (state === undefined) return;\n\n\tif (!state.has(key)) {\n\t\tif (valueIfMissing !== undefined) {\n\t\t\tstate.set(key, valueIfMissing);\n\t\t}\n\t}\n\treturn state.get(key);\n};\n\nexport const hasState = (key: string) => store.getStore()?.has(key) || false;\n\nexport const setState = (key: string, value: unknown): void => {\n\tconst state = store.getStore();\n\tstate?.set(key, value);\n};\n\nexport type CollectedState<T> = {\n\tgetState: () => string | false;\n\tresult: T;\n};\n\nconst wellKnownSymbols = new Map(\n\tObject.entries(Symbol)\n\t\t.filter(([key, value]) => typeof value === 'symbol' && typeof key === 'string')\n\t\t.map(([key, value]) => [value, key])\n);\n\nconst reducers: Record<string, (value: any) => any> = {\n\tundefined: (value) => value === undefined,\n\tURL: (value) => value instanceof URL && value.href,\n\tDate: (value) => value instanceof Date && value.valueOf(),\n\tGlobalSymbol: (value) =>\n\t\ttypeof value === 'symbol' &&\n\t\tvalue.description !== undefined &&\n\t\tvalue === Symbol.for(value.description) &&\n\t\tvalue.description,\n\tWellKnownSymbol: (value) => typeof value === 'symbol' && wellKnownSymbols.get(value),\n};\n\nexport const collectState = async <R>(cb: () => Promise<R>): Promise<CollectedState<R>> => {\n\tconst state = new Map();\n\tconst result = await store.run(state, cb);\n\treturn {\n\t\tresult,\n\t\tgetState: () => state.size > 0 && stringify(state, reducers),\n\t};\n};\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inox-tools/request-state",
3
- "version": "0.7.0",
3
+ "version": "0.7.1",
4
4
  "description": "Shared request state between server and client",
5
5
  "keywords": [
6
6
  "astro-integration",
@@ -2,6 +2,7 @@ import { parse } from 'devalue';
2
2
  import { type State, ServerStateLoaded } from '../events.js';
3
3
 
4
4
  const revivers: Record<string, (value: any) => any> = {
5
+ undefined: () => undefined,
5
6
  URL: (value) => new URL(value),
6
7
  Date: (value) => new Date(value),
7
8
  GlobalSymbol: (value) => Symbol.for(value),
@@ -91,9 +92,5 @@ export const getState = (key: string, valueIfMissing?: unknown): unknown => {
91
92
  export const hasState = (key: string) => state.has(key);
92
93
 
93
94
  export const setState = (key: string, value: unknown): void => {
94
- if (value === undefined) {
95
- state.delete(key);
96
- } else {
97
- state.set(key, value);
98
- }
95
+ state.set(key, value);
99
96
  };
@@ -21,11 +21,7 @@ export const hasState = (key: string) => store.getStore()?.has(key) || false;
21
21
 
22
22
  export const setState = (key: string, value: unknown): void => {
23
23
  const state = store.getStore();
24
- if (value === undefined) {
25
- state?.delete(key);
26
- } else {
27
- state?.set(key, value);
28
- }
24
+ state?.set(key, value);
29
25
  };
30
26
 
31
27
  export type CollectedState<T> = {
@@ -40,6 +36,7 @@ const wellKnownSymbols = new Map(
40
36
  );
41
37
 
42
38
  const reducers: Record<string, (value: any) => any> = {
39
+ undefined: (value) => value === undefined,
43
40
  URL: (value) => value instanceof URL && value.href,
44
41
  Date: (value) => value instanceof Date && value.valueOf(),
45
42
  GlobalSymbol: (value) =>