Skip to content

Commit

Permalink
fix(provider): event handler binding for doc updates & awareness #868
Browse files Browse the repository at this point in the history
  • Loading branch information
nperez0111 authored Nov 1, 2024
2 parents de38e44 + 7077588 commit 1c18ee7
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions packages/provider/src/HocuspocusProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ export class HocuspocusProvider extends EventEmitter {
this.emit('awarenessChange', { states: awarenessStatesToArray(this.awareness!.getStates()) })
})

this.document.on('update', this.documentUpdateHandler.bind(this))
this.awareness?.on('update', this.awarenessUpdateHandler.bind(this))
this.document.on('update', this.boundDocumentUpdateHandler)
this.awareness?.on('update', this.boundAwarenessUpdateHandler)
this.registerEventListeners()

if (
Expand All @@ -229,6 +229,10 @@ export class HocuspocusProvider extends EventEmitter {
this.configuration.websocketProvider.attach(this)
}

boundDocumentUpdateHandler = this.documentUpdateHandler.bind(this)

boundAwarenessUpdateHandler = this.awarenessUpdateHandler.bind(this)

boundBroadcastChannelSubscriber = this.broadcastChannelSubscriber.bind(this)

boundPageHide = this.pageHide.bind(this)
Expand Down Expand Up @@ -490,11 +494,11 @@ export class HocuspocusProvider extends EventEmitter {

if (this.awareness) {
removeAwarenessStates(this.awareness, [this.document.clientID], 'provider destroy')
this.awareness.off('update', this.awarenessUpdateHandler)
this.awareness.off('update', this.boundAwarenessUpdateHandler)
this.awareness.destroy()
}

this.document.off('update', this.documentUpdateHandler)
this.document.off('update', this.boundDocumentUpdateHandler)

this.removeAllListeners()

Expand Down

0 comments on commit 1c18ee7

Please sign in to comment.