aboutsummaryrefslogtreecommitdiff
path: root/front-end/src/store/globalState.ts
blob: 9e700ebeecd3a74336464ab3f2f9104d574d8284 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import 'pinia'
import { shallowRef } from 'vue';
import { useAutoHeartbeat } from '@vnuge/vnlib.browser';
import { toRefs, useLocalStorage } from '@vueuse/core';
import { PiniaPluginContext, PiniaPlugin } from 'pinia'
import { defaults } from 'lodash-es';

declare module 'pinia' {
    export interface PiniaCustomProperties {
        autoHeartbeat: boolean;
    }
}

export const globalStatePlugin: PiniaPlugin = ({ store }: PiniaPluginContext) =>{

    //Get shared global state storage
    const mainState = useLocalStorage("vn-state", { ahEnabled: false });
    
    //Set defaults to avoid undefined errors from toRefs
    defaults(mainState.value, { ahEnabled: false })
    
    const { ahEnabled } = toRefs(mainState)

    //Setup heartbeat for 5 minutes
    useAutoHeartbeat(shallowRef(5 * 60 * 1000), ahEnabled)
    
    return{
        autoHeartbeat: ahEnabled,
    }
}