@@ -2,7 +2,11 @@ import * as Y from 'yjs'
22import * as bc from 'lib0/broadcastchannel'
33import * as AwarenessProtocol from 'y-protocols/awareness'
44import { Observable } from 'lib0/observable'
5- import { io , Socket } from 'socket.io-client'
5+ import { io } from 'socket.io-client'
6+
7+ /**
8+ * @typedef {import('socket.io-client').Socket } ClientSocket
9+ */
610
711/**
812 * @typedef ProviderConfiguration
@@ -59,7 +63,7 @@ export class SocketIOProvider extends Observable {
5963 _broadcastChannel
6064 /**
6165 * The socket connection
62- * @type {Socket }
66+ * @type {ClientSocket }
6367 * @public
6468 */
6569 socket
@@ -116,7 +120,7 @@ export class SocketIOProvider extends Observable {
116120 * @param {ProviderConfiguration= } options Configuration options to the SocketIOProvider
117121 * @param {Partial<import('socket.io-client').ManagerOptions & import('socket.io-client').SocketOptions>= } socketIoOptions optional overrides for socket.io
118122 */
119- constructor (
123+ constructor (
120124 url ,
121125 roomName ,
122126 doc = new Y . Doc ( ) ,
@@ -125,7 +129,7 @@ export class SocketIOProvider extends Observable {
125129 awareness = new AwarenessProtocol . Awareness ( doc ) ,
126130 resyncInterval = - 1 ,
127131 disableBc = false ,
128- auth = { } ,
132+ auth = { }
129133 } = { } ,
130134 socketIoOptions = undefined
131135 ) {
@@ -146,8 +150,8 @@ export class SocketIOProvider extends Observable {
146150 autoConnect : false ,
147151 transports : [ 'websocket' ] ,
148152 forceNew : true ,
149- auth : auth ,
150- ...socketIoOptions ,
153+ auth,
154+ ...socketIoOptions
151155 } )
152156 this . _socketIoOptions = socketIoOptions
153157
@@ -176,30 +180,30 @@ export class SocketIOProvider extends Observable {
176180 * Broadcast channel room getter
177181 * @type {string }
178182 */
179- get broadcastChannel ( ) {
183+ get broadcastChannel ( ) {
180184 return this . _broadcastChannel
181185 }
182186
183187 /**
184188 * URL getter
185189 * @type {string }
186190 */
187- get url ( ) {
191+ get url ( ) {
188192 return this . _url
189193 }
190194
191195 /**
192196 * Synchronized state flag getter
193197 * @type {boolean }
194198 */
195- get synced ( ) {
199+ get synced ( ) {
196200 return this . _synced
197201 }
198202
199203 /**
200204 * Synchronized state flag setter
201205 */
202- set synced ( state ) {
206+ set synced ( state ) {
203207 if ( this . _synced !== state ) {
204208 this . _synced = state
205209 this . emit ( 'synced' , [ state ] )
@@ -272,17 +276,14 @@ export class SocketIOProvider extends Observable {
272276 * @readonly
273277 */
274278 initSystemListeners = ( ) => {
275- if ( typeof window !== 'undefined' )
276- window . addEventListener ( 'beforeunload' , this . beforeUnloadHandler )
277- else if ( typeof process !== 'undefined' )
278- process . on ( 'exit' , this . beforeUnloadHandler )
279+ if ( typeof window !== 'undefined' ) { window . addEventListener ( 'beforeunload' , this . beforeUnloadHandler ) } else if ( typeof process !== 'undefined' ) { process . on ( 'exit' , this . beforeUnloadHandler ) }
279280 }
280281
281282 /**
282283 * Connect provider's socket
283284 * @type {() => void }
284285 */
285- connect ( ) {
286+ connect ( ) {
286287 if ( ! this . socket . connected ) {
287288 this . emit ( 'status' , [ { status : 'connecting' } ] )
288289 this . socket . connect ( )
@@ -313,7 +314,7 @@ export class SocketIOProvider extends Observable {
313314 this . socket . emit (
314315 'awareness-update' ,
315316 AwarenessProtocol . encodeAwarenessUpdate ( this . awareness , [
316- this . doc . clientID ,
317+ this . doc . clientID
317318 ] )
318319 )
319320 }
@@ -335,7 +336,7 @@ export class SocketIOProvider extends Observable {
335336 * Disconnect provider's socket
336337 * @type {() => void }
337338 */
338- disconnect ( ) {
339+ disconnect ( ) {
339340 if ( this . socket . connected ) {
340341 this . disconnectBc ( )
341342 this . socket . disconnect ( )
@@ -346,7 +347,7 @@ export class SocketIOProvider extends Observable {
346347 * This function runs when the socket is disconnected and emits the socket event `awareness-update`
347348 * which removes this client from awareness.
348349 * @private
349- * @param {Socket.DisconnectReason } event The reason of the socket disconnection
350+ * @param {import('socket.io-client'). Socket.DisconnectReason } event The reason of the socket disconnection
350351 * @readonly
351352 */
352353 onSocketDisconnection = ( event ) => {
@@ -375,13 +376,10 @@ export class SocketIOProvider extends Observable {
375376 * Destroy the provider. This method clears the document, awareness, and window/process listeners and disconnects the socket.
376377 * @type {() => void }
377378 */
378- destroy ( ) {
379+ destroy ( ) {
379380 if ( this . resyncInterval != null ) clearInterval ( this . resyncInterval )
380381 this . disconnect ( )
381- if ( typeof window !== 'undefined' )
382- window . removeEventListener ( 'beforeunload' , this . beforeUnloadHandler )
383- else if ( typeof process !== 'undefined' )
384- process . off ( 'exit' , this . beforeUnloadHandler )
382+ if ( typeof window !== 'undefined' ) { window . removeEventListener ( 'beforeunload' , this . beforeUnloadHandler ) } else if ( typeof process !== 'undefined' ) { process . off ( 'exit' , this . beforeUnloadHandler ) }
385383 this . awareness . off ( 'update' , this . awarenessUpdate )
386384 this . awareness . destroy ( )
387385 this . doc . off ( 'update' , this . onUpdateDoc )
@@ -404,7 +402,7 @@ export class SocketIOProvider extends Observable {
404402 this . _broadcastChannel ,
405403 {
406404 type : 'sync-update' ,
407- data : update ,
405+ data : update
408406 } ,
409407 this
410408 )
@@ -442,7 +440,7 @@ export class SocketIOProvider extends Observable {
442440 data : AwarenessProtocol . encodeAwarenessUpdate (
443441 this . awareness ,
444442 changedClients
445- ) ,
443+ )
446444 } ,
447445 this
448446 )
@@ -495,8 +493,8 @@ export class SocketIOProvider extends Observable {
495493 {
496494 type : 'awareness-update' ,
497495 data : AwarenessProtocol . encodeAwarenessUpdate ( this . awareness , [
498- this . doc . clientID ,
499- ] ) ,
496+ this . doc . clientID
497+ ] )
500498 } ,
501499 this
502500 )
@@ -517,7 +515,7 @@ export class SocketIOProvider extends Observable {
517515 this . awareness ,
518516 [ this . doc . clientID ] ,
519517 new Map ( )
520- ) ,
518+ )
521519 } ,
522520 this
523521 )
@@ -542,7 +540,7 @@ export class SocketIOProvider extends Observable {
542540 this . _broadcastChannel ,
543541 {
544542 type : 'sync-step-2' ,
545- data : Y . encodeStateAsUpdate ( this . doc , message . data ) ,
543+ data : Y . encodeStateAsUpdate ( this . doc , message . data )
546544 } ,
547545 this
548546 )
@@ -564,7 +562,7 @@ export class SocketIOProvider extends Observable {
564562 data : AwarenessProtocol . encodeAwarenessUpdate (
565563 this . awareness ,
566564 Array . from ( this . awareness . getStates ( ) . keys ( ) )
567- ) ,
565+ )
568566 } ,
569567 this
570568 )
0 commit comments