-
Notifications
You must be signed in to change notification settings - Fork 30.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
events: allow safely adding listener to abortSignal
PR-URL: #48596 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Moshe Atlow <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Robert Nagy <[email protected]>
- Loading branch information
1 parent
dfa0aee
commit a316808
Showing
4 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import * as common from '../common/index.mjs'; | ||
import * as events from 'node:events'; | ||
import * as assert from 'node:assert'; | ||
import { describe, it } from 'node:test'; | ||
|
||
describe('events.addAbortListener', () => { | ||
it('should throw if signal not provided', () => { | ||
assert.throws(() => events.addAbortListener(), { code: 'ERR_INVALID_ARG_TYPE' }); | ||
}); | ||
|
||
it('should throw if provided signal is invalid', () => { | ||
assert.throws(() => events.addAbortListener(undefined), { code: 'ERR_INVALID_ARG_TYPE' }); | ||
assert.throws(() => events.addAbortListener(null), { code: 'ERR_INVALID_ARG_TYPE' }); | ||
assert.throws(() => events.addAbortListener({}), { code: 'ERR_INVALID_ARG_TYPE' }); | ||
}); | ||
|
||
it('should throw if listener is not a function', () => { | ||
const { signal } = new AbortController(); | ||
assert.throws(() => events.addAbortListener(signal), { code: 'ERR_INVALID_ARG_TYPE' }); | ||
assert.throws(() => events.addAbortListener(signal, {}), { code: 'ERR_INVALID_ARG_TYPE' }); | ||
assert.throws(() => events.addAbortListener(signal, undefined), { code: 'ERR_INVALID_ARG_TYPE' }); | ||
}); | ||
|
||
it('should return a Disposable', () => { | ||
const { signal } = new AbortController(); | ||
const disposable = events.addAbortListener(signal, common.mustNotCall()); | ||
|
||
assert.strictEqual(typeof disposable[Symbol.dispose], 'function'); | ||
}); | ||
|
||
it('should execute the listener immediately for aborted runners', () => { | ||
const disposable = events.addAbortListener(AbortSignal.abort(), common.mustCall()); | ||
assert.strictEqual(typeof disposable[Symbol.dispose], 'function'); | ||
}); | ||
|
||
it('should execute the listener even when event propagation stopped', () => { | ||
const controller = new AbortController(); | ||
const { signal } = controller; | ||
|
||
signal.addEventListener('abort', (e) => e.stopImmediatePropagation()); | ||
events.addAbortListener( | ||
signal, | ||
common.mustCall((e) => assert.strictEqual(e.target, signal)), | ||
); | ||
|
||
controller.abort(); | ||
}); | ||
|
||
it('should remove event listeners when disposed', () => { | ||
const controller = new AbortController(); | ||
const disposable = events.addAbortListener(controller.signal, common.mustNotCall()); | ||
disposable[Symbol.dispose](); | ||
controller.abort(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters