Skip to content

Commit bb53494

Browse files
EastSun5566galaxian85
authored andcommitted
feat: add enableAwareness option to SocketIOProvider
1 parent c1d038a commit bb53494

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, [
@@ -355,13 +370,15 @@ export class SocketIOProvider extends Observable {
355370

356371
this.emit('connection-close', [event, this])
357372
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+
}
365382
this.emit('status', [{ status: 'disconnected' }])
366383
}
367384

@@ -382,8 +399,10 @@ export class SocketIOProvider extends Observable {
382399
if (this.resyncInterval != null) clearInterval(this.resyncInterval)
383400
this.disconnect()
384401
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+
}
387406
this.doc.off('update', this.onUpdateDoc)
388407
super.destroy()
389408
}
@@ -429,6 +448,8 @@ export class SocketIOProvider extends Observable {
429448
* @readonly
430449
*/
431450
awarenessUpdate = ({ added, updated, removed }, origin) => {
451+
if (!this.awareness) return
452+
432453
const changedClients = added.concat(updated).concat(removed)
433454
this.socket.emit(
434455
'awareness-update',
@@ -457,6 +478,8 @@ export class SocketIOProvider extends Observable {
457478
* @readonly
458479
*/
459480
beforeUnloadHandler = () => {
481+
if (!this.enableAwareness || !this.awareness) return
482+
460483
AwarenessProtocol.removeAwarenessStates(
461484
this.awareness,
462485
[this.doc.clientID],
@@ -485,21 +508,24 @@ export class SocketIOProvider extends Observable {
485508
{ type: 'sync-step-2', data: Y.encodeStateAsUpdate(this.doc) },
486509
this
487510
)
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+
}
503529
}
504530

505531
/**
@@ -509,18 +535,20 @@ export class SocketIOProvider extends Observable {
509535
* @readonly
510536
*/
511537
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+
}
524552
if (this.bcconnected) {
525553
bc.unsubscribe(this._broadcastChannel, this.onBroadcastChannelMessage)
526554
this.bcconnected = false
@@ -556,27 +584,33 @@ export class SocketIOProvider extends Observable {
556584
Y.applyUpdate(this.doc, new Uint8Array(message.data), this)
557585
break
558586

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+
}
571601
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+
}
579612
break
613+
}
580614

581615
default:
582616
break

0 commit comments

Comments
 (0)