aboutsummaryrefslogtreecommitdiffstats
path: root/code/app/src/utilities/global-state.ts
blob: b585ced807ad0bc0699be3416232ad730c1c9dba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { get } from "svelte/store";
import { create_writable_persistent } from "./persistent-store";

const state = create_writable_persistent<any>({
    initialState: {},
    name: "global-state"
});

export type GlobalStateKeys = "isLoggedIn" | "showEmailValidatedAlertWhenLoggedIn" | "all";

export function fgs(key: GlobalStateKeys): any {
    const value = get(state);
    if (key === "all") return value;
    return value[key];
}

export function sgs(key: GlobalStateKeys, value: any) {
    if (key === "all") throw new Error("Not allowed to set global state key: all");
    const stateValue = get(state);
    stateValue[key] = JSON.stringify(value)
    state.set(stateValue);
}