-
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.
util: allow safely adding listener to abortSignal
- Loading branch information
1 parent
e8810b9
commit 0e45c8f
Showing
3 changed files
with
125 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
43 changes: 43 additions & 0 deletions
43
test/parallel/test-util-add-safe-abort-signal-abort-listener.mjs
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,43 @@ | ||
import * as common from '../common/index.mjs'; | ||
import * as util from 'node:util'; | ||
import * as assert from 'node:assert'; | ||
import { describe, it } from 'node:test'; | ||
|
||
describe('util.addSafeAbortSignalAbortListener', () => { | ||
it('should throw if signal not provided', () => { | ||
assert.throws(() => util.addSafeAbortSignalAbortListener(), { code: 'ERR_INVALID_ARG_TYPE' }); | ||
}); | ||
|
||
it('should throw if provided signal is invalid', () => { | ||
assert.throws(() => util.addSafeAbortSignalAbortListener(undefined), { code: 'ERR_INVALID_ARG_TYPE' }); | ||
assert.throws(() => util.addSafeAbortSignalAbortListener(null), { code: 'ERR_INVALID_ARG_TYPE' }); | ||
assert.throws(() => util.addSafeAbortSignalAbortListener({}), { code: 'ERR_INVALID_ARG_TYPE' }); | ||
}); | ||
|
||
it('should return a Disposable', () => { | ||
const { signal } = new AbortController(); | ||
const disposable = util.addSafeAbortSignalAbortListener(signal, common.mustNotCall()); | ||
|
||
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()); | ||
util.addSafeAbortSignalAbortListener( | ||
signal, | ||
common.mustCall((e) => assert.strictEqual(e.target, signal)), | ||
); | ||
|
||
controller.abort(); | ||
}); | ||
|
||
it('should remove event listeners when disposed', () => { | ||
const controller = new AbortController(); | ||
const disposable = util.addSafeAbortSignalAbortListener(controller.signal, common.mustNotCall()); | ||
disposable[Symbol.dispose](); | ||
controller.abort(); | ||
}); | ||
}); |