-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
events: add addDisposableListener method to EventEmitter #58453
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -177,36 +177,38 @@ function eos(stream, options, callback) { | |
callback.call(stream); | ||
}; | ||
|
||
const disposableStack = new DisposableStack(); // eslint-disable-line no-undef | ||
|
||
const onrequest = () => { | ||
stream.req.on('finish', onfinish); | ||
disposableStack.use(stream.req.addDisposableListener('finish', onfinish)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would need a stream benchmark run before landing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The results are mixed and it's just not clear if the difference is worth being concerned about... some benchmarks are faster, others are slower.
|
||
}; | ||
|
||
if (isRequest(stream)) { | ||
stream.on('complete', onfinish); | ||
disposableStack.use(stream.addDisposableListener('complete', onfinish)); | ||
if (!willEmitClose) { | ||
stream.on('abort', onclose); | ||
disposableStack.use(stream.addDisposableListener('abort', onclose)); | ||
} | ||
if (stream.req) { | ||
onrequest(); | ||
} else { | ||
stream.on('request', onrequest); | ||
disposableStack.use(stream.addDisposableListener('request', onrequest)); | ||
} | ||
} else if (writable && !wState) { // legacy streams | ||
stream.on('end', onlegacyfinish); | ||
stream.on('close', onlegacyfinish); | ||
disposableStack.use(stream.addDisposableListener('end', onlegacyfinish)); | ||
disposableStack.use(stream.addDisposableListener('close', onlegacyfinish)); | ||
} | ||
|
||
// Not all streams will emit 'close' after 'aborted'. | ||
if (!willEmitClose && typeof stream.aborted === 'boolean') { | ||
stream.on('aborted', onclose); | ||
disposableStack.use(stream.addDisposableListener('aborted', onclose)); | ||
} | ||
|
||
stream.on('end', onend); | ||
stream.on('finish', onfinish); | ||
disposableStack.use(stream.addDisposableListener('end', onend)); | ||
disposableStack.use(stream.addDisposableListener('finish', onfinish)); | ||
if (options.error !== false) { | ||
stream.on('error', onerror); | ||
disposableStack.use(stream.addDisposableListener('error', onerror)); | ||
} | ||
stream.on('close', onclose); | ||
disposableStack.use(stream.addDisposableListener('close', onclose)); | ||
|
||
if (closed) { | ||
process.nextTick(onclose); | ||
|
@@ -233,18 +235,10 @@ function eos(stream, options, callback) { | |
|
||
const cleanup = () => { | ||
callback = nop; | ||
stream.removeListener('aborted', onclose); | ||
stream.removeListener('complete', onfinish); | ||
stream.removeListener('abort', onclose); | ||
stream.removeListener('request', onrequest); | ||
if (stream.req) stream.req.removeListener('finish', onfinish); | ||
stream.removeListener('end', onlegacyfinish); | ||
stream.removeListener('close', onlegacyfinish); | ||
stream.removeListener('finish', onfinish); | ||
stream.removeListener('end', onend); | ||
stream.removeListener('error', onerror); | ||
stream.removeListener('close', onclose); | ||
disposableStack.dispose(); | ||
}; | ||
// Arrange for the cleanup function to call itself when disposed. | ||
cleanup[SymbolDispose] = cleanup; | ||
|
||
if (options.signal && !closed) { | ||
const abort = () => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const { strictEqual, throws } = require('assert'); | ||
const { EventEmitter } = require('events'); | ||
|
||
const emitter = new EventEmitter(); | ||
|
||
{ | ||
// Verify that the disposable stack removes the handlers | ||
// when the stack is disposed. | ||
using ds = new DisposableStack(); // eslint-disable-line no-undef | ||
ds.use(emitter.addDisposableListener('foo', common.mustCall())); | ||
ds.use(emitter.addDisposableListener('bar', common.mustCall())); | ||
ds.use(emitter.addDisposableListener('baz', common.mustNotCall()), | ||
{ once: true }); | ||
emitter.emit('foo'); | ||
emitter.emit('bar'); | ||
strictEqual(emitter.listenerCount('foo'), 1); | ||
strictEqual(emitter.listenerCount('bar'), 1); | ||
|
||
// The disposer returned by addDisposableListener is a function that | ||
// can be used to remove the listener. | ||
const disposer = emitter.addDisposableListener('foo', common.mustNotCall()); | ||
disposer(); | ||
} | ||
emitter.emit('foo'); | ||
emitter.emit('bar'); | ||
emitter.emit('baz'); | ||
strictEqual(emitter.listenerCount('foo'), 0); | ||
strictEqual(emitter.listenerCount('bar'), 0); | ||
|
||
// ============================================================================ | ||
// Type checking on inputs | ||
throws(() => emitter.addDisposableListener('foo', 'not a function'), { | ||
code: 'ERR_INVALID_ARG_TYPE', | ||
}); | ||
|
||
throws(() => emitter.addDisposableListener('foo', () => {}, ''), { | ||
code: 'ERR_INVALID_ARG_TYPE', | ||
}); | ||
|
||
throws(() => emitter.addDisposableListener('foo', () => {}, { once: '' }), { | ||
code: 'ERR_INVALID_ARG_TYPE', | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Experimental?