@@ -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 , [
@@ -353,13 +368,15 @@ export class SocketIOProvider extends Observable {
353
368
onSocketDisconnection = ( event ) => {
354
369
this . emit ( 'connection-close' , [ event , this ] )
355
370
this . synced = false
356
- AwarenessProtocol . removeAwarenessStates (
357
- this . awareness ,
358
- Array . from ( this . awareness . getStates ( ) . keys ( ) ) . filter (
359
- ( client ) => client !== this . doc . clientID
360
- ) ,
361
- this
362
- )
371
+ if ( this . enableAwareness && this . awareness ) {
372
+ AwarenessProtocol . removeAwarenessStates (
373
+ this . awareness ,
374
+ Array . from ( this . awareness . getStates ( ) . keys ( ) ) . filter (
375
+ ( client ) => client !== this . doc . clientID
376
+ ) ,
377
+ this
378
+ )
379
+ }
363
380
this . emit ( 'status' , [ { status : 'disconnected' } ] )
364
381
}
365
382
@@ -380,8 +397,10 @@ export class SocketIOProvider extends Observable {
380
397
if ( this . resyncInterval != null ) clearInterval ( this . resyncInterval )
381
398
this . disconnect ( )
382
399
if ( typeof window !== 'undefined' ) { window . removeEventListener ( 'beforeunload' , this . beforeUnloadHandler ) } else if ( typeof process !== 'undefined' ) { process . off ( 'exit' , this . beforeUnloadHandler ) }
383
- this . awareness . off ( 'update' , this . awarenessUpdate )
384
- this . awareness . destroy ( )
400
+ if ( this . enableAwareness ) {
401
+ this . awareness ?. off ( 'update' , this . awarenessUpdate )
402
+ this . awareness ?. destroy ( )
403
+ }
385
404
this . doc . off ( 'update' , this . onUpdateDoc )
386
405
super . destroy ( )
387
406
}
@@ -427,6 +446,8 @@ export class SocketIOProvider extends Observable {
427
446
* @readonly
428
447
*/
429
448
awarenessUpdate = ( { added, updated, removed } , origin ) => {
449
+ if ( ! this . awareness ) return
450
+
430
451
const changedClients = added . concat ( updated ) . concat ( removed )
431
452
this . socket . emit (
432
453
'awareness-update' ,
@@ -455,6 +476,8 @@ export class SocketIOProvider extends Observable {
455
476
* @readonly
456
477
*/
457
478
beforeUnloadHandler = ( ) => {
479
+ if ( ! this . enableAwareness || ! this . awareness ) return
480
+
458
481
AwarenessProtocol . removeAwarenessStates (
459
482
this . awareness ,
460
483
[ this . doc . clientID ] ,
@@ -483,21 +506,24 @@ export class SocketIOProvider extends Observable {
483
506
{ type : 'sync-step-2' , data : Y . encodeStateAsUpdate ( this . doc ) } ,
484
507
this
485
508
)
486
- bc . publish (
487
- this . _broadcastChannel ,
488
- { type : 'query-awareness' , data : null } ,
489
- this
490
- )
491
- bc . publish (
492
- this . _broadcastChannel ,
493
- {
494
- type : 'awareness-update' ,
495
- data : AwarenessProtocol . encodeAwarenessUpdate ( this . awareness , [
496
- this . doc . clientID
497
- ] )
498
- } ,
499
- this
500
- )
509
+
510
+ if ( this . enableAwareness && this . awareness ) {
511
+ bc . publish (
512
+ this . _broadcastChannel ,
513
+ { type : 'query-awareness' , data : null } ,
514
+ this
515
+ )
516
+ bc . publish (
517
+ this . _broadcastChannel ,
518
+ {
519
+ type : 'awareness-update' ,
520
+ data : AwarenessProtocol . encodeAwarenessUpdate ( this . awareness , [
521
+ this . doc . clientID
522
+ ] )
523
+ } ,
524
+ this
525
+ )
526
+ }
501
527
}
502
528
503
529
/**
@@ -507,18 +533,20 @@ export class SocketIOProvider extends Observable {
507
533
* @readonly
508
534
*/
509
535
disconnectBc = ( ) => {
510
- bc . publish (
511
- this . _broadcastChannel ,
512
- {
513
- type : 'awareness-update' ,
514
- data : AwarenessProtocol . encodeAwarenessUpdate (
515
- this . awareness ,
516
- [ this . doc . clientID ] ,
517
- new Map ( )
518
- )
519
- } ,
520
- this
521
- )
536
+ if ( this . enableAwareness && this . awareness ) {
537
+ bc . publish (
538
+ this . _broadcastChannel ,
539
+ {
540
+ type : 'awareness-update' ,
541
+ data : AwarenessProtocol . encodeAwarenessUpdate (
542
+ this . awareness ,
543
+ [ this . doc . clientID ] ,
544
+ new Map ( )
545
+ )
546
+ } ,
547
+ this
548
+ )
549
+ }
522
550
if ( this . bcconnected ) {
523
551
bc . unsubscribe ( this . _broadcastChannel , this . onBroadcastChannelMessage )
524
552
this . bcconnected = false
@@ -554,27 +582,33 @@ export class SocketIOProvider extends Observable {
554
582
Y . applyUpdate ( this . doc , new Uint8Array ( message . data ) , this )
555
583
break
556
584
557
- case 'query-awareness' :
558
- bc . publish (
559
- this . _broadcastChannel ,
560
- {
561
- type : 'awareness-update' ,
562
- data : AwarenessProtocol . encodeAwarenessUpdate (
563
- this . awareness ,
564
- Array . from ( this . awareness . getStates ( ) . keys ( ) )
565
- )
566
- } ,
567
- this
568
- )
585
+ case 'query-awareness' : {
586
+ if ( this . enableAwareness && this . awareness ) {
587
+ bc . publish (
588
+ this . _broadcastChannel ,
589
+ {
590
+ type : 'awareness-update' ,
591
+ data : AwarenessProtocol . encodeAwarenessUpdate (
592
+ this . awareness ,
593
+ Array . from ( this . awareness . getStates ( ) . keys ( ) )
594
+ )
595
+ } ,
596
+ this
597
+ )
598
+ }
569
599
break
570
-
571
- case 'awareness-update' :
572
- AwarenessProtocol . applyAwarenessUpdate (
573
- this . awareness ,
574
- new Uint8Array ( message . data ) ,
575
- this
576
- )
600
+ }
601
+
602
+ case 'awareness-update' : {
603
+ if ( this . enableAwareness && this . awareness ) {
604
+ AwarenessProtocol . applyAwarenessUpdate (
605
+ this . awareness ,
606
+ new Uint8Array ( message . data ) ,
607
+ this
608
+ )
609
+ }
577
610
break
611
+ }
578
612
579
613
default :
580
614
break
0 commit comments