Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/sync/client/conat-sync-client.ts
1447 views
1
/*
2
A SyncClient implementation that ONLY requires a valid Conat Client instance to
3
work. This makes it possible for any two clients connected to a Conat network to use
4
a document together collaboratively.
5
6
Any functionality involving the filesystem obviously is a no-op.
7
8
!WORK IN PROGRESS!
9
*/
10
11
import { EventEmitter } from "events";
12
import { type Client as SyncClient } from "@cocalc/sync/client/types";
13
import { SyncTable } from "@cocalc/sync/table/synctable";
14
import { once } from "@cocalc/util/async-utils";
15
import { FileWatcher } from "@cocalc/sync/editor/string/test/client-test";
16
import { type Client as ConatClient } from "@cocalc/conat/core/client";
17
import { getLogger } from "@cocalc/conat/client";
18
import { parseQueryWithOptions } from "@cocalc/sync/table/util";
19
import { PubSub } from "@cocalc/conat/sync/pubsub";
20
21
const logger = getLogger("conat-sync-client");
22
23
export class ConatSyncClient extends EventEmitter implements SyncClient {
24
constructor(private client: ConatClient) {
25
super();
26
}
27
28
synctable_conat = async (query0, options?): Promise<SyncTable> => {
29
const { query } = parseQueryWithOptions(query0, options);
30
return (await this.client.sync.synctable({
31
...options,
32
query,
33
})) as any;
34
};
35
36
pubsub_conat = async (opts) => {
37
return new PubSub({ client: this.client, ...opts });
38
};
39
40
// account_id or project_id
41
client_id = (): string => {
42
return this.client.id;
43
};
44
45
server_time = (): Date => {
46
return new Date();
47
};
48
49
isTestClient = () => {
50
return false;
51
};
52
53
is_project = (): boolean => {
54
return false;
55
};
56
57
is_browser = (): boolean => {
58
// most generic -- no filesystem assumption
59
return true;
60
};
61
62
is_compute_server = (): boolean => {
63
return false;
64
};
65
66
dbg = (f: string): Function => {
67
return (...args) => logger.debug(f, ...args);
68
};
69
70
log_error = (_opts): void => {};
71
72
query = (_opts): void => {};
73
74
is_connected = (): boolean => {
75
return true;
76
};
77
78
is_signed_in = (): boolean => {
79
return true;
80
};
81
82
//
83
// filesystem stuff that is assumed to be defined but not used...
84
//
85
mark_file = (_opts: {
86
project_id: string;
87
path: string;
88
action: string;
89
ttl: number;
90
}) => {};
91
92
path_access = (opts: { path: string; mode: string; cb: Function }): void => {
93
opts.cb(true);
94
};
95
96
path_exists = (opts: { path: string; cb: Function }): void => {
97
opts.cb(true);
98
};
99
path_stat = (opts: { path: string; cb: Function }): void => {
100
opts.cb(true);
101
};
102
103
path_read = async (opts: {
104
path: string;
105
maxsize_MB?: number;
106
cb: Function;
107
}): Promise<void> => {
108
opts.cb(true);
109
};
110
write_file = async (opts: {
111
path: string;
112
data: string;
113
cb: Function;
114
}): Promise<void> => {
115
opts.cb(true);
116
};
117
watch_file = (opts: { path: string }): FileWatcher => {
118
return new FileWatcher(opts.path);
119
};
120
121
touch_project = (_): void => {};
122
123
query_cancel = (_): void => {};
124
125
alert_message = (_): void => {};
126
127
is_deleted = (_filename: string, _project_id?: string): boolean => {
128
return false;
129
};
130
131
set_deleted = (_filename: string, _project_id?: string): void => {};
132
133
synctable_ephemeral = async (
134
_project_id: string,
135
query: any,
136
options: any,
137
throttle_changes?: number,
138
): Promise<SyncTable> => {
139
const s = new SyncTable(query, options, this, throttle_changes);
140
await once(s, "connected");
141
return s;
142
};
143
144
sage_session = (_opts): void => {};
145
146
shell = (_opts): void => {};
147
}
148
149