Skip to content

Commit bc87d6d

Browse files
author
Veetaha
committed
vscode-postrefactor: global storages
1 parent 77a206e commit bc87d6d

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

editors/code/src/config.ts

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ export class Config {
186186
"rust-analyzer-server-release-date",
187187
this.ctx.globalState
188188
);
189-
readonly serverReleaseTag = new StringStorage(
190-
"rust-analyzer-release-tag", this.ctx.globalState
189+
readonly serverReleaseTag = new Storage<null | string>(
190+
"rust-analyzer-release-tag", this.ctx.globalState, null
191191
);
192192

193193
// We don't do runtime config validation here for simplicity. More on stackoverflow:
@@ -234,37 +234,36 @@ export class Config {
234234
get withSysroot() { return this.cfg.get("withSysroot", true) as boolean; }
235235
}
236236

237-
export class StringStorage {
237+
export class Storage<T> {
238238
constructor(
239239
private readonly key: string,
240-
private readonly storage: vscode.Memento
240+
private readonly storage: vscode.Memento,
241+
private readonly defaultVal: T
241242
) { }
242243

243-
get(): null | string {
244-
const tag = this.storage.get(this.key, null);
245-
log.debug(this.key, "==", tag);
246-
return tag;
244+
get(): T {
245+
const val = this.storage.get(this.key, this.defaultVal);
246+
log.debug(this.key, "==", val);
247+
return val;
247248
}
248-
async set(tag: string) {
249-
log.debug(this.key, "=", tag);
250-
await this.storage.update(this.key, tag);
249+
async set(val: T) {
250+
log.debug(this.key, "=", val);
251+
await this.storage.update(this.key, val);
251252
}
252253
}
253254
export class DateStorage {
255+
inner: Storage<null | string>;
254256

255-
constructor(
256-
private readonly key: string,
257-
private readonly storage: vscode.Memento
258-
) { }
257+
constructor(key: string, storage: vscode.Memento) {
258+
this.inner = new Storage(key, storage, null);
259+
}
259260

260261
get(): null | Date {
261-
const date = this.storage.get(this.key, null);
262-
log.debug(this.key, "==", date);
263-
return date ? new Date(date) : null;
262+
const dateStr = this.inner.get();
263+
return dateStr ? new Date(dateStr) : null;
264264
}
265265

266266
async set(date: null | Date) {
267-
log.debug(this.key, "=", date);
268-
await this.storage.update(this.key, date);
267+
await this.inner.set(date ? date.toString() : null);
269268
}
270269
}

0 commit comments

Comments
 (0)