-
Notifications
You must be signed in to change notification settings - Fork 12
/
provider.ts
469 lines (429 loc) · 17.9 KB
/
provider.ts
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
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, ManagerOptions, Socket, SocketOptions } from 'socket.io-client'
import { AwarenessChange } from '../types'
/**
* 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
*/
export interface ProviderConfiguration {
/**
* (Optional) This boolean specify if the provider should connect when the instance is created, by default is true
*/
autoConnect?: boolean
/**
* (Optional) An existent awareness, by default is a new AwarenessProtocol.Awareness instance
*/
awareness?: AwarenessProtocol.Awareness
/**
* (optional) Specify the number of milliseconds to synchronize, by default is -1 (this disable resync interval)
*/
resyncInterval?: number
/**
* (Optional) This boolean disable the broadcast channel functionality, by default is false (broadcast channel enabled)
*/
disableBc?: boolean
/**
* (Optional) Add the authentication data
*/
auth?: { [key: string]: any }
}
/**
* The socket io provider class to sync a document
*/
export class SocketIOProvider extends Observable<string> {
/**
* The connection url to server. Example: `ws://localhost:3001`
* @type {string}
*/
private readonly _url: string
/**
* The name of the document room
* @type {string}
*/
public roomName: string
/**
* The broadcast channel room
* @type {string}
* @private
*/
private readonly _broadcastChannel: string
/**
* The socket connection
* @type {Socket}
*/
public socket: Socket
/**
* The yjs document
* @type {Y.Doc}
*/
public doc: Y.Doc
/**
* The awareness
* @type {AwarenessProtocol.Awareness}
*/
public awareness: AwarenessProtocol.Awareness
/**
* Disable broadcast channel, by default is false
* @type {boolean}
*/
public disableBc: boolean
/**
* The broadcast channel connection status indicator
* @type {boolean}
*/
public bcconnected: boolean = false
/**
* The document's sync status indicator
* @type {boolean}
* @private
*/
private _synced: boolean = false
/**
* Interval to emit `sync-step-1` to sync changes
* @type {ReturnType<typeof setTimeout> | null}
* @private
*/
private resyncInterval: ReturnType<typeof setTimeout> | null = null
/**
* Optional overrides for socket.io
* @type {Partial<ManagerOptions & SocketOptions> | undefined}
* @private
*/
private readonly _socketIoOptions: Partial<ManagerOptions & SocketOptions> | undefined;
/**
* 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<ManagerOptions & SocketOptions> | undefined} socketIoOptions optional overrides for socket.io
*/
constructor (url: string, roomName: string, doc: Y.Doc = new Y.Doc(), {
autoConnect = true,
awareness = new AwarenessProtocol.Awareness(doc),
resyncInterval = -1,
disableBc = false,
auth = {}
}: ProviderConfiguration,
socketIoOptions: Partial<ManagerOptions & SocketOptions> | undefined = 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: auth,
...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}
*/
public get broadcastChannel (): string {
return this._broadcastChannel
}
/**
* URL getter
* @type {string}
*/
public get url (): string {
return this._url
}
/**
* Synchronized state flag getter
* @type {boolean}
*/
public get synced (): boolean {
return this._synced
}
/**
* Synchronized state flag setter
*/
public 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
*/
private readonly initSyncListeners = (): void => {
this.socket.on('sync-step-1', (stateVector: ArrayBuffer, syncStep2: (update: Uint8Array) => void) => {
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
*/
private readonly initAwarenessListeners = (): void => {
this.socket.on('awareness-update', (update: ArrayBuffer) => {
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.
* @type {() => void}
*/
private readonly initSystemListeners = (): void => {
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}
*/
public connect (): void {
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 {() => void | Promise<void>} onConnect (Optional) A callback that will be triggered every time that socket is connected or reconnected
* @param {number} resyncInterval (Optional) A number of milliseconds for interval of synchronize
* @type {(onConnect: () => void | Promise<void>, resyncInterval: number = -1) => void}
*/
private readonly onSocketConnection = (resyncInterval: ProviderConfiguration['resyncInterval'] = -1): void => {
this.emit('status', [{ status: 'connected' }])
this.socket.emit('sync-step-1', Y.encodeStateVector(this.doc), (update: Uint8Array) => {
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), (update: Uint8Array) => {
Y.applyUpdate(this.doc, new Uint8Array(update), this)
})
}, resyncInterval)
}
}
/**
* Disconnect provider's socket
* @type {() => void}
*/
public disconnect (): void {
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 {Socket.DisconnectReason} event The reason of the socket disconnection
* @param {() => void | Promise<void>} onDisconnect (Optional) A callback that will be triggered every time that socket is disconnected
* @type {(event: Socket.DisconnectReason, onDisconnect: () => void | Promise<void>) => void}
*/
private readonly onSocketDisconnection = (event: Socket.DisconnectReason): void => {
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
* @param {(error: Error) => void | Promise<void>} onConnectError (Optional) A callback that will be triggered every time that socket has a connection error
* @type {(error: Error, onConnectError: (error: Error) => void | Promise<void>) => void}
*/
private readonly onSocketConnectionError = (error: Error): void => {
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}
*/
public destroy (): void {
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.
* @type {(update: Uint8Array, origin: SocketIOProvider) => void}
*/
private readonly onUpdateDoc = (update: Uint8Array, origin: SocketIOProvider): void => {
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
* @type {(update: Uint8Array) => void}
*/
private readonly onSocketSyncUpdate = (update: ArrayBuffer): void => {
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.
* @type {({ added, updated, removed }: { added: number[], updated: number[], removed: number[] }, origin: SocketIOProvider | null) => void}
*/
private readonly awarenessUpdate = ({ added, updated, removed }: AwarenessChange, origin: SocketIOProvider | null): void => {
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}
*/
private readonly beforeUnloadHandler = (): void => {
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 = (): void => {
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 = (): void => {
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
* @type {(message: { type: string, data: any }, origin: SocketIOProvider) => void}
*/
private readonly onBroadcastChannelMessage = (message: { type: string, data: any }, origin: SocketIOProvider): void => {
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
}
}
}
}