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/sync/table/synctable-no-database.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45/*6Make a SyncTable which does not use a changefeed or the central7database at all.89The initial read waits on the client calling a function to provide10the initial data, and all changes are also injected by explicitly11calling a function. An event is emitted when a new change is made12that has to get saved.1314This is used to implement the browser side of project specific15SyncTables. It's also obviously useful for unit testing.16*/1718import { EventEmitter } from "events";19import { SyncTable, Client } from "./synctable";20import { bind_methods, keys } from "@cocalc/util/misc";2122export function synctable_no_database(23query,24options,25client: Client,26throttle_changes: undefined | number = undefined,27initial_get_query: any[] = [],28project_id?: string,29): SyncTable {30if (options == null) {31options = [];32}33const client2 = new ClientNoDatabase(client, initial_get_query);34return new SyncTable(35query,36options,37client2,38throttle_changes,39true,40true,41project_id,42);43}4445class ClientNoDatabase extends EventEmitter {46private client: Client;47private initial_get_query: any[];48private connected: boolean = true;4950constructor(client, initial_get_query) {51super();5253this.initial_get_query = initial_get_query;54bind_methods(this, ["query", "dbg", "query_cancel"]);55this.client = client;56}5758public set_connected(connected: boolean): void {59const event = connected && this.connected != connected;60this.connected = connected;61if (event) {62this.emit("signed_in");63this.emit("connected");64}65}6667public is_project(): boolean {68return this.client.is_project();69}7071public is_browser(): boolean {72return this.client.is_browser();73}7475public is_compute_server(): boolean {76return this.client.is_compute_server();77}7879public async touch_project(project_id: string): Promise<void> {80await this.client.touch_project(project_id);81}8283public is_connected(): boolean {84return this.connected;85}8687public is_signed_in(): boolean {88return this.connected;89}9091public server_time(): Date {92return this.client.server_time();93}9495public dbg(s: string): Function {96return this.client.dbg(s);97}9899public query(opts): void {100if (opts.options && opts.options.length === 1 && opts.options[0].set) {101if (this.connected) {102// set query -- totally ignore.103opts.cb();104} else {105opts.cb("disconnected");106}107} else {108// get query -- returns predetermined result (default: empty)109const table = keys(opts.query)[0];110opts.cb(undefined, { query: { [table]: this.initial_get_query } });111}112}113114public query_cancel(_): void {}115116public alert_message(opts): void {117if (this.client.alert_message != null) {118this.client.alert_message(opts);119}120}121122is_deleted = (_path: string, _project_id: string) => {123// not implemented yet in general124return undefined;125};126}127128129