|
| 1 | +/* |
| 2 | + * This file is part of CoCalc: Copyright © 2020 Sagemath, Inc. |
| 3 | + * License: MS-RSL – see LICENSE.md for details |
| 4 | + */ |
| 5 | + |
| 6 | +import { EventEmitter } from "events"; |
| 7 | +import { bind_methods, keys } from "@cocalc/util/misc"; |
| 8 | +import { |
| 9 | + Client as Client0, |
| 10 | + FileWatcher as FileWatcher0, |
| 11 | +} from "@cocalc/sync/editor/generic/types"; |
| 12 | +import { SyncTable } from "@cocalc/sync/table/synctable"; |
| 13 | +import { ExecuteCodeOptionsWithCallback } from "@cocalc/util/types/execute-code"; |
| 14 | +import { once } from "@cocalc/util/async-utils"; |
| 15 | + |
| 16 | +export class FileWatcher extends EventEmitter implements FileWatcher0 { |
| 17 | + private path: string; |
| 18 | + constructor(path: string) { |
| 19 | + super(); |
| 20 | + this.path = path; |
| 21 | + console.log("FileWatcher", this.path); |
| 22 | + } |
| 23 | + public close(): void {} |
| 24 | +} |
| 25 | + |
| 26 | +export class Client extends EventEmitter implements Client0 { |
| 27 | + private _client_id: string; |
| 28 | + private initial_get_query: { [table: string]: any[] }; |
| 29 | + public set_queries: any[] = []; |
| 30 | + |
| 31 | + constructor( |
| 32 | + initial_get_query: { [table: string]: any[] }, |
| 33 | + client_id: string, |
| 34 | + ) { |
| 35 | + super(); |
| 36 | + this._client_id = client_id; |
| 37 | + this.initial_get_query = initial_get_query; |
| 38 | + bind_methods(this, ["query", "dbg", "query_cancel"]); |
| 39 | + } |
| 40 | + |
| 41 | + public server_time(): Date { |
| 42 | + return new Date(); |
| 43 | + } |
| 44 | + |
| 45 | + isTestClient = () => { |
| 46 | + return true; |
| 47 | + }; |
| 48 | + |
| 49 | + public is_project(): boolean { |
| 50 | + return false; |
| 51 | + } |
| 52 | + |
| 53 | + public is_browser(): boolean { |
| 54 | + return true; |
| 55 | + } |
| 56 | + |
| 57 | + public is_compute_server(): boolean { |
| 58 | + return false; |
| 59 | + } |
| 60 | + |
| 61 | + public dbg(_f: string): Function { |
| 62 | + // return (...args) => { |
| 63 | + // console.log(_f, ...args); |
| 64 | + // }; |
| 65 | + return (..._) => {}; |
| 66 | + } |
| 67 | + |
| 68 | + public mark_file(_opts: { |
| 69 | + project_id: string; |
| 70 | + path: string; |
| 71 | + action: string; |
| 72 | + ttl: number; |
| 73 | + }): void { |
| 74 | + //console.log("mark_file", opts); |
| 75 | + } |
| 76 | + |
| 77 | + public log_error(opts: { |
| 78 | + project_id: string; |
| 79 | + path: string; |
| 80 | + string_id: string; |
| 81 | + error: any; |
| 82 | + }): void { |
| 83 | + console.log("log_error", opts); |
| 84 | + } |
| 85 | + |
| 86 | + public query(opts): void { |
| 87 | + if (opts.options && opts.options.length === 1 && opts.options[0].set) { |
| 88 | + // set query |
| 89 | + this.set_queries.push(opts); |
| 90 | + opts.cb(); |
| 91 | + } else { |
| 92 | + // get query -- returns predetermined result |
| 93 | + const table = keys(opts.query)[0]; |
| 94 | + let result = this.initial_get_query[table]; |
| 95 | + if (result == null) { |
| 96 | + result = []; |
| 97 | + } |
| 98 | + //console.log("GET QUERY ", table, result); |
| 99 | + opts.cb(undefined, { query: { [table]: result } }); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + path_access(opts: { path: string; mode: string; cb: Function }): void { |
| 104 | + console.log("path_access", opts.path, opts.mode); |
| 105 | + opts.cb(true); |
| 106 | + } |
| 107 | + path_exists(opts: { path: string; cb: Function }): void { |
| 108 | + console.log("path_access", opts.path); |
| 109 | + opts.cb(true); |
| 110 | + } |
| 111 | + path_stat(opts: { path: string; cb: Function }): void { |
| 112 | + console.log("path_state", opts.path); |
| 113 | + opts.cb(true); |
| 114 | + } |
| 115 | + async path_read(opts: { |
| 116 | + path: string; |
| 117 | + maxsize_MB?: number; |
| 118 | + cb: Function; |
| 119 | + }): Promise<void> { |
| 120 | + console.log("path_ready", opts.path); |
| 121 | + opts.cb(true); |
| 122 | + } |
| 123 | + async write_file(opts: { |
| 124 | + path: string; |
| 125 | + data: string; |
| 126 | + cb: Function; |
| 127 | + }): Promise<void> { |
| 128 | + console.log("write_file", opts.path, opts.data); |
| 129 | + opts.cb(true); |
| 130 | + } |
| 131 | + watch_file(opts: { path: string }): FileWatcher { |
| 132 | + return new FileWatcher(opts.path); |
| 133 | + } |
| 134 | + |
| 135 | + public is_connected(): boolean { |
| 136 | + return true; |
| 137 | + } |
| 138 | + |
| 139 | + public is_signed_in(): boolean { |
| 140 | + return true; |
| 141 | + } |
| 142 | + |
| 143 | + public touch_project(_): void {} |
| 144 | + |
| 145 | + public query_cancel(_): void {} |
| 146 | + |
| 147 | + public alert_message(_): void {} |
| 148 | + |
| 149 | + public is_deleted(_filename: string, _project_id?: string): boolean { |
| 150 | + return false; |
| 151 | + } |
| 152 | + |
| 153 | + public set_deleted(_filename: string, _project_id?: string): void {} |
| 154 | + |
| 155 | + async synctable_ephemeral( |
| 156 | + _project_id: string, |
| 157 | + query: any, |
| 158 | + options: any, |
| 159 | + throttle_changes?: number, |
| 160 | + ): Promise<SyncTable> { |
| 161 | + const s = new SyncTable(query, options, this, throttle_changes); |
| 162 | + await once(s, "connected"); |
| 163 | + return s; |
| 164 | + } |
| 165 | + |
| 166 | + async synctable_conat(_query: any): Promise<SyncTable> { |
| 167 | + throw Error("synctable_conat: not implemented"); |
| 168 | + } |
| 169 | + async pubsub_conat(_query: any): Promise<SyncTable> { |
| 170 | + throw Error("pubsub_conat: not implemented"); |
| 171 | + } |
| 172 | + |
| 173 | + // account_id or project_id |
| 174 | + public client_id(): string { |
| 175 | + return this._client_id; |
| 176 | + } |
| 177 | + |
| 178 | + public sage_session({ path }): void { |
| 179 | + console.log(`sage_session: path=${path}`); |
| 180 | + } |
| 181 | + |
| 182 | + public shell(opts: ExecuteCodeOptionsWithCallback): void { |
| 183 | + console.log(`shell: opts=${JSON.stringify(opts)}`); |
| 184 | + } |
| 185 | +} |
0 commit comments