83d8035dc2
* root + server * frontend * frontend-2 * dui3 * dui3 * tailwind theme * ui-components * preview service * viewer * viewer-sandbox * fileimport-service * webhook service * objectloader * shared * ui-components-nuxt * WIP full config * WIP full linter * eslint projectwide util * minor fix * removing redundant ci * clean up test errors * fixed prettier formatting * CI improvements * TSC lint fix * 'buildBatch' needs to be async since some batch types (like Text) require it. Removed a disabled liniting rule from ObjLoader * removed unnecessary void --------- Co-authored-by: AlexandruPopovici <alexandrupopoviciioan@gmail.com>
20 lines
585 B
TypeScript
20 lines
585 B
TypeScript
import type { NuxtApp } from 'nuxt/dist/app/nuxt'
|
|
|
|
/**
|
|
* 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() as NuxtApp
|
|
|
|
if (!nuxtApp.__scopedStates) {
|
|
nuxtApp.__scopedStates = {}
|
|
}
|
|
|
|
if (!nuxtApp.__scopedStates[key]) {
|
|
nuxtApp.__scopedStates[key] = init()
|
|
}
|
|
|
|
return nuxtApp.__scopedStates[key] as T
|
|
}
|