@@ -23,6 +23,9 @@ import { io } from 'socket.io-client'
23
23
* @prop {boolean= } autoConnect
24
24
* (Optional) This boolean specify if the provider should connect when the instance is created, by default is true
25
25
*
26
+ * @prop {boolean= } enableAwareness
27
+ * (Optional) This boolean enable the awareness functionality, by default is true
28
+ *
26
29
* @prop {AwarenessProtocol.Awareness= } awareness
27
30
* (Optional) An existent awareness, by default is a new AwarenessProtocol.Awareness instance
28
31
*
@@ -73,9 +76,14 @@ export class SocketIOProvider extends Observable {
73
76
* @public
74
77
*/
75
78
doc
79
+ /**
80
+ * Enable awareness
81
+ * @type {boolean }
82
+ */
83
+ enableAwareness
76
84
/**
77
85
* The awareness
78
- * @type {AwarenessProtocol.Awareness }
86
+ * @type {AwarenessProtocol.Awareness= }
79
87
* @public
80
88
*/
81
89
awareness
@@ -126,7 +134,8 @@ export class SocketIOProvider extends Observable {
126
134
doc = new Y . Doc ( ) ,
127
135
{
128
136
autoConnect = true ,
129
- awareness = new AwarenessProtocol . Awareness ( doc ) ,
137
+ enableAwareness = true ,
138
+ awareness = enableAwareness ? new AwarenessProtocol . Awareness ( doc ) : undefined ,
130
139
resyncInterval = - 1 ,
131
140
disableBc = false ,
132
141
auth = { }
@@ -140,6 +149,8 @@ export class SocketIOProvider extends Observable {
140
149
this . _url = url
141
150
this . roomName = roomName
142
151
this . doc = doc
152
+
153
+ this . enableAwareness = enableAwareness
143
154
this . awareness = awareness
144
155
145
156
this . _broadcastChannel = `${ url } /${ roomName } `
@@ -167,11 +178,13 @@ export class SocketIOProvider extends Observable {
167
178
168
179
this . initSyncListeners ( )
169
180
170
- this . initAwarenessListeners ( )
181
+ if ( this . enableAwareness ) {
182
+ this . initAwarenessListeners ( )
183
+ awareness ?. on ( 'update' , this . awarenessUpdate )
184
+ }
171
185
172
186
this . initSystemListeners ( )
173
187
174
- awareness . on ( 'update' , this . awarenessUpdate )
175
188
176
189
if ( autoConnect ) this . connect ( )
177
190
}
@@ -260,6 +273,8 @@ export class SocketIOProvider extends Observable {
260
273
*/
261
274
initAwarenessListeners = ( ) => {
262
275
this . socket . on ( 'awareness-update' , ( /** @type {ArrayBuffer } */ update ) => {
276
+ if ( ! this . awareness ) return
277
+
263
278
AwarenessProtocol . applyAwarenessUpdate (
264
279
this . awareness ,
265
280
new Uint8Array ( update ) ,
@@ -310,7 +325,7 @@ export class SocketIOProvider extends Observable {
310
325
Y . applyUpdate ( this . doc , new Uint8Array ( update ) , this )
311
326
}
312
327
)
313
- if ( this . awareness . getLocalState ( ) !== null ) {
328
+ if ( this . enableAwareness && this . awareness && this . awareness . getLocalState ( ) !== null ) {
314
329
this . socket . emit (
315
330
'awareness-update' ,
316
331
AwarenessProtocol . encodeAwarenessUpdate ( this . awareness , [
@@ -355,13 +370,15 @@ export class SocketIOProvider extends Observable {
355
370
356
371
this . emit ( 'connection-close' , [ event , this ] )
357
372
this . synced = false
358
- AwarenessProtocol . removeAwarenessStates (
359
- this . awareness ,
360
- Array . from ( this . awareness . getStates ( ) . keys ( ) ) . filter (
361
- ( client ) => client !== this . doc . clientID
362
- ) ,
363
- this
364
- )
373
+ if ( this . enableAwareness && this . awareness ) {
374
+ AwarenessProtocol . removeAwarenessStates (
375
+ this . awareness ,
376
+ Array . from ( this . awareness . getStates ( ) . keys ( ) ) . filter (
377
+ ( client ) => client !== this . doc . clientID
378
+ ) ,
379
+ this
380
+ )
381
+ }
365
382
this . emit ( 'status' , [ { status : 'disconnected' } ] )
366
383
}
367
384
@@ -382,8 +399,10 @@ export class SocketIOProvider extends Observable {
382
399
if ( this . resyncInterval != null ) clearInterval ( this . resyncInterval )
383
400
this . disconnect ( )
384
401
if ( typeof window !== 'undefined' ) { window . removeEventListener ( 'beforeunload' , this . beforeUnloadHandler ) } else if ( typeof process !== 'undefined' ) { process . off ( 'exit' , this . beforeUnloadHandler ) }
385
- this . awareness . off ( 'update' , this . awarenessUpdate )
386
- this . awareness . destroy ( )
402
+ if ( this . enableAwareness ) {
403
+ this . awareness ?. off ( 'update' , this . awarenessUpdate )
404
+ this . awareness ?. destroy ( )
405
+ }
387
406
this . doc . off ( 'update' , this . onUpdateDoc )
388
407
super . destroy ( )
389
408
}
@@ -429,6 +448,8 @@ export class SocketIOProvider extends Observable {
429
448
* @readonly
430
449
*/
431
450
awarenessUpdate = ( { added, updated, removed } , origin ) => {
451
+ if ( ! this . awareness ) return
452
+
432
453
const changedClients = added . concat ( updated ) . concat ( removed )
433
454
this . socket . emit (
434
455
'awareness-update' ,
@@ -457,6 +478,8 @@ export class SocketIOProvider extends Observable {
457
478
* @readonly
458
479
*/
459
480
beforeUnloadHandler = ( ) => {
481
+ if ( ! this . enableAwareness || ! this . awareness ) return
482
+
460
483
AwarenessProtocol . removeAwarenessStates (
461
484
this . awareness ,
462
485
[ this . doc . clientID ] ,
@@ -485,21 +508,24 @@ export class SocketIOProvider extends Observable {
485
508
{ type : 'sync-step-2' , data : Y . encodeStateAsUpdate ( this . doc ) } ,
486
509
this
487
510
)
488
- bc . publish (
489
- this . _broadcastChannel ,
490
- { type : 'query-awareness' , data : null } ,
491
- this
492
- )
493
- bc . publish (
494
- this . _broadcastChannel ,
495
- {
496
- type : 'awareness-update' ,
497
- data : AwarenessProtocol . encodeAwarenessUpdate ( this . awareness , [
498
- this . doc . clientID
499
- ] )
500
- } ,
501
- this
502
- )
511
+
512
+ if ( this . enableAwareness && this . awareness ) {
513
+ bc . publish (
514
+ this . _broadcastChannel ,
515
+ { type : 'query-awareness' , data : null } ,
516
+ this
517
+ )
518
+ bc . publish (
519
+ this . _broadcastChannel ,
520
+ {
521
+ type : 'awareness-update' ,
522
+ data : AwarenessProtocol . encodeAwarenessUpdate ( this . awareness , [
523
+ this . doc . clientID
524
+ ] )
525
+ } ,
526
+ this
527
+ )
528
+ }
503
529
}
504
530
505
531
/**
@@ -509,18 +535,20 @@ export class SocketIOProvider extends Observable {
509
535
* @readonly
510
536
*/
511
537
disconnectBc = ( ) => {
512
- bc . publish (
513
- this . _broadcastChannel ,
514
- {
515
- type : 'awareness-update' ,
516
- data : AwarenessProtocol . encodeAwarenessUpdate (
517
- this . awareness ,
518
- [ this . doc . clientID ] ,
519
- new Map ( )
520
- )
521
- } ,
522
- this
523
- )
538
+ if ( this . enableAwareness && this . awareness ) {
539
+ bc . publish (
540
+ this . _broadcastChannel ,
541
+ {
542
+ type : 'awareness-update' ,
543
+ data : AwarenessProtocol . encodeAwarenessUpdate (
544
+ this . awareness ,
545
+ [ this . doc . clientID ] ,
546
+ new Map ( )
547
+ )
548
+ } ,
549
+ this
550
+ )
551
+ }
524
552
if ( this . bcconnected ) {
525
553
bc . unsubscribe ( this . _broadcastChannel , this . onBroadcastChannelMessage )
526
554
this . bcconnected = false
@@ -556,27 +584,33 @@ export class SocketIOProvider extends Observable {
556
584
Y . applyUpdate ( this . doc , new Uint8Array ( message . data ) , this )
557
585
break
558
586
559
- case 'query-awareness' :
560
- bc . publish (
561
- this . _broadcastChannel ,
562
- {
563
- type : 'awareness-update' ,
564
- data : AwarenessProtocol . encodeAwarenessUpdate (
565
- this . awareness ,
566
- Array . from ( this . awareness . getStates ( ) . keys ( ) )
567
- )
568
- } ,
569
- this
570
- )
587
+ case 'query-awareness' : {
588
+ if ( this . enableAwareness && this . awareness ) {
589
+ bc . publish (
590
+ this . _broadcastChannel ,
591
+ {
592
+ type : 'awareness-update' ,
593
+ data : AwarenessProtocol . encodeAwarenessUpdate (
594
+ this . awareness ,
595
+ Array . from ( this . awareness . getStates ( ) . keys ( ) )
596
+ )
597
+ } ,
598
+ this
599
+ )
600
+ }
571
601
break
572
-
573
- case 'awareness-update' :
574
- AwarenessProtocol . applyAwarenessUpdate (
575
- this . awareness ,
576
- new Uint8Array ( message . data ) ,
577
- this
578
- )
602
+ }
603
+
604
+ case 'awareness-update' : {
605
+ if ( this . enableAwareness && this . awareness ) {
606
+ AwarenessProtocol . applyAwarenessUpdate (
607
+ this . awareness ,
608
+ new Uint8Array ( message . data ) ,
609
+ this
610
+ )
611
+ }
579
612
break
613
+ }
580
614
581
615
default :
582
616
break
0 commit comments