-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
236 lines (195 loc) · 6.78 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
const Pigeon = require('pigeon');
Pigeon.configure({ strict: false });
const Auto = Pigeon.auto
const ReconnectingWebSocket = require('reconnecting-websocket').default || require('reconnecting-websocket');
const WebSocket = require('isomorphic-ws');
const Rater = require('./rater');
const instances = {};
const isNode = typeof process !== "undefined" &&
process.versions != null &&
process.versions.node != null;
const isTestRun = isNode && process.env.IS_TEST_RUN;
class Transport {
constructor({ _parent, pingTimeoutMs, resolve, reject, url }) {
this.ws = new ReconnectingWebSocket(url, [], { WebSocket });
this.id = Math.random().toString(36).slice(2);
this.rater = new Rater(3);
this.pingTimeoutMs = pingTimeoutMs || 20000;
this.url = url;
this._resolve = resolve;
this._parent = _parent;
this.deltas = [];
this.connectionTimeout = setTimeout(_ => {
reject(`There was a problem initializing syncable [60s timeout]`);
}, 60 * 1000);
this.initialize();
}
initialize() {
this.ws.onopen = () => {
this.emit('connected');
clearInterval(this.pingIv);
this.isOpen = true;
this.pendingChanges = [];
this.applyPendingChanges();
};
this.ws.onerror = error => {
this.emit('error', { error });
console.warn(error);
};
this.ws.onclose = () => {
this.emit('closed');
clearInterval(this.pingIv);
}
this.ws.onmessage = ({ data }) => {
const message = JSON.parse(data);
this.emit('message', { message });
if (message.action == 'init') {
this.setDoc(Auto.load(message.doc));
clearInterval(this.pingIv);
clearTimeout(this.connectionTimeout);
this._resolve(instances[this.url]);
// start pinging after the server has done the work to give us a doc
// and installed its side of the ping handler
this.ping()
this.emit('initialized');
setTimeout(_ => {
clearInterval(this.pingIv);
this.pingIv = setInterval(this.ping.bind(this), 5000);
}, 60 * 1000);
this.pingIv = setInterval(this.ping.bind(this), 1000);
} else if (message.error == 'rejected') {
this.emit('rejected', { message });
} else if (message.action == 'change') {
this.rater.increment();
this.pendingChanges.push(message.data.changes);
} else if (message.serverTime) {
const { serverTime, clientTime } = message;
this.lastPingResponse = Date.now();
const windowSize = 10;
const latency = Date.now() - clientTime;
const delta = clientTime - serverTime + latency / 2;
const { deltas } = this;
deltas.push(delta);
deltas.length > windowSize && deltas.shift();
this.delta = [].concat(deltas).sort((a,b) => a - b)[deltas.length/2|0];
// most recent websocket with timestamps is the winning timestamper
Auto.setTimestamp(this.timestamp.bind(this));
// resolve to syncable() caller after we have a doc and also our clock
// is in good shape
if (this._resolve && this.doc) this._resolve(instances[this.url]);
} else if (message.action == 'snapshot') {
try {
const { crc, ts } = message;
const rewindable = Auto.clone(this.doc);
Auto.rewindChanges(rewindable, ts);
const myCrc = Auto.crc(rewindable);
if (myCrc != crc) {
console.log(`CRC mismatch: ${myCrc} ${crc}`);
// this.ws.reconnect();
}
} catch (e) {
console.log(`snapshot crc error: ${e}`);
}
}
}
this.setDoc(null);
}
getPendingChangesDelay(rate) {
const MAX_DELAY = 2000;
return (
rate === null ? MAX_DELAY :
rate <= 1 ? 100 :
rate <= 5 ? 500 :
rate <= 10 ? 1000 : MAX_DELAY
);
}
applyPendingChanges(directCall) {
if (this.pendingChanges.length) {
const catonatedDiffs = this.pendingChanges
.sort((a, b) => a.ts - b.ts)
.map(c => c.diff).flat()
const changes = Object.assign(this.pendingChanges[0], { diff: catonatedDiffs });
for (const k in this.doc) this.doc[k] = _clone(this.doc[k]);
this.setDoc(Auto.applyChangesInPlace(Auto.alias(this.doc), changes));
this.emit('changed', { changes })
}
this.pendingChanges = [];
const rate = this.rater.rate();
const delay = this.getPendingChangesDelay(rate);
if (directCall) return;
setTimeout(_ => this.applyPendingChanges(), delay);
}
ping() {
// if we're backgrounded, last ping response may be bogus
if (typeof document != 'undefined' && document.visibilityState == 'hidden') {
this.lastPingResponse = false;
}
if (this.lastPingResponse && ((Date.now() - this.lastPingResponse) > this.pingTimeoutMs)) {
console.log('took too long for ping response');
this.lastPingResponse = false;
this.emit('reconnecting');
this.ws.reconnect();
} else {
this.ws.send(JSON.stringify({ action: 'time', data: Date.now() }));
}
}
timestamp() {
return Math.floor(Date.now() - this.delta);
}
emit() {
this._parent.emit(...arguments);
}
setDoc(doc) {
this.doc = doc;
Object.assign(this._parent, doc);
for (const prop of Object.keys(this._parent)) {
if (!(prop in doc)) {
delete this._parent[prop];
}
}
}
}
class Syncable extends EventTarget {
constructor(options) {
super();
Object.defineProperty(this, '_transport', { value: new Transport({ ...options, _parent: this}) });
instances[options.url] = this;
}
sync(fn) {
const transport = this._transport;
if (transport.ws.readyState === 3) {
console.warn("can't sync before open");
transport.ws.reconnect();
this.emit('reconnecting');
} else if (transport.ws.readyState !== 1) {
console.warn("websocket is not open, queueing messages");
}
const directCall = true;
transport.applyPendingChanges(directCall);
const newDoc = Auto.change(this._transport.doc, fn);
const changes = Auto.getChanges(this._transport.doc, newDoc);
if (!changes.diff.length) {
return null;
}
transport.setDoc(newDoc);
this.emit('changed', { changes });
transport.ws.send(JSON.stringify({ action: 'change', data: { changes } }));
}
on(eventName, handler) {
this.addEventListener(eventName, e => handler(e.detail));
}
emit(eventName, data) {
this.dispatchEvent(new CustomEvent(eventName, { detail: data }));
}
}
async function syncable({ url, pingTimeoutMs, noCache }) {
return new Promise((resolve, reject) => {
if (!instances[url] || noCache) {
new Syncable({url, pingTimeoutMs, resolve, reject});
} else {
resolve(instances[url]);
}
});
}
const _clone = x => JSON.parse(JSON.stringify(x));
module.exports = syncable;