Path: blob/master/src/packages/conat/project/project-info.ts
1452 views
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import type { ProjectInfo } from "@cocalc/util/types/project-info/types";6export type { ProjectInfo };7import { getLogger } from "@cocalc/conat/client";8import { projectSubject } from "@cocalc/conat/names";9import { conat } from "@cocalc/conat/client";1011const SERVICE_NAME = "project-info";12const logger = getLogger("project:project-info");1314interface Api {15get: () => Promise<ProjectInfo | null>;16}1718export async function get({19project_id,20compute_server_id = 0,21}: {22project_id: string;23compute_server_id?: number;24}) {25const c = await conat();26const subject = getSubject({ project_id, compute_server_id });27return await c.call(subject).get();28}2930function getSubject({ project_id, compute_server_id }) {31return projectSubject({32project_id,33compute_server_id,34service: SERVICE_NAME,35});36}3738export function createService(opts: {39infoServer;40project_id: string;41compute_server_id: number;42}) {43return new ProjectInfoService(opts);44}4546class ProjectInfoService {47private infoServer?;48private service?;49private readonly subject: string;50info?: ProjectInfo | null = null;5152constructor({ infoServer, project_id, compute_server_id }) {53logger.debug("register");54this.subject = getSubject({ project_id, compute_server_id });55// initializing project info server + reacting when it has something to say56this.infoServer = infoServer;57this.infoServer.start();58this.infoServer.on("info", this.saveInfo);59this.createService();60}6162private saveInfo = (info) => {63this.info = info;64};6566private createService = async () => {67logger.debug("started project info service ", { subject: this.subject });68const client = await conat();69this.service = await client.service<Api>(this.subject, {70get: async () => this.info ?? null,71});72};7374close = (): void => {75if (this.infoServer == null) {76return;77}78logger.debug("close");79this.infoServer?.removeListener("info", this.saveInfo);80delete this.infoServer;81this.service?.close();82delete this.service;83}84}858687