-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstate.ts
66 lines (56 loc) · 1.51 KB
/
state.ts
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 * as vscode from "vscode";
import type { Spec } from "../download";
import { Chat } from "../prompt/promptChat";
const StateValues = {
inlineSuggestModeAuto: true,
serverSpec: null,
};
type StateValuesType = {
inlineSuggestModeAuto: boolean;
serverSpec: Spec | null;
[key: `chat-${string}`]: Chat | undefined;
};
class State {
state?: vscode.Memento;
constructor() {}
public init(state: vscode.Memento) {
this.state = state;
}
public get<T extends keyof StateValuesType & string>(
key: T
): StateValuesType[T] {
// @ts-ignore
return this.state?.get(key) ?? StateValues[key];
}
public async update<T extends keyof StateValuesType>(
key: T,
value: StateValuesType[T]
) {
await this.state?.update(key, value);
}
public getChats(): Chat[] {
const allKeys = (this.state?.keys() ||
[]) as unknown as (keyof StateValuesType)[];
return allKeys
.filter((key) => key.startsWith("chat-"))
.map((key) => {
return this.get(key as `chat-${string}`) as Chat;
});
}
public async delete<T extends keyof StateValuesType>(key: T) {
await this.state?.update(key, undefined);
}
public async deleteChats() {
const allKeys = (this.state?.keys() ||
[]) as unknown as (keyof StateValuesType)[];
await Promise.all(
allKeys
.filter((key) => key.startsWith("chat-"))
.map((key) => this.delete(key as `chat-${string}`))
);
}
}
export const state = {
workspace: new State(),
global: new State(),
};