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/frontend/app-framework/Table.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { AppRedux } from "../app-framework";6import { bind_methods } from "@cocalc/util/misc";7import { webapp_client } from "../webapp-client";89declare let Primus;1011export type TableConstructor<T extends Table> = new (name, redux) => T;1213export abstract class Table {14public name: string;15public _table: any;16protected redux: AppRedux;1718// override in derived class to pass in options to the query -- these only impact initial query, not changefeed!19options(): any[] {20return [];21}2223abstract query(): void;2425protected abstract _change(table: any, keys: string[]): void;2627protected no_changefeed(): boolean {28return false;29}3031constructor(name, redux) {32bind_methods(this);33this.name = name;34this.redux = redux;35if (this.no_changefeed()) {36// Create the table but with no changefeed.37this._table = webapp_client.sync_client.synctable_no_changefeed(38this.query(),39this.options(),40);41} else {42// Set up a changefeed43this._table = webapp_client.sync_client.sync_table(44this.query(),45this.options(),46);47}4849this._table.on("error", (error) => {50console.warn(`Synctable error (table='${name}'): ${error}`);51});5253if (this._change != null) {54// Call the _change method whenever there is a change.55this._table.on("change", (keys) => {56this._change(this._table, keys);57});58}59}6061close = () => {62this._table.close();63};6465set = async (66changes: object,67merge?: "deep" | "shallow" | "none", // The actual default is "deep" (see @cocalc/sync/table/synctable.ts)68cb?: (error?: string) => void,69): Promise<void> => {70if (cb == null) {71// No callback, so let async/await report errors.72// Do not let the error silently hide (like using the code below did)!!73// We were missing lot of bugs because of this...74this._table.set(changes, merge);75await this._table.save();76return;77}7879// callback is defined still.80let e: undefined | string = undefined;81try {82this._table.set(changes, merge);83await this._table.save();84} catch (err) {85e = err.toString();86}87cb(e);88};89}909192