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-changefeed.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 at all.78It does the initial database read as usual, and also9writes changes as usual, but does not use a changefeed10at all. Instead changes are injected by calling11a function.1213This is used, e.g., by a backend project for implementing a version14of SyncTable, where the project itself handles all changes,15not the database or hubs. However, data is still persisted16to the central database.1718Returned object is not cached in any way.19*/2021import { EventEmitter } from "events";22import { SyncTable, Client } from "./synctable";23import { bind_methods } from "@cocalc/util/misc";2425export function synctable_no_changefeed(26query,27options,28client: Client,29throttle_changes?: undefined | number,30): SyncTable {31if (options == null) {32options = [];33}34const client2 = new ClientNoChangefeed(client);35return new SyncTable(query, options, client2, throttle_changes, true, false);36}3738class ClientNoChangefeed extends EventEmitter {39private client: Client;4041constructor(client) {42super();4344bind_methods(this, [45"query",46"dbg",47"query_cancel",48"emit_connected",49"emit_signed_in",50]);51this.client = client;5253// These MUST be after the binds above, obviously.54client.on("connected", this.emit_connected);55client.on("signed_in", this.emit_signed_in);56}5758private emit_connected(): void {59this.emit("connected");60}6162private emit_signed_in(): void {63this.emit("signed_in");64}6566public is_project(): boolean {67return this.client.is_project();68}6970public is_browser(): boolean {71return this.client.is_browser();72}7374public is_compute_server(): boolean {75return this.client.is_compute_server();76}7778public async touch_project(project_id: string): Promise<void> {79await this.client.touch_project(project_id);80}8182public is_connected(): boolean {83return this.client.is_connected();84}8586public is_signed_in(): boolean {87return this.client.is_signed_in();88}8990public server_time(): Date {91return this.client.server_time();92}9394public dbg(s: string): Function {95return this.client.dbg(s);96}9798public query(opts): void {99if (opts.changes) {100this.changefeed_query(opts);101} else {102this.client.query(opts);103}104}105106private changefeed_query(opts): void {107opts.changes = false;108this.client.query(opts);109}110111public query_cancel(_): void {112// no op since no changefeed.113this.client.removeListener("connected", this.emit_connected);114this.client.removeListener("signed_in", this.emit_signed_in);115}116117public alert_message(opts): void {118if (this.client.alert_message != null) {119this.client.alert_message(opts);120}121}122123is_deleted = (_path: string, _project_id: string) => {124// not implemented yet in general125return undefined;126};127}128129130