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/client/sync-client.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45/*6Functionality related to Sync.7*/89import { callback2 } from "@cocalc/util/async-utils";10import { once } from "@cocalc/util/async-utils";11import {12defaults,13is_valid_uuid_string,14merge,15required,16} from "@cocalc/util/misc";17import { SyncDoc, SyncOpts0 } from "@cocalc/sync/editor/generic/sync-doc";18import { SyncDB, SyncDBOpts0 } from "@cocalc/sync/editor/db";19import { SyncString } from "@cocalc/sync/editor/string/sync";20import {21synctable,22SyncTable,23Query,24QueryOptions,25synctable_no_changefeed,26} from "@cocalc/sync/table";27import synctable_project from "./synctable-project";28import type { Channel, AppClient } from "./types";2930interface SyncOpts extends Omit<SyncOpts0, "client"> {}3132interface SyncDBOpts extends Omit<SyncDBOpts0, "client" | "string_cols"> {33string_cols?: string[];34}3536export class SyncClient {37private client: AppClient;3839constructor(client: AppClient) {40this.client = client;41}4243public sync_table(44query: Query,45options: QueryOptions,46throttle_changes?: number,47): SyncTable {48return synctable(query, options, this.client, throttle_changes);49}5051public async synctable_database(52query: Query,53options: QueryOptions,54throttle_changes?: number,55): Promise<SyncTable> {56const s = this.sync_table(query, options, throttle_changes);57await once(s, "connected");58return s;59}6061public synctable_no_changefeed(62query: Query,63options: QueryOptions,64throttle_changes?: number,65): SyncTable {66return synctable_no_changefeed(67query,68options,69this.client,70throttle_changes,71);72}7374public async synctable_project(75project_id: string,76query: Query,77options: QueryOptions,78throttle_changes: number | undefined = undefined,79id: string = "",80): Promise<SyncTable> {81return await synctable_project({82project_id,83query,84options,85client: this.client,86throttle_changes,87id,88});89}9091// NOT currently used.92public async symmetric_channel(93name: string,94project_id: string,95): Promise<Channel> {96if (!is_valid_uuid_string(project_id) || typeof name !== "string") {97throw Error("project_id must be a valid uuid and name must be a string");98}99return (await this.client.project_client.api(project_id)).symmetric_channel(100name,101);102}103104public sync_string(opts: SyncOpts): SyncString {105const opts0: SyncOpts0 = defaults(opts, {106id: undefined,107project_id: required,108path: required,109file_use_interval: "default",110cursors: false,111patch_interval: 1000,112save_interval: 2000,113persistent: false,114data_server: undefined,115client: this.client,116ephemeral: false,117});118return new SyncString(opts0);119}120121public sync_db(opts: SyncDBOpts): SyncDoc {122const opts0: SyncDBOpts0 = defaults(opts, {123id: undefined,124project_id: required,125path: required,126file_use_interval: "default",127cursors: false,128patch_interval: 1000,129save_interval: 2000,130change_throttle: undefined,131persistent: false,132data_server: undefined,133134primary_keys: required,135string_cols: [],136137client: this.client,138139ephemeral: false,140});141return new SyncDB(opts0);142}143144public async open_existing_sync_document(opts: {145project_id: string;146path: string;147data_server?: string;148persistent?: boolean;149}): Promise<SyncDoc | undefined> {150const resp = await callback2(this.client.query, {151query: {152syncstrings: {153project_id: opts.project_id,154path: opts.path,155doctype: null,156},157},158});159if (resp.event === "error") {160throw Error(resp.error);161}162if (resp.query?.syncstrings == null) {163throw Error(`no document '${opts.path}' in project '${opts.project_id}'`);164}165const doctype = JSON.parse(166resp.query.syncstrings.doctype ?? '{"type":"string"}',167);168let opts2: any = {169project_id: opts.project_id,170path: opts.path,171};172if (opts.data_server) {173opts2.data_server = opts.data_server;174}175if (opts.persistent) {176opts2.persistent = opts.persistent;177}178if (doctype.opts != null) {179opts2 = merge(opts2, doctype.opts);180}181const f = `sync_${doctype.type}`;182return (this as any)[f](opts2);183}184}185186187