forked from galaxian85/CRDT-folder-poc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
585 lines (544 loc) · 17 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
// copy of https://github.com/hackmdio/y-socketio-redis/blob/main/src/y-socket-io/client.js
import * as Y from 'yjs'
import * as bc from 'lib0/broadcastchannel'
import * as AwarenessProtocol from 'y-protocols/awareness'
import { Observable } from 'lib0/observable'
import { io } from 'socket.io-client'
/**
* @typedef {import('socket.io-client').Socket} ClientSocket
*/
/**
* @typedef ProviderConfiguration
* SocketIOProvider instance configuration. Here you can configure:
* - autoConnect: (Optional) Will try to connect to the server when the instance is created if true; otherwise you have to call `provider.connect()` manually
* - awareness: (Optional) Give an existing awareness
* - resyncInterval: (Optional) Specify the number of milliseconds to set an interval to synchronize the document,
* if it is greater than 0 enable the synchronization interval (by default is -1)
* - disableBc: (Optional) This boolean disable the broadcast channel functionality, by default is false (broadcast channel enabled)
* - onConnect: (Optional) Set a callback that will triggered immediately when the socket is connected
* - onDisconnect: (Optional) Set a callback that will triggered immediately when the socket is disconnected
* - onConnectError: (Optional) Set a callback that will triggered immediately when the occurs a socket connection error
*
* @prop {boolean=} autoConnect
* (Optional) This boolean specify if the provider should connect when the instance is created, by default is true
*
* @prop {AwarenessProtocol.Awareness=} awareness
* (Optional) An existent awareness, by default is a new AwarenessProtocol.Awareness instance
*
* @prop {number=} resyncInterval
* (Optional) Specify the number of milliseconds to synchronize, by default is -1 (this disable resync interval)
*
* @prop {boolean=} disableBc
* (Optional) This boolean disable the broadcast channel functionality, by default is false (broadcast channel enabled)
*
* @prop {Record<string, unknown>=} auth
* (Optional) Add the authentication data
*/
/**
* The socket io provider class to sync a document
* @extends Observable<string>
*/
export class SocketIOProvider extends Observable {
/**
* The connection url to server. Example: `ws://localhost:3001`
* @type {string}
* @private
* @readonly
*/
_url
/**
* The name of the document room
* @type {string}
* @public
*/
roomName
/**
* The broadcast channel room
* @type {string}
* @private
* @readonly
*/
_broadcastChannel
/**
* The socket connection
* @type {ClientSocket}
* @public
*/
socket
/**
* The yjs document
* @type {Y.Doc}
* @public
*/
doc
/**
* The awareness
* @type {AwarenessProtocol.Awareness}
* @public
*/
awareness
/**
* Disable broadcast channel, by default is false
* @type {boolean}
* @public
*/
disableBc
/**
* The broadcast channel connection status indicator
* @type {boolean}
* @public
*/
bcconnected = false
/**
* The document's sync status indicator
* @type {boolean}
* @private
*/
_synced = false
/**
* Interval to emit `sync-step-1` to sync changes
* @type {ReturnType<typeof setTimeout> | null}
* @private
*/
resyncInterval = null
/**
* Optional overrides for socket.io
* @type {Partial<import('socket.io-client').ManagerOptions & import('socket.io-client').SocketOptions> | undefined}
* @private
* @readonly
*/
_socketIoOptions
/**
* SocketIOProvider constructor
* @constructor
* @param {string} url The connection url from server
* @param {string} roomName The document's room name
* @param {Y.Doc} doc The yjs document
* @param {ProviderConfiguration=} options Configuration options to the SocketIOProvider
* @param {Partial<import('socket.io-client').ManagerOptions & import('socket.io-client').SocketOptions>=} socketIoOptions optional overrides for socket.io
*/
constructor (
url,
roomName,
doc = new Y.Doc(),
{
autoConnect = true,
awareness = new AwarenessProtocol.Awareness(doc),
resyncInterval = -1,
disableBc = false,
auth = {}
} = {},
socketIoOptions = undefined
) {
super()
while (url[url.length - 1] === '/') {
url = url.slice(0, url.length - 1)
}
this._url = url
this.roomName = roomName
this.doc = doc
this.awareness = awareness
this._broadcastChannel = `${url}/${roomName}`
this.disableBc = disableBc
this._socketIoOptions = socketIoOptions
this.socket = io(`${this.url}/yjs|${roomName}`, {
autoConnect: false,
transports: ['websocket'],
forceNew: true,
auth,
...socketIoOptions
})
this._socketIoOptions = socketIoOptions
this.doc.on('update', this.onUpdateDoc)
this.socket.on('connect', () => this.onSocketConnection(resyncInterval))
this.socket.on('disconnect', (event) => this.onSocketDisconnection(event))
this.socket.on('connect_error', (error) =>
this.onSocketConnectionError(error)
)
this.initSyncListeners()
this.initAwarenessListeners()
this.initSystemListeners()
awareness.on('update', this.awarenessUpdate)
if (autoConnect) this.connect()
}
/**
* Broadcast channel room getter
* @type {string}
*/
get broadcastChannel () {
return this._broadcastChannel
}
/**
* URL getter
* @type {string}
*/
get url () {
return this._url
}
/**
* Synchronized state flag getter
* @type {boolean}
*/
get synced () {
return this._synced
}
/**
* Synchronized state flag setter
*/
set synced (state) {
if (this._synced !== state) {
this._synced = state
this.emit('synced', [state])
this.emit('sync', [state])
}
}
/**
* This function initializes the socket event listeners to synchronize document changes.
*
* The synchronization protocol is as follows:
* - A server emits the sync step one event (`sync-step-1`) which sends the document as a state vector
* and the sync step two callback as an acknowledgment according to the socket io acknowledgments.
* - When the client receives the `sync-step-1` event, it executes the `syncStep2` acknowledgment callback and sends
* the difference between the received state vector and the local document (this difference is called an update).
* - The second step of the sync is to apply the update sent in the `syncStep2` callback parameters from the client
* to the document on the server side.
* - There is another event (`sync-update`) that is emitted from the server, which sends an update for the document,
* and when the client receives this event, it applies the received update to the local document.
* - When an update is applied to a document, it will fire the document's "update" event, which
* sends the update to the server.
* @type {() => void}
* @private
* @readonly
*/
initSyncListeners = () => {
this.socket.on(
'sync-step-1',
(
/** @type {ArrayBuffer} */
stateVector,
/** @type {(update: Uint8Array) => void} */
syncStep2
) => {
syncStep2(Y.encodeStateAsUpdate(this.doc, new Uint8Array(stateVector)))
this.synced = true
}
)
this.socket.on('sync-update', this.onSocketSyncUpdate)
}
/**
* This function initializes socket event listeners to synchronize awareness changes.
*
* The awareness protocol is as follows:
* - The server emits the `awareness-update` event by sending the awareness update.
* - The client receives that event and applies the received update to the local awareness.
* - When an update is applied to awareness, the awareness "update" event will fire, which
* sends the update to the server.
* @type {() => void}
* @private
* @readonly
*/
initAwarenessListeners = () => {
this.socket.on('awareness-update', (/** @type {ArrayBuffer} */ update) => {
AwarenessProtocol.applyAwarenessUpdate(
this.awareness,
new Uint8Array(update),
this
)
})
}
/**
* This function initialize the window or process events listener. Specifically set ups the
* window `beforeunload` and process `exit` events to remove the client from the awareness.
* @private
* @type {() => void}
* @readonly
*/
initSystemListeners = () => {
if (typeof window !== 'undefined') { window.addEventListener('beforeunload', this.beforeUnloadHandler) } else if (typeof process !== 'undefined') { process.on('exit', this.beforeUnloadHandler) }
}
/**
* Connect provider's socket
* @type {() => void}
*/
connect () {
if (!this.socket.connected) {
this.emit('status', [{ status: 'connecting' }])
this.socket.connect()
if (!this.disableBc) this.connectBc()
this.synced = false
}
}
/**
* This function runs when the socket connects and reconnects and emits the `sync-step-1`
* and `awareness-update` socket events to start synchronization.
*
* Also starts the resync interval if is enabled.
* @private
* @param {number=} resyncInterval (Optional) A number of milliseconds for interval of synchronize
* @readonly
*/
onSocketConnection = (resyncInterval = -1) => {
this.emit('status', [{ status: 'connected' }])
this.socket.emit(
'sync-step-1',
Y.encodeStateVector(this.doc),
(/** @type {Uint8Array} */ update) => {
Y.applyUpdate(this.doc, new Uint8Array(update), this)
}
)
if (this.awareness.getLocalState() !== null) {
this.socket.emit(
'awareness-update',
AwarenessProtocol.encodeAwarenessUpdate(this.awareness, [
this.doc.clientID
])
)
}
if (resyncInterval > 0) {
this.resyncInterval = setInterval(() => {
if (this.socket.disconnected) return
this.socket.emit(
'sync-step-1',
Y.encodeStateVector(this.doc),
(/** @type {Uint8Array} */ update) => {
Y.applyUpdate(this.doc, new Uint8Array(update), this)
}
)
}, resyncInterval)
}
}
/**
* Disconnect provider's socket
* @type {() => void}
*/
disconnect () {
if (this.socket.connected) {
this.disconnectBc()
this.socket.disconnect()
}
}
/**
* This function runs when the socket is disconnected and emits the socket event `awareness-update`
* which removes this client from awareness.
* @private
* @param {import('socket.io-client').Socket.DisconnectReason} event The reason of the socket disconnection
* @readonly
*/
onSocketDisconnection = (event) => {
this.emit('connection-close', [event, this])
this.synced = false
AwarenessProtocol.removeAwarenessStates(
this.awareness,
Array.from(this.awareness.getStates().keys()).filter(
(client) => client !== this.doc.clientID
),
this
)
this.emit('status', [{ status: 'disconnected' }])
}
/**
* This function is executed when the socket connection fails.
* @param {Error} error The error in the connection
* @readonly
*/
onSocketConnectionError = (error) => {
this.emit('connection-error', [error, this])
}
/**
* Destroy the provider. This method clears the document, awareness, and window/process listeners and disconnects the socket.
* @type {() => void}
*/
destroy () {
if (this.resyncInterval != null) clearInterval(this.resyncInterval)
this.disconnect()
if (typeof window !== 'undefined') { window.removeEventListener('beforeunload', this.beforeUnloadHandler) } else if (typeof process !== 'undefined') { process.off('exit', this.beforeUnloadHandler) }
this.awareness.off('update', this.awarenessUpdate)
this.awareness.destroy()
this.doc.off('update', this.onUpdateDoc)
super.destroy()
}
/**
* This function is executed when the document is updated, if the instance that
* emit the change is not this, it emit the changes by socket and broadcast channel.
* @private
* @param {Uint8Array} update Document update
* @param {SocketIOProvider} origin The SocketIOProvider instance that emits the change.
* @readonly
*/
onUpdateDoc = (update, origin) => {
if (origin !== this) {
this.socket.emit('sync-update', update)
if (this.bcconnected) {
bc.publish(
this._broadcastChannel,
{
type: 'sync-update',
data: update
},
this
)
}
}
}
/**
* This function is called when the server emits the `sync-update` event and applies the received update to the local document.
* @private
* @param {Uint8Array} update A document update received by the `sync-update` socket event
*/
onSocketSyncUpdate = (update) => {
Y.applyUpdate(this.doc, new Uint8Array(update), this)
}
/**
* This function is executed when the local awareness changes and this broadcasts the changes per socket and broadcast channel.
* @private
* @param {{ added: number[], updated: number[], removed: number[] }} awarenessChanges The clients added, updated and removed
* @param {SocketIOProvider | null} origin The SocketIOProvider instance that emits the change.
* @readonly
*/
awarenessUpdate = ({ added, updated, removed }, origin) => {
const changedClients = added.concat(updated).concat(removed)
this.socket.emit(
'awareness-update',
AwarenessProtocol.encodeAwarenessUpdate(this.awareness, changedClients)
)
if (this.bcconnected) {
bc.publish(
this._broadcastChannel,
{
type: 'awareness-update',
data: AwarenessProtocol.encodeAwarenessUpdate(
this.awareness,
changedClients
)
},
this
)
}
}
/**
* This function is executed when the windows will be unloaded or the process will be closed and this
* will remove the local client from awareness.
* @private
* @type {() => void}
* @readonly
*/
beforeUnloadHandler = () => {
AwarenessProtocol.removeAwarenessStates(
this.awareness,
[this.doc.clientID],
'window unload'
)
}
/**
* This function subscribes the provider to the broadcast channel and initiates synchronization by broadcast channel.
* @type {() => void}
* @private
* @readonly
*/
connectBc = () => {
if (!this.bcconnected) {
bc.subscribe(this._broadcastChannel, this.onBroadcastChannelMessage)
this.bcconnected = true
}
bc.publish(
this._broadcastChannel,
{ type: 'sync-step-1', data: Y.encodeStateVector(this.doc) },
this
)
bc.publish(
this._broadcastChannel,
{ type: 'sync-step-2', data: Y.encodeStateAsUpdate(this.doc) },
this
)
bc.publish(
this._broadcastChannel,
{ type: 'query-awareness', data: null },
this
)
bc.publish(
this._broadcastChannel,
{
type: 'awareness-update',
data: AwarenessProtocol.encodeAwarenessUpdate(this.awareness, [
this.doc.clientID
])
},
this
)
}
/**
* This function unsubscribes the provider from the broadcast channel and before unsubscribing, updates the awareness.
* @type {() => void}
* @private
* @readonly
*/
disconnectBc = () => {
bc.publish(
this._broadcastChannel,
{
type: 'awareness-update',
data: AwarenessProtocol.encodeAwarenessUpdate(
this.awareness,
[this.doc.clientID],
new Map()
)
},
this
)
if (this.bcconnected) {
bc.unsubscribe(this._broadcastChannel, this.onBroadcastChannelMessage)
this.bcconnected = false
}
}
/**
* This method handles messages received by the broadcast channel and responds to them.
* @param {{ type: string, data: any }} message The object message received by broadcast channel
* @param {SocketIOProvider} origin The SocketIOProvider instance that emits the change
* @private
* @readonly
*/
onBroadcastChannelMessage = (message, origin) => {
if (origin !== this && message.type.length > 0) {
switch (message.type) {
case 'sync-step-1':
bc.publish(
this._broadcastChannel,
{
type: 'sync-step-2',
data: Y.encodeStateAsUpdate(this.doc, message.data)
},
this
)
break
case 'sync-step-2':
Y.applyUpdate(this.doc, new Uint8Array(message.data), this)
break
case 'sync-update':
Y.applyUpdate(this.doc, new Uint8Array(message.data), this)
break
case 'query-awareness':
bc.publish(
this._broadcastChannel,
{
type: 'awareness-update',
data: AwarenessProtocol.encodeAwarenessUpdate(
this.awareness,
Array.from(this.awareness.getStates().keys())
)
},
this
)
break
case 'awareness-update':
AwarenessProtocol.applyAwarenessUpdate(
this.awareness,
new Uint8Array(message.data),
this
)
break
default:
break
}
}
}
}