aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/persistent_state.ts
blob: 13095b806584f5dc1a76fe9e8bccd4f1590d2044 (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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import * as vscode from 'vscode';
import { log } from "./util";

export class PersistentState {
    constructor(private readonly ctx: vscode.ExtensionContext) {
    }

    readonly installedNightlyExtensionReleaseDate = new DateStorage(
        "installed-nightly-extension-release-date",
        this.ctx.globalState
    );
    readonly serverReleaseDate = new DateStorage("server-release-date", this.ctx.globalState);
    readonly serverReleaseTag = new Storage<null | string>("server-release-tag", this.ctx.globalState, null);
}


export class Storage<T> {
    constructor(
        private readonly key: string,
        private readonly storage: vscode.Memento,
        private readonly defaultVal: T
    ) { }

    get(): T {
        const val = this.storage.get(this.key, this.defaultVal);
        log.debug(this.key, "==", val);
        return val;
    }
    async set(val: T) {
        log.debug(this.key, "=", val);
        await this.storage.update(this.key, val);
    }
}
export class DateStorage {
    inner: Storage<null | string>;

    constructor(key: string, storage: vscode.Memento) {
        this.inner = new Storage(key, storage, null);
    }

    get(): null | Date {
        const dateStr = this.inner.get();
        return dateStr ? new Date(dateStr) : null;
    }

    async set(date: null | Date) {
        await this.inner.set(date ? date.toString() : null);
    }
}