-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathmementoManager.ts
More file actions
84 lines (76 loc) · 2.55 KB
/
mementoManager.ts
File metadata and controls
84 lines (76 loc) · 2.55 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import type { Memento } from "vscode";
// Maximum number of recent URLs to store.
const MAX_URLS = 10;
export class MementoManager {
constructor(private readonly memento: Memento) {}
/**
* Add a URL to the history of recently accessed URLs.
* Used by the URL picker to show recent deployments.
*/
public async addToUrlHistory(url: string): Promise<void> {
if (url) {
const history = this.withUrlHistory(url);
await this.memento.update("urlHistory", history);
}
}
/**
* Get the most recently accessed URLs (oldest to newest) with the provided
* values appended. Duplicates will be removed.
*/
public withUrlHistory(...append: Array<string | undefined>): string[] {
const val = this.memento.get<string[]>("urlHistory");
const urls: Set<string> = Array.isArray(val) ? new Set(val) : new Set();
for (const url of append) {
if (url) {
// It might exist; delete first so it gets appended.
urls.delete(url);
urls.add(url);
}
}
// Slice off the head if the list is too large.
return urls.size > MAX_URLS
? Array.from(urls).slice(urls.size - MAX_URLS, urls.size)
: Array.from(urls);
}
/**
* Mark this as the first connection to a workspace, which influences whether
* the workspace startup confirmation is shown to the user.
*/
public async setFirstConnect(): Promise<void> {
return this.memento.update("firstConnect", true);
}
/**
* Check if this is the first connection to a workspace and clear the flag.
* Used to determine whether to automatically start workspaces without
* prompting the user for confirmation.
*/
public async getAndClearFirstConnect(): Promise<boolean> {
const isFirst = this.memento.get<boolean>("firstConnect");
if (isFirst !== undefined) {
await this.memento.update("firstConnect", undefined);
}
return isFirst === true;
}
/**
* Store a chat ID to open after a window reload.
* Used by the /open deep link handler: it must call
* commands.open() which triggers a remote-authority
* reload, wiping in-memory state. The chat ID is
* persisted here so the extension can pick it up on
* the other side of the reload.
*/
public async setPendingChatId(chatId: string): Promise<void> {
await this.memento.update("pendingChatId", chatId);
}
/**
* Read and clear the pending chat ID. Returns
* undefined if none was stored.
*/
public async getAndClearPendingChatId(): Promise<string | undefined> {
const chatId = this.memento.get<string>("pendingChatId");
if (chatId !== undefined) {
await this.memento.update("pendingChatId", undefined);
}
return chatId;
}
}