summaryrefslogtreecommitdiffstats
path: root/apps/web-shared/src/lib/persistent-store.ts
blob: 922f3abebbc084cab7d6348df500c81aabf440d1 (plain) (blame)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { writable as _writable, readable as _readable, } from "svelte/store";
import type { Writable, Readable, StartStopNotifier } from "svelte/store";

enum StoreType {
    SESSION = 0,
    LOCAL = 1
}

interface StoreOptions {
    store?: StoreType;
}

const default_store_options = {
    store: StoreType.SESSION
} as StoreOptions;

interface WritableStore<T> {
    name: string,
    initialState: T,
    options?: StoreOptions
}

interface ReadableStore<T> {
    name: string,
    initialState: T,
    callback: StartStopNotifier<any>,
    options?: StoreOptions
}

function get_store(type: StoreType): Storage {
    switch (type) {
        case StoreType.SESSION:
            return window.sessionStorage;
        case StoreType.LOCAL:
            return window.localStorage;
    }
}

function prepared_store_value(value: any): string {
    try {
        return JSON.stringify(value);
    } catch (e) {
        console.error(e);
        return "__INVALID__";
    }
}

function get_store_value<T>(options: WritableStore<T> | ReadableStore<T>): any {
    try {
        const storage = get_store(options.options.store);
        const value = storage.getItem(options.name);
        if (!value) return false;
        return JSON.parse(value);
    } catch (e) {
        console.error(e);
        return { __INVALID__: true };
    }
}

function hydrate<T>(store: Writable<T>, options: WritableStore<T> | ReadableStore<T>): void {
    const value = get_store_value<T>(options);
    if (value && store.set) store.set(value);
}

function subscribe<T>(store: Writable<T> | Readable<T>, options: WritableStore<T> | ReadableStore<T>): void {
    const storage = get_store(options.options.store);
    if (!store.subscribe) return;
    store.subscribe((state: any) => {
        storage.setItem(options.name, prepared_store_value(state));
    });
}

function writable_persistent<T>(options: WritableStore<T>): Writable<T> {
    if (options.options === undefined) options.options = default_store_options;
    console.log("Creating writable store with options: ", options);
    const store = _writable<T>(options.initialState);
    hydrate(store, options);
    subscribe(store, options);
    return store;
}

function readable_persistent<T>(options: ReadableStore<T>): Readable<T> {
    if (options.options === undefined) options.options = default_store_options;
    console.log("Creating readable store with options: ", options);
    const store = _readable<T>(options.initialState, options.callback);
    // hydrate(store, options);
    subscribe(store, options);
    return store;
}

export {
    writable_persistent,
    readable_persistent,
    StoreType
};

export type {
    WritableStore,
    ReadableStore,
    StoreOptions
};