mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-08-23 03:44:23 +00:00
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:
parent
2e9b6320e6
commit
ae662617a2
7 changed files with 80 additions and 65 deletions
49
editors/code/src/persistent_state.ts
Normal file
49
editors/code/src/persistent_state.ts
Normal file
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue