Skip to content

Commit e790582

Browse files
removing an eventListener on abort.
1 parent 81c1775 commit e790582

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,9 @@ class Stream extends EventEmitter {
525525
if (opts.signal.aborted) {
526526
throw new Error('Stream aborted.')
527527
}
528-
opts.signal.addEventListener('abort', abort.bind(this))
528+
const handler = abort.bind(this)
529+
this._clearSignal = clearSignal.bind(null, opts.signal, handler)
530+
opts.signal.addEventListener('abort', handler)
529531
}
530532
}
531533
}
@@ -571,6 +573,9 @@ class Stream extends EventEmitter {
571573
this._writableState.updateNextTick()
572574
}
573575
this._predestroy()
576+
if (this._clearSignal !== undefined) {
577+
this._clearSignal()
578+
}
574579
}
575580
}
576581

@@ -976,6 +981,10 @@ function abort () {
976981
this.destroy(new Error('Stream aborted.'))
977982
}
978983

984+
function clearSignal (signal, handler) {
985+
signal.removeEventListener('abort', handler)
986+
}
987+
979988
module.exports = {
980989
pipeline,
981990
pipelinePromise,

test/async-iterator.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ tape('using abort controller', async function (t) {
158158
return r
159159
}
160160
const controller = new AbortController()
161+
let removeCalled = false
162+
beforeFn(controller.signal, 'removeEventListener', () => { removeCalled = true })
161163
const inc = []
162164
setTimeout(() => controller.abort(), 10)
163165
try {
@@ -167,8 +169,17 @@ tape('using abort controller', async function (t) {
167169
} catch (err) {
168170
t.same(err.message, 'Stream aborted.')
169171
}
172+
t.same(removeCalled, true)
170173
t.same(inc, [0])
171174
t.end()
175+
176+
function beforeFn (obj, fnName, handler) {
177+
const orig = obj[fnName]
178+
obj[fnName] = function () {
179+
handler.apply(this, arguments)
180+
orig.apply(this, arguments)
181+
}
182+
}
172183
})
173184
tape('using aborted abort controller', async function (t) {
174185
const controller = new AbortController()

0 commit comments

Comments
 (0)