Path: blob/master/src/packages/sync/client/conat-sync-client.ts
1447 views
/*1A SyncClient implementation that ONLY requires a valid Conat Client instance to2work. This makes it possible for any two clients connected to a Conat network to use3a document together collaboratively.45Any functionality involving the filesystem obviously is a no-op.67!WORK IN PROGRESS!8*/910import { EventEmitter } from "events";11import { type Client as SyncClient } from "@cocalc/sync/client/types";12import { SyncTable } from "@cocalc/sync/table/synctable";13import { once } from "@cocalc/util/async-utils";14import { FileWatcher } from "@cocalc/sync/editor/string/test/client-test";15import { type Client as ConatClient } from "@cocalc/conat/core/client";16import { getLogger } from "@cocalc/conat/client";17import { parseQueryWithOptions } from "@cocalc/sync/table/util";18import { PubSub } from "@cocalc/conat/sync/pubsub";1920const logger = getLogger("conat-sync-client");2122export class ConatSyncClient extends EventEmitter implements SyncClient {23constructor(private client: ConatClient) {24super();25}2627synctable_conat = async (query0, options?): Promise<SyncTable> => {28const { query } = parseQueryWithOptions(query0, options);29return (await this.client.sync.synctable({30...options,31query,32})) as any;33};3435pubsub_conat = async (opts) => {36return new PubSub({ client: this.client, ...opts });37};3839// account_id or project_id40client_id = (): string => {41return this.client.id;42};4344server_time = (): Date => {45return new Date();46};4748isTestClient = () => {49return false;50};5152is_project = (): boolean => {53return false;54};5556is_browser = (): boolean => {57// most generic -- no filesystem assumption58return true;59};6061is_compute_server = (): boolean => {62return false;63};6465dbg = (f: string): Function => {66return (...args) => logger.debug(f, ...args);67};6869log_error = (_opts): void => {};7071query = (_opts): void => {};7273is_connected = (): boolean => {74return true;75};7677is_signed_in = (): boolean => {78return true;79};8081//82// filesystem stuff that is assumed to be defined but not used...83//84mark_file = (_opts: {85project_id: string;86path: string;87action: string;88ttl: number;89}) => {};9091path_access = (opts: { path: string; mode: string; cb: Function }): void => {92opts.cb(true);93};9495path_exists = (opts: { path: string; cb: Function }): void => {96opts.cb(true);97};98path_stat = (opts: { path: string; cb: Function }): void => {99opts.cb(true);100};101102path_read = async (opts: {103path: string;104maxsize_MB?: number;105cb: Function;106}): Promise<void> => {107opts.cb(true);108};109write_file = async (opts: {110path: string;111data: string;112cb: Function;113}): Promise<void> => {114opts.cb(true);115};116watch_file = (opts: { path: string }): FileWatcher => {117return new FileWatcher(opts.path);118};119120touch_project = (_): void => {};121122query_cancel = (_): void => {};123124alert_message = (_): void => {};125126is_deleted = (_filename: string, _project_id?: string): boolean => {127return false;128};129130set_deleted = (_filename: string, _project_id?: string): void => {};131132synctable_ephemeral = async (133_project_id: string,134query: any,135options: any,136throttle_changes?: number,137): Promise<SyncTable> => {138const s = new SyncTable(query, options, this, throttle_changes);139await once(s, "connected");140return s;141};142143sage_session = (_opts): void => {};144145shell = (_opts): void => {};146}147148149