Skip to content

Commit

Permalink
fix: Handle events only once.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shogun committed Apr 1, 2020
1 parent 829f520 commit 40e57d4
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = function generateHeapSnapshot(options, cb) {
const { destination } = Object.assign({ destination: destinationFile('heapsnapshot') }, options)
const session = new Session()
let error = null
let handled = false

if (typeof destination !== 'string' || destination.length === 0) {
throw new Error('The destination option must be a non empty string')
Expand All @@ -24,10 +25,22 @@ module.exports = function generateHeapSnapshot(options, cb) {
const writer = new SonicBoom({ dest: destination })

writer.on('error', err => {
/* istanbul ignore if */
if (handled) {
return
}

handled = true
callback(err)
})

writer.on('close', () => {
/* istanbul ignore if */
if (handled) {
return
}

handled = true
callback(error, destination)
})

Expand Down

0 comments on commit 40e57d4

Please sign in to comment.