@@ -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,12 +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
-
176
188
if ( autoConnect ) this . connect ( )
177
189
}
178
190
@@ -260,6 +272,8 @@ export class SocketIOProvider extends Observable {
260
272
*/
261
273
initAwarenessListeners = ( ) => {
262
274
this . socket . on ( 'awareness-update' , ( /** @type {ArrayBuffer } */ update ) => {
275
+ if ( ! this . awareness ) return
276
+
263
277
AwarenessProtocol . applyAwarenessUpdate (
264
278
this . awareness ,
265
279
new Uint8Array ( update ) ,
@@ -310,7 +324,7 @@ export class SocketIOProvider extends Observable {
310
324
Y . applyUpdate ( this . doc , new Uint8Array ( update ) , this )
311
325
}
312
326
)
313
- if ( this . awareness . getLocalState ( ) !== null ) {
327
+ if ( this . enableAwareness && this . awareness && this . awareness . getLocalState ( ) !== null ) {
314
328
this . socket . emit (
315
329
'awareness-update' ,
316
330
AwarenessProtocol . encodeAwarenessUpdate ( this . awareness , [
@@ -355,13 +369,15 @@ export class SocketIOProvider extends Observable {
355
369
356
370
this . emit ( 'connection-close' , [ event , this ] )
357
371
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
- )
372
+ if ( this . enableAwareness && this . awareness ) {
373
+ AwarenessProtocol . removeAwarenessStates (
374
+ this . awareness ,
375
+ Array . from ( this . awareness . getStates ( ) . keys ( ) ) . filter (
376
+ ( client ) => client !== this . doc . clientID
377
+ ) ,
378
+ this
379
+ )
380
+ }
365
381
this . emit ( 'status' , [ { status : 'disconnected' } ] )
366
382
}
367
383
@@ -382,8 +398,10 @@ export class SocketIOProvider extends Observable {
382
398
if ( this . resyncInterval != null ) clearInterval ( this . resyncInterval )
383
399
this . disconnect ( )
384
400
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 ( )
401
+ if ( this . enableAwareness ) {
402
+ this . awareness ?. off ( 'update' , this . awarenessUpdate )
403
+ this . awareness ?. destroy ( )
404
+ }
387
405
this . doc . off ( 'update' , this . onUpdateDoc )
388
406
super . destroy ( )
389
407
}
@@ -429,6 +447,8 @@ export class SocketIOProvider extends Observable {
429
447
* @readonly
430
448
*/
431
449
awarenessUpdate = ( { added, updated, removed } , origin ) => {
450
+ if ( ! this . awareness ) return
451
+
432
452
const changedClients = added . concat ( updated ) . concat ( removed )
433
453
this . socket . emit (
434
454
'awareness-update' ,
@@ -457,6 +477,8 @@ export class SocketIOProvider extends Observable {
457
477
* @readonly
458
478
*/
459
479
beforeUnloadHandler = ( ) => {
480
+ if ( ! this . enableAwareness || ! this . awareness ) return
481
+
460
482
AwarenessProtocol . removeAwarenessStates (
461
483
this . awareness ,
462
484
[ this . doc . clientID ] ,
@@ -485,21 +507,24 @@ export class SocketIOProvider extends Observable {
485
507
{ type : 'sync-step-2' , data : Y . encodeStateAsUpdate ( this . doc ) } ,
486
508
this
487
509
)
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
- )
510
+
511
+ if ( this . enableAwareness && this . awareness ) {
512
+ bc . publish (
513
+ this . _broadcastChannel ,
514
+ { type : 'query-awareness' , data : null } ,
515
+ this
516
+ )
517
+ bc . publish (
518
+ this . _broadcastChannel ,
519
+ {
520
+ type : 'awareness-update' ,
521
+ data : AwarenessProtocol . encodeAwarenessUpdate ( this . awareness , [
522
+ this . doc . clientID
523
+ ] )
524
+ } ,
525
+ this
526
+ )
527
+ }
503
528
}
504
529
505
530
/**
@@ -509,18 +534,20 @@ export class SocketIOProvider extends Observable {
509
534
* @readonly
510
535
*/
511
536
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
- )
537
+ if ( this . enableAwareness && this . awareness ) {
538
+ bc . publish (
539
+ this . _broadcastChannel ,
540
+ {
541
+ type : 'awareness-update' ,
542
+ data : AwarenessProtocol . encodeAwarenessUpdate (
543
+ this . awareness ,
544
+ [ this . doc . clientID ] ,
545
+ new Map ( )
546
+ )
547
+ } ,
548
+ this
549
+ )
550
+ }
524
551
if ( this . bcconnected ) {
525
552
bc . unsubscribe ( this . _broadcastChannel , this . onBroadcastChannelMessage )
526
553
this . bcconnected = false
@@ -556,27 +583,33 @@ export class SocketIOProvider extends Observable {
556
583
Y . applyUpdate ( this . doc , new Uint8Array ( message . data ) , this )
557
584
break
558
585
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
- )
586
+ case 'query-awareness' : {
587
+ if ( this . enableAwareness && this . awareness ) {
588
+ bc . publish (
589
+ this . _broadcastChannel ,
590
+ {
591
+ type : 'awareness-update' ,
592
+ data : AwarenessProtocol . encodeAwarenessUpdate (
593
+ this . awareness ,
594
+ Array . from ( this . awareness . getStates ( ) . keys ( ) )
595
+ )
596
+ } ,
597
+ this
598
+ )
599
+ }
571
600
break
572
-
573
- case 'awareness-update' :
574
- AwarenessProtocol . applyAwarenessUpdate (
575
- this . awareness ,
576
- new Uint8Array ( message . data ) ,
577
- this
578
- )
601
+ }
602
+
603
+ case 'awareness-update' : {
604
+ if ( this . enableAwareness && this . awareness ) {
605
+ AwarenessProtocol . applyAwarenessUpdate (
606
+ this . awareness ,
607
+ new Uint8Array ( message . data ) ,
608
+ this
609
+ )
610
+ }
579
611
break
612
+ }
580
613
581
614
default :
582
615
break
0 commit comments