Path: blob/master/src/packages/sync/table/synctable-no-changefeed.ts
1447 views
/*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(79project_id: string,80compute_server_id?: number,81): Promise<void> {82await this.client.touch_project(project_id, compute_server_id);83}8485public is_connected(): boolean {86return this.client.is_connected();87}8889public is_signed_in(): boolean {90return this.client.is_signed_in();91}9293public server_time(): Date {94return this.client.server_time();95}9697public dbg(s: string): Function {98return this.client.dbg(s);99}100101public query(opts): void {102if (opts.changes) {103this.changefeed_query(opts);104} else {105this.client.query(opts);106}107}108109private changefeed_query(opts): void {110opts.changes = false;111this.client.query(opts);112}113114public query_cancel(_): void {115// no op since no changefeed.116this.client.removeListener("connected", this.emit_connected);117this.client.removeListener("signed_in", this.emit_signed_in);118}119120public alert_message(opts): void {121if (this.client.alert_message != null) {122this.client.alert_message(opts);123}124}125126is_deleted = (_path: string, _project_id: string) => {127// not implemented yet in general128return undefined;129};130}131132133