Skip to content

Commit

Permalink
Move from unload to pagehide to avoid using deprecated API
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed May 16, 2024
1 parent 82d8985 commit 24ba0ef
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ export class Client {

this.onUnload = this.onUnload.bind(this)
if (typeof window !== 'undefined' && window.addEventListener) {
window.addEventListener('unload', this.onUnload)
window.addEventListener('pagehide', this.onUnload)
}

this.processing = {}
Expand Down Expand Up @@ -347,7 +347,7 @@ export class Client {
this.node.destroy()
clearInterval(this.pinging)
if (typeof window !== 'undefined' && window.removeEventListener) {
window.removeEventListener('unload', this.onUnload)
window.removeEventListener('pagehide', this.onUnload)
}
}

Expand Down
2 changes: 1 addition & 1 deletion client/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ it('cleans own actions on unload', async () => {
{ type: 'A' },
{ reasons: ['tab' + client.tabId], tab: client.tabId }
)
window.dispatchEvent(new Event('unload'))
window.dispatchEvent(new Event('pagehide'))
await delay(10)
expect(client.log.actions()).toHaveLength(0)
expect(localStorage.getItem('test:tab:' + client.tabId)).toBeNull()
Expand Down
2 changes: 1 addition & 1 deletion cross-tab-client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class CrossTabClient extends Client {

if (typeof window !== 'undefined' && window.addEventListener) {
window.addEventListener('storage', e => this.onStorage(e))
window.addEventListener('unload', e => this.onUnload(e))
window.addEventListener('pagehide', e => this.onUnload(e))
}

if (this.isLocalStorage) {
Expand Down

0 comments on commit 24ba0ef

Please sign in to comment.