-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
workspace-model.ts
146 lines (128 loc) · 5 KB
/
workspace-model.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
* Copyright (c) 2021 Gitpod GmbH. All rights reserved.
* Licensed under the GNU Affero General Public License (AGPL).
* See License-AGPL.txt in the project root for license information.
*/
import { Disposable, DisposableCollection, GitpodClient, WorkspaceInfo, WorkspaceInstance } from "@gitpod/gitpod-protocol";
import { getGitpodService } from "../service/service";
export class WorkspaceModel implements Disposable, Partial<GitpodClient> {
protected workspaces = new Map<string,WorkspaceInfo>();
protected currentlyFetching = new Set<string>();
protected disposables = new DisposableCollection();
protected internalLimit = 50;
get limit(): number {
return this.internalLimit;
}
set limit(limit: number) {
this.internalLimit = limit;
this.internalRefetch();
}
constructor(protected setWorkspaces: (ws: WorkspaceInfo[]) => void) {
this.internalRefetch();
}
protected internalRefetch() {
this.disposables.dispose();
this.disposables = new DisposableCollection();
getGitpodService().server.getWorkspaces({
limit: this.internalLimit
}).then( infos => {
this.updateMap(infos);
// Additional fetch pinned workspaces
// see also: https://github.com/gitpod-io/gitpod/issues/4488
getGitpodService().server.getWorkspaces({
limit: this.internalLimit,
pinnedOnly: true,
}).then(infos => {
this.updateMap(infos);
this.notifyWorkpaces();
});
});
this.disposables.push(getGitpodService().registerClient(this));
}
protected updateMap(workspaces: WorkspaceInfo[]) {
for (const ws of workspaces) {
this.workspaces.set(ws.workspace.id, ws);
}
}
dispose(): void {
this.disposables.dispose();
}
async onInstanceUpdate(instance: WorkspaceInstance) {
if (this.workspaces) {
if (this.workspaces.has(instance.workspaceId)) {
const info = this.workspaces.get(instance.workspaceId)!;
info.latestInstance = instance;
if (info.workspace !== undefined) {
this.notifyWorkpaces();
}
} else if (!this.currentlyFetching.has(instance.workspaceId)) {
try {
this.currentlyFetching.add(instance.workspaceId);
const info = await getGitpodService().server.getWorkspace(instance.workspaceId);
this.workspaces.set(instance.workspaceId, info);
this.notifyWorkpaces();
} finally {
this.currentlyFetching.delete(instance.workspaceId);
}
}
}
}
async deleteWorkspace(id: string): Promise<void> {
await getGitpodService().server.deleteWorkspace(id);
this.workspaces.delete(id);
this.notifyWorkpaces();
}
protected internalActive = true;
get active() {
return this.internalActive;
}
set active(active: boolean) {
if (active !== this.internalActive) {
this.internalActive = active;
this.notifyWorkpaces();
}
}
searchTerm: string | undefined;
setSearch(searchTerm: string) {
if (searchTerm !== this.searchTerm) {
this.searchTerm = searchTerm;
this.notifyWorkpaces();
}
}
async togglePinned(workspaceId: string) {
const ws = this.workspaces.get(workspaceId)?.workspace;
if (ws) {
ws.pinned = !ws.pinned;
await getGitpodService().server.updateWorkspaceUserPin(ws.id, 'toggle');
this.notifyWorkpaces();
}
}
async toggleShared(workspaceId: string) {
const ws = this.workspaces.get(workspaceId)?.workspace;
if (ws) {
ws.shareable = !ws.shareable;
await getGitpodService().server.controlAdmission(ws.id, ws.shareable ? "everyone" : "owner");
this.notifyWorkpaces();
}
}
protected notifyWorkpaces(): void {
let infos = Array.from(this.workspaces.values());
infos = infos.filter(ws => !this.active || this.isActive(ws));
if (this.searchTerm) {
infos = infos.filter(ws => (ws.workspace.description+ws.workspace.id+ws.workspace.contextURL+ws.workspace.context).toLowerCase().indexOf(this.searchTerm!.toLowerCase()) !== -1);
}
infos = infos.sort((a,b) => {
return WorkspaceInfo.lastActiveISODate(b).localeCompare(WorkspaceInfo.lastActiveISODate(a));
});
this.setWorkspaces(infos.slice(0, this.internalLimit));
}
protected isActive(info: WorkspaceInfo): boolean {
return (
info.workspace.pinned ||
(!!info.latestInstance && info.latestInstance.status?.phase !== 'stopped')
) && !info.workspace.softDeleted;
}
public getAllFetchedWorkspaces(): Map<string, WorkspaceInfo> {
return this.workspaces;
}
}