Skip to content

Commit

Permalink
feat(realtime): Reacts to visibility changes
Browse files Browse the repository at this point in the history
  • Loading branch information
edas committed Mar 3, 2020
1 parent a2170a8 commit 4ce703e
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ exports[`TriggerManager when given an oauth konnector should redirect to OAuthFo
"onOffline": [Function],
"onOnline": [Function],
"onResume": [Function],
"onVisibilityChange": [Function],
"onWebSocketClose": [Function],
"onWebSocketError": [Function],
"onWebSocketMessage": [Function],
Expand Down
14 changes: 14 additions & 0 deletions packages/cozy-realtime/src/CozyRealtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ class CozyRealtime {
// global events
this.onOnline = this.onOnline.bind(this)
this.onOffline = this.onOffline.bind(this)
this.onVisibilityChange = this.onVisibilityChange.bind(this)
// cordova events
this.onDeviceReady = this.onDeviceReady.bind(this)
this.onResume = this.onResume.bind(this)
Expand Down Expand Up @@ -618,6 +619,7 @@ class CozyRealtime {
if (hasBrowserContext && window.addEventListener) {
window.addEventListener('online', this.onOnline)
window.addEventListener('offline', this.onOffline)
window.addEventListener('visibilitychange', this.onVisibilityChange)
}
}

Expand All @@ -629,6 +631,7 @@ class CozyRealtime {
if (hasBrowserContext && window.removeEventListener) {
window.removeEventListener('online', this.onOnline)
window.removeEventListener('offline', this.onOffline)
window.removeEventListener('visibilitychange', this.onVisibilityChange)
}
}

Expand All @@ -648,6 +651,17 @@ class CozyRealtime {
onOffline() {
this.hasWebSocket() && this.revokeWebSocket()
}

/**
* When the page visibility changes
* @private
*/
onVisibilityChange() {
if (document.visibilityState && document.visibilityState === 'visible') {
// if we have a reconnect waiting, do it immediatly
this.retryManager.stopCurrentAttemptWaitingTime()
}
}
}

MicroEE.mixin(CozyRealtime)
Expand Down

0 comments on commit 4ce703e

Please sign in to comment.