UNPKG

@cocalc/project

Version:
67 lines 2.56 kB
"use strict"; /* * This file is part of CoCalc: Copyright © 2020 Sagemath, Inc. * License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details */ Object.defineProperty(exports, "__esModule", { value: true }); exports.get_project_info_table = exports.register_project_info_table = void 0; const hof_1 = require("async-await-utils/hof"); const misc_1 = require("@cocalc/util/misc"); const project_info_1 = require("../project-info"); class ProjectInfoTable { constructor(table, logger, project_id) { this.state = "ready"; this.project_id = project_id; this.logger = logger; this.log("register"); this.publish = (0, hof_1.reuseInFlight)(this.publish_impl.bind(this)); this.table = table; this.table.on("closed", () => this.close()); // initializing project info server + reacting when it has something to say this.info_server = (0, project_info_1.get_ProjectInfoServer)(); this.info_server.start(); this.info_server.on("info", this.publish); } async publish_impl(info) { if (this.state == "ready" && this.table.get_state() != "closed") { const next = { project_id: this.project_id, info }; this.table.set(next, "shallow"); try { await this.table.save(); } catch (err) { this.log(`error saving ${err}`); } } else if (this.log != null) { this.log(`ProjectInfoTable state = '${this.state}' and table is '${this.table?.get_state()}'`); } } close() { this.log("close"); this.info_server?.off("info", this.publish); this.table?.close_no_async(); (0, misc_1.close)(this); this.state = "closed"; } log(...args) { if (this.logger == null) return; this.logger.debug("project_info", ...args); } } let project_info_table = undefined; function register_project_info_table(table, logger, project_id) { logger.debug("register_project_info_table"); if (project_info_table != null) { logger.debug("register_project_info_table: cleaning up an already existing one"); project_info_table.close(); } project_info_table = new ProjectInfoTable(table, logger, project_id); } exports.register_project_info_table = register_project_info_table; function get_project_info_table() { return project_info_table; } exports.get_project_info_table = get_project_info_table; //# sourceMappingURL=project-info.js.map