forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestPersistentStateFactory.ts
More file actions
53 lines (46 loc) · 1.91 KB
/
testPersistentStateFactory.ts
File metadata and controls
53 lines (46 loc) · 1.91 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
import { Memento } from 'vscode';
import { PersistentStateFactory } from '../../client/common/persistentState';
import { IPersistentState, IPersistentStateFactory } from '../../client/common/types';
const PrefixesToStore = ['INTERPRETERS_CACHE'];
// tslint:disable-next-line: no-any
const persistedState = new Map<string, any>();
class TestPersistentState<T> implements IPersistentState<T> {
constructor(private key: string, defaultValue?: T | undefined) {
if (defaultValue) {
persistedState.set(key, defaultValue);
}
}
public get value(): T {
return persistedState.get(this.key);
}
public async updateValue(value: T): Promise<void> {
persistedState.set(this.key, value);
}
}
// This class is used to make certain values persist across tests.
export class TestPersistentStateFactory implements IPersistentStateFactory {
private realStateFactory: PersistentStateFactory;
constructor(globalState: Memento, localState: Memento) {
this.realStateFactory = new PersistentStateFactory(globalState, localState);
}
public createGlobalPersistentState<T>(
key: string,
defaultValue?: T | undefined,
expiryDurationMs?: number | undefined
): IPersistentState<T> {
if (PrefixesToStore.find((p) => key.startsWith(p))) {
return new TestPersistentState(key, defaultValue);
}
return this.realStateFactory.createGlobalPersistentState(key, defaultValue, expiryDurationMs);
}
public createWorkspacePersistentState<T>(
key: string,
defaultValue?: T | undefined,
expiryDurationMs?: number | undefined
): IPersistentState<T> {
if (PrefixesToStore.find((p) => key.startsWith(p))) {
return new TestPersistentState(key, defaultValue);
}
return this.realStateFactory.createWorkspacePersistentState(key, defaultValue, expiryDurationMs);
}
}