Files
speckle-server/packages/frontend-2/lib/common/composables/scopedState.ts
T
Kristaps Fabians Geikins b02a07e2b6 feat: Frontend 2.0 MVP
2023-05-08 10:47:01 +03:00

21 lines
644 B
TypeScript

/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import { 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
}