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.
This commit is contained in:
Aleksey Kladov 2020-03-16 19:23:38 +01:00
parent 2e9b6320e6
commit ae662617a2
7 changed files with 80 additions and 65 deletions

View file

@ -182,13 +182,6 @@ export class Config {
return this.createGithubReleaseSource("rust-analyzer.vsix", NIGHTLY_TAG);
}
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);
// We don't do runtime config validation here for simplicity. More on stackoverflow:
// https://stackoverflow.com/questions/60135780/what-is-the-best-way-to-type-check-the-configuration-for-vscode-extension
@ -232,37 +225,3 @@ export class Config {
// for internal use
get withSysroot() { return this.cfg.get("withSysroot", true) as boolean; }
}
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);
}
}