Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/project/sync/project-info.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { reuseInFlight } from "@cocalc/util/reuse-in-flight";6import { close } from "@cocalc/util/misc";7import { SyncTable } from "@cocalc/sync/table";8import { get_ProjectInfoServer } from "../project-info";9import { ProjectInfo } from "@cocalc/util/types/project-info/types";10import { ProjectInfoServer } from "../project-info";1112class ProjectInfoTable {13private table: SyncTable;14private logger: { debug: Function };15private project_id: string;16private state: "ready" | "closed" = "ready";17private readonly publish: (info: ProjectInfo) => Promise<void>;18private readonly info_server: ProjectInfoServer;1920constructor(21table: SyncTable,22logger: { debug: Function },23project_id: string24) {25this.project_id = project_id;26this.logger = logger;27this.log("register");28this.publish = reuseInFlight(this.publish_impl.bind(this));29this.table = table;30this.table.on("closed", () => this.close());31// initializing project info server + reacting when it has something to say32this.info_server = get_ProjectInfoServer();33this.info_server.start();34this.info_server.on("info", this.publish);35}3637private async publish_impl(info: ProjectInfo): Promise<void> {38if (this.state == "ready" && this.table.get_state() != "closed") {39const next = { project_id: this.project_id, info };40this.table.set(next, "shallow");41try {42await this.table.save();43} catch (err) {44this.log(`error saving ${err}`);45}46} else if (this.log != null) {47this.log(48`ProjectInfoTable state = '${49this.state50}' and table is '${this.table?.get_state()}'`51);52}53}5455public close(): void {56this.log("close");57this.info_server?.off("info", this.publish);58this.table?.close_no_async();59close(this);60this.state = "closed";61}6263private log(...args): void {64if (this.logger == null) return;65this.logger.debug("project_info", ...args);66}67}6869let project_info_table: ProjectInfoTable | undefined = undefined;7071export function register_project_info_table(72table: SyncTable,73logger: any,74project_id: string75): void {76logger.debug("register_project_info_table");77if (project_info_table != null) {78logger.debug(79"register_project_info_table: cleaning up an already existing one"80);81project_info_table.close();82}83project_info_table = new ProjectInfoTable(table, logger, project_id);84}8586export function get_project_info_table(): ProjectInfoTable | undefined {87return project_info_table;88}899091