Skip to content

Commit

Permalink
feat(Realtime): Remove all subscribe before window is unloaded
Browse files Browse the repository at this point in the history
  • Loading branch information
kosssi committed May 7, 2019
1 parent 58c4369 commit a74fa65
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/cozy-realtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class CozyRealtime {
* - Save cozyClient
* - create socket
* - listen cozyClient events
* - unsubscribeAll if window unload
*
* @constructor
* @param {CozyClient} cozyClient A cozy client
Expand All @@ -94,12 +95,22 @@ class CozyRealtime {
this._receiveMessage = this._receiveMessage.bind(this)
this._receiveError = this._receiveError.bind(this)
this._resubscribe = this._resubscribe.bind(this)
this._windowUnload = this._windowUnload.bind(this)

this._createSocket()

this._cozyClient.on('login', this._updateAuthentication)
this._cozyClient.on('tokenRefreshed', this._updateAuthentication)
this._cozyClient.on('logout', this.unsubscribeAll)

if (window) {
window.addEventListener('beforeunload', this._windowUnload)
}
}

_windowUnload() {
window.removeEventListener('beforeunload', this._windowUnload)
this.unsubscribeAll()
}

/**
Expand Down

0 comments on commit a74fa65

Please sign in to comment.