CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/sync-client/lib/project-client.ts
Views: 687
1
import type {
2
ProjectClient as Client,
3
ProjectWebsocket,
4
} from "@cocalc/sync/client/types";
5
import connectToProject from "./connect-to-project";
6
import API from "./api";
7
8
export default class ProjectClient implements Client {
9
private websocketCache: { [project_id: string]: ProjectWebsocket } = {};
10
private apiCache: { [project_id: string]: API } = {};
11
12
// TODO [ ]: will prob need to reuseInFlight websocket and api.
13
14
async websocket(project_id: string): Promise<ProjectWebsocket> {
15
if (this.websocketCache[project_id] != null) {
16
return this.websocketCache[project_id];
17
}
18
const w = await connectToProject(project_id);
19
this.websocketCache[project_id] = w;
20
return w;
21
}
22
23
async api(project_id: string): Promise<API> {
24
if (this.apiCache[project_id] != null) {
25
return this.apiCache[project_id];
26
}
27
const conn = await this.websocket(project_id);
28
this.apiCache[project_id] = new API(conn);
29
return this.apiCache[project_id];
30
}
31
}
32
33