]> git.lizzy.rs Git - rust.git/blob - editors/code/src/persistent_state.ts
Merge #3614
[rust.git] / editors / code / src / persistent_state.ts
1 import * as vscode from 'vscode';
2 import { log } from "./util";
3
4 export class PersistentState {
5     constructor(private readonly ctx: vscode.ExtensionContext) {
6     }
7
8     readonly installedNightlyExtensionReleaseDate = new DateStorage(
9         "installed-nightly-extension-release-date",
10         this.ctx.globalState
11     );
12     readonly serverReleaseDate = new DateStorage("server-release-date", this.ctx.globalState);
13     readonly serverReleaseTag = new Storage<null | string>("server-release-tag", this.ctx.globalState, null);
14 }
15
16
17 export class Storage<T> {
18     constructor(
19         private readonly key: string,
20         private readonly storage: vscode.Memento,
21         private readonly defaultVal: T
22     ) { }
23
24     get(): T {
25         const val = this.storage.get(this.key, this.defaultVal);
26         log.debug(this.key, "==", val);
27         return val;
28     }
29     async set(val: T) {
30         log.debug(this.key, "=", val);
31         await this.storage.update(this.key, val);
32     }
33 }
34 export class DateStorage {
35     inner: Storage<null | string>;
36
37     constructor(key: string, storage: vscode.Memento) {
38         this.inner = new Storage(key, storage, null);
39     }
40
41     get(): null | Date {
42         const dateStr = this.inner.get();
43         return dateStr ? new Date(dateStr) : null;
44     }
45
46     async set(date: null | Date) {
47         await this.inner.set(date ? date.toString() : null);
48     }
49 }