Skip to content

Commit

Permalink
fix: 👔 fixing unload event to handle both browser and node.
Browse files Browse the repository at this point in the history
  • Loading branch information
whizzzkid committed Jan 26, 2023
1 parent 8c4f560 commit c999b06
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/EventAccumulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,15 @@ export class EventAccumulator<T extends CountlyWebSdk | CountlyNodeSdk> implemen
* Setup the beforeunload event to flush all events.
*/
private setupUnloadEvent (): void {
globalThis.addEventListener('beforeunload', () => {
const flushAllHandler = (): void => {
this.flushAll()
})
}

if (typeof globalThis.addEventListener === 'function') {
globalThis.addEventListener('beforeunload', flushAllHandler)
} else if (typeof globalThis.process?.on === 'function') {
globalThis.process.on('beforeExit', flushAllHandler)
}
}

/**
Expand Down

0 comments on commit c999b06

Please sign in to comment.