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/editor/string/test/client-test.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45/*6Minimal client class that we use for testing.7*/89import { EventEmitter } from "events";10import { bind_methods, keys } from "@cocalc/util/misc";11import { once } from "@cocalc/util/async-utils";12import {13Client as Client0,14FileWatcher as FileWatcher0,15} from "../../generic/types";16import { SyncTable } from "@cocalc/sync/table/synctable";17import { ExecuteCodeOptionsWithCallback } from "@cocalc/util/types/execute-code";1819export class FileWatcher extends EventEmitter implements FileWatcher0 {20private path: string;21constructor(path: string) {22super();23this.path = path;24console.log("FileWatcher", this.path);25}26public close(): void {}27}2829export class Client extends EventEmitter implements Client0 {30private _client_id: string;31private initial_get_query: { [table: string]: any[] };32public set_queries: any[] = [];3334constructor(35initial_get_query: { [table: string]: any[] },36client_id: string,37) {38super();39this._client_id = client_id;40this.initial_get_query = initial_get_query;41bind_methods(this, ["query", "dbg", "query_cancel"]);42}4344public server_time(): Date {45return new Date();46}4748public is_project(): boolean {49return false;50}5152public is_browser(): boolean {53return true;54}5556public is_compute_server(): boolean {57return false;58}5960public dbg(_f: string): Function {61// return (...args) => {62// console.log(_f, ...args);63// };64return (..._) => {};65}6667public mark_file(_opts: {68project_id: string;69path: string;70action: string;71ttl: number;72}): void {73//console.log("mark_file", opts);74}7576public log_error(opts: {77project_id: string;78path: string;79string_id: string;80error: any;81}): void {82console.log("log_error", opts);83}8485public query(opts): void {86if (opts.options && opts.options.length === 1 && opts.options[0].set) {87// set query88this.set_queries.push(opts);89opts.cb();90} else {91// get query -- returns predetermined result92const table = keys(opts.query)[0];93let result = this.initial_get_query[table];94if (result == null) {95result = [];96}97//console.log("GET QUERY ", table, result);98opts.cb(undefined, { query: { [table]: result } });99}100}101102path_access(opts: { path: string; mode: string; cb: Function }): void {103console.log("path_access", opts.path, opts.mode);104opts.cb(true);105}106path_exists(opts: { path: string; cb: Function }): void {107console.log("path_access", opts.path);108opts.cb(true);109}110path_stat(opts: { path: string; cb: Function }): void {111console.log("path_state", opts.path);112opts.cb(true);113}114async path_read(opts: {115path: string;116maxsize_MB?: number;117cb: Function;118}): Promise<void> {119console.log("path_ready", opts.path);120opts.cb(true);121}122async write_file(opts: {123path: string;124data: string;125cb: Function;126}): Promise<void> {127console.log("write_file", opts.path, opts.data);128opts.cb(true);129}130watch_file(opts: { path: string }): FileWatcher {131return new FileWatcher(opts.path);132}133134public is_connected(): boolean {135return true;136}137138public is_signed_in(): boolean {139return true;140}141142public touch_project(_): void {}143144public query_cancel(_): void {}145146public alert_message(_): void {}147148public is_deleted(_filename: string, _project_id?: string): boolean {149return false;150}151152public set_deleted(_filename: string, _project_id?: string): void {}153154async synctable_project(155_project_id: string,156query: any,157options: any,158throttle_changes?: number,159): Promise<SyncTable> {160const s = new SyncTable(query, options, this, throttle_changes);161await once(s, "connected");162return s;163}164165async synctable_database(166_query: any,167_options: any,168_throttle_changes?: number,169): Promise<SyncTable> {170throw Error("not implemented");171}172173// account_id or project_id174public client_id(): string {175return this._client_id;176}177178public sage_session({ path }): void {179console.log(`sage_session: path=${path}`);180}181182public shell(opts: ExecuteCodeOptionsWithCallback): void {183console.log(`shell: opts=${JSON.stringify(opts)}`);184}185}186187188