Skip to content

Commit

Permalink
Fix for RN check if addEventListener & removeEventListener are availa…
Browse files Browse the repository at this point in the history
…ble (#34)

* Fix for RN check if addEventListener & removeEventListener are available

* add checks to cross-tab-client.js as well

* add checks to confirm.js
  • Loading branch information
CanRau authored and ai committed Nov 5, 2019
1 parent 88b6b01 commit 14504f0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
4 changes: 2 additions & 2 deletions client.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ function Client (options) {
})

this.onUnload = this.onUnload.bind(this)
if (typeof window !== 'undefined') {
if (typeof window !== 'undefined' && window.addEventListener) {
window.addEventListener('unload', this.onUnload)
}
}
Expand Down Expand Up @@ -392,7 +392,7 @@ Client.prototype = {
this.onUnload()
this.node.destroy()
clearInterval(this.pinging)
if (typeof window !== 'undefined') {
if (typeof window !== 'undefined' && window.removeEventListener) {
window.removeEventListener('unload', this.onUnload)
}
},
Expand Down
10 changes: 6 additions & 4 deletions confirm.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ function confirm (client) {
wait = false
}

if (client.role !== 'follower' && wait && disconnected) {
window.addEventListener('beforeunload', block)
} else {
window.removeEventListener('beforeunload', block)
if (typeof window !== 'undefined' && window.addEventListener) {
if (client.role !== 'follower' && wait && disconnected) {
window.addEventListener('beforeunload', block)
} else {
window.removeEventListener('beforeunload', block)
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions cross-tab-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ function CrossTabClient (options) {

this.onStorage = this.onStorage.bind(this)
this.onUnload = this.onUnload.bind(this)
if (typeof window !== 'undefined') {
if (typeof window !== 'undefined' && window.addEventListener) {
window.addEventListener('storage', this.onStorage)
window.addEventListener('unload', this.onUnload)
}
Expand Down Expand Up @@ -240,7 +240,7 @@ CrossTabClient.prototype = {
clearTimeout(this.watching)
clearTimeout(this.elections)
clearInterval(this.leadership)
if (typeof window !== 'undefined') {
if (typeof window !== 'undefined' && window.removeEventListener) {
window.removeEventListener('storage', this.onStorage)
}
},
Expand Down

0 comments on commit 14504f0

Please sign in to comment.