Files
speckle-server/packages/frontend-2/lib/common/composables/scopedState.ts
T
Kristaps Fabians Geikins c54d15fd93 feat: authz frontend foundation + reworked errors (#4275)
* feat: authz frontend foundation + reworked errors

* lint fixes

* test fix

* fixed noCache() util
2025-03-27 16:13:35 +02:00

18 lines
523 B
TypeScript

/**
* Similar to nuxt's useState() except state is scoped to only the SSR request or only the client-side session.
* The state doesn't get serialized in SSR and thus won't be transferred to the client-side session
*/
export function useScopedState<T>(key: string | symbol, init: () => T) {
const nuxtApp = useNuxtApp()
if (!nuxtApp.__scopedStates) {
nuxtApp.__scopedStates = {}
}
if (!nuxtApp.__scopedStates[key]) {
nuxtApp.__scopedStates[key] = init()
}
return nuxtApp.__scopedStates[key] as T
}