Skip to content

Commit 430620a

Browse files
committed
feat: add enableAwareness option to SocketIOProvider
1 parent 0be69a9 commit 430620a

File tree

1 file changed

+94
-60
lines changed

1 file changed

+94
-60
lines changed

src/y-socket-io/client.js

Lines changed: 94 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ import { io } from 'socket.io-client'
2323
* @prop {boolean=} autoConnect
2424
* (Optional) This boolean specify if the provider should connect when the instance is created, by default is true
2525
*
26+
* @prop {boolean=} enableAwareness
27+
* (Optional) This boolean enable the awareness functionality, by default is true
28+
*
2629
* @prop {AwarenessProtocol.Awareness=} awareness
2730
* (Optional) An existent awareness, by default is a new AwarenessProtocol.Awareness instance
2831
*
@@ -73,9 +76,14 @@ export class SocketIOProvider extends Observable {
7376
* @public
7477
*/
7578
doc
79+
/**
80+
* Enable awareness
81+
* @type {boolean}
82+
*/
83+
enableAwareness
7684
/**
7785
* The awareness
78-
* @type {AwarenessProtocol.Awareness}
86+
* @type {AwarenessProtocol.Awareness=}
7987
* @public
8088
*/
8189
awareness
@@ -126,7 +134,8 @@ export class SocketIOProvider extends Observable {
126134
doc = new Y.Doc(),
127135
{
128136
autoConnect = true,
129-
awareness = new AwarenessProtocol.Awareness(doc),
137+
enableAwareness = true,
138+
awareness = enableAwareness ? new AwarenessProtocol.Awareness(doc) : undefined,
130139
resyncInterval = -1,
131140
disableBc = false,
132141
auth = {}
@@ -140,6 +149,8 @@ export class SocketIOProvider extends Observable {
140149
this._url = url
141150
this.roomName = roomName
142151
this.doc = doc
152+
153+
this.enableAwareness = enableAwareness
143154
this.awareness = awareness
144155

145156
this._broadcastChannel = `${url}/${roomName}`
@@ -167,11 +178,13 @@ export class SocketIOProvider extends Observable {
167178

168179
this.initSyncListeners()
169180

170-
this.initAwarenessListeners()
181+
if (this.enableAwareness) {
182+
this.initAwarenessListeners()
183+
awareness?.on('update', this.awarenessUpdate)
184+
}
171185

172186
this.initSystemListeners()
173187

174-
awareness.on('update', this.awarenessUpdate)
175188

176189
if (autoConnect) this.connect()
177190
}
@@ -260,6 +273,8 @@ export class SocketIOProvider extends Observable {
260273
*/
261274
initAwarenessListeners = () => {
262275
this.socket.on('awareness-update', (/** @type {ArrayBuffer} */ update) => {
276+
if (!this.awareness) return
277+
263278
AwarenessProtocol.applyAwarenessUpdate(
264279
this.awareness,
265280
new Uint8Array(update),
@@ -310,7 +325,7 @@ export class SocketIOProvider extends Observable {
310325
Y.applyUpdate(this.doc, new Uint8Array(update), this)
311326
}
312327
)
313-
if (this.awareness.getLocalState() !== null) {
328+
if (this.enableAwareness && this.awareness && this.awareness.getLocalState() !== null) {
314329
this.socket.emit(
315330
'awareness-update',
316331
AwarenessProtocol.encodeAwarenessUpdate(this.awareness, [
@@ -353,13 +368,15 @@ export class SocketIOProvider extends Observable {
353368
onSocketDisconnection = (event) => {
354369
this.emit('connection-close', [event, this])
355370
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+
}
363380
this.emit('status', [{ status: 'disconnected' }])
364381
}
365382

@@ -380,8 +397,10 @@ export class SocketIOProvider extends Observable {
380397
if (this.resyncInterval != null) clearInterval(this.resyncInterval)
381398
this.disconnect()
382399
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+
}
385404
this.doc.off('update', this.onUpdateDoc)
386405
super.destroy()
387406
}
@@ -427,6 +446,8 @@ export class SocketIOProvider extends Observable {
427446
* @readonly
428447
*/
429448
awarenessUpdate = ({ added, updated, removed }, origin) => {
449+
if (!this.awareness) return
450+
430451
const changedClients = added.concat(updated).concat(removed)
431452
this.socket.emit(
432453
'awareness-update',
@@ -455,6 +476,8 @@ export class SocketIOProvider extends Observable {
455476
* @readonly
456477
*/
457478
beforeUnloadHandler = () => {
479+
if (!this.enableAwareness || !this.awareness) return
480+
458481
AwarenessProtocol.removeAwarenessStates(
459482
this.awareness,
460483
[this.doc.clientID],
@@ -483,21 +506,24 @@ export class SocketIOProvider extends Observable {
483506
{ type: 'sync-step-2', data: Y.encodeStateAsUpdate(this.doc) },
484507
this
485508
)
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+
}
501527
}
502528

503529
/**
@@ -507,18 +533,20 @@ export class SocketIOProvider extends Observable {
507533
* @readonly
508534
*/
509535
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+
}
522550
if (this.bcconnected) {
523551
bc.unsubscribe(this._broadcastChannel, this.onBroadcastChannelMessage)
524552
this.bcconnected = false
@@ -554,27 +582,33 @@ export class SocketIOProvider extends Observable {
554582
Y.applyUpdate(this.doc, new Uint8Array(message.data), this)
555583
break
556584

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+
}
569599
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+
}
577610
break
611+
}
578612

579613
default:
580614
break

0 commit comments

Comments
 (0)