diff options
| author | Aleksey Kladov <aleksey.kladov@gmail.com> | 2020-03-16 19:23:38 +0100 |
|---|---|---|
| committer | Aleksey Kladov <aleksey.kladov@gmail.com> | 2020-03-16 22:02:11 +0100 |
| commit | ae662617a2bc49d025adaf9c4a8ff2dfa557d36c (patch) | |
| tree | f321ad0a529283bbb276095da7644d8f1ba663d2 /editors/code/src/persistent_state.ts | |
| parent | 2e9b6320e66cb764b9683a38c284a06b7c35aab6 (diff) | |
| download | rust-ae662617a2bc49d025adaf9c4a8ff2dfa557d36c.tar.gz rust-ae662617a2bc49d025adaf9c4a8ff2dfa557d36c.zip | |
Separate persistent mutable state from config
That way, we clearly see which things are not change, and we also clearly see which things are persistent.
Diffstat (limited to 'editors/code/src/persistent_state.ts')
| -rw-r--r-- | editors/code/src/persistent_state.ts | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/editors/code/src/persistent_state.ts b/editors/code/src/persistent_state.ts new file mode 100644 index 00000000000..13095b80658 --- /dev/null +++ b/editors/code/src/persistent_state.ts @@ -0,0 +1,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); + } +} |
