forked from microsoft/vscode-python-environments
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpersistentState.ts
More file actions
66 lines (58 loc) · 2.37 KB
/
persistentState.ts
File metadata and controls
66 lines (58 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { ExtensionContext, Memento } from 'vscode';
import { traceError } from './logging';
import { createDeferred, Deferred } from './utils/deferred';
export interface PersistentState {
get<T>(key: string, defaultValue?: T): Promise<T | undefined>;
set<T>(key: string, value: T): Promise<void>;
clear(keys?: string[]): Promise<void>;
}
class PersistentStateImpl implements PersistentState {
private clearing: Deferred<void>;
constructor(private readonly momento: Memento) {
this.clearing = createDeferred<void>();
this.clearing.resolve();
}
async get<T>(key: string, defaultValue?: T): Promise<T | undefined> {
await this.clearing.promise;
if (defaultValue === undefined) {
return this.momento.get<T>(key);
}
return this.momento.get<T>(key, defaultValue);
}
async set<T>(key: string, value: T): Promise<void> {
await this.clearing.promise;
await this.momento.update(key, value);
const before = JSON.stringify(value);
const after = JSON.stringify(await this.momento.get<T>(key));
if (before !== after) {
await this.momento.update(key, undefined);
traceError('Error while updating state for key:', key);
}
}
async clear(keys?: string[]): Promise<void> {
if (this.clearing.completed) {
this.clearing = createDeferred<void>();
const _keys = keys ?? this.momento.keys();
await Promise.all(_keys.map((key) => this.momento.update(key, undefined)));
this.clearing.resolve();
}
return this.clearing.promise;
}
}
const _workspace = createDeferred<PersistentState>();
const _global = createDeferred<PersistentState>();
export function setPersistentState(context: ExtensionContext): void {
_workspace.resolve(new PersistentStateImpl(context.workspaceState));
_global.resolve(new PersistentStateImpl(context.globalState));
}
export function getWorkspacePersistentState(): Promise<PersistentState> {
return _workspace.promise;
}
export function getGlobalPersistentState(): Promise<PersistentState> {
return _global.promise;
}
export async function clearPersistentState(): Promise<void> {
const [workspace, global] = await Promise.all([_workspace.promise, _global.promise]);
await Promise.all([workspace.clear(), global.clear()]);
return undefined;
}