Skip to content

Commit

Permalink
run npm run format
Browse files Browse the repository at this point in the history
  • Loading branch information
jeswr committed Dec 31, 2024
1 parent 152d787 commit 0d05403
Showing 1 changed file with 48 additions and 41 deletions.
89 changes: 48 additions & 41 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ const isBlob =
/* eslint-enable indent */

const validateAbortSignal = (signal, name) => {
if (signal !== undefined &&
(signal === null ||
typeof signal !== 'object' ||
!('aborted' in signal))) {
if (signal !== undefined && (signal === null || typeof signal !== 'object' || !('aborted' in signal))) {
throw new ERR_INVALID_ARG_TYPE(name, 'AbortSignal', signal)
}
}
const validateFunction = (value, name) => {
if (typeof value !== 'function') { throw new ERR_INVALID_ARG_TYPE(name, 'Function', value) }
if (typeof value !== 'function') {
throw new ERR_INVALID_ARG_TYPE(name, 'Function', value)
}
}

// This is a simplified version of AggregateError
Expand Down Expand Up @@ -152,45 +151,53 @@ module.exports = {
deprecate(fn, message) {
return fn
},
addAbortListener: require('events').addAbortListener || function addAbortListener(signal, listener) {
if (signal === undefined) {
throw new ERR_INVALID_ARG_TYPE('signal', 'AbortSignal', signal)
}
validateAbortSignal(signal, 'signal')
validateFunction(listener, 'listener')

let removeEventListener
if (signal.aborted) {
queueMicrotask(() => listener())
} else {
signal.addEventListener('abort', listener, { __proto__: null, once: true, [kResistStopPropagation]: true })
removeEventListener = () => {
signal.removeEventListener('abort', listener)
addAbortListener:
require('events').addAbortListener ||
function addAbortListener(signal, listener) {
if (signal === undefined) {
throw new ERR_INVALID_ARG_TYPE('signal', 'AbortSignal', signal)
}
}
return {
__proto__: null,
[SymbolDispose]() {
removeEventListener?.()
validateAbortSignal(signal, 'signal')
validateFunction(listener, 'listener')

let removeEventListener
if (signal.aborted) {
queueMicrotask(() => listener())
} else {
signal.addEventListener('abort', listener, { __proto__: null, once: true, [kResistStopPropagation]: true })
removeEventListener = () => {
signal.removeEventListener('abort', listener)
}
}
return {
__proto__: null,
[SymbolDispose]() {
removeEventListener?.()
}
}
},
AbortSignalAny:
AbortSignal.any ||
function AbortSignalAny(signals) {
// Fast path if there is only one signal.
if (signals.length === 1) {
return signals[0]
}
const ac = new AbortController()
const abort = () => ac.abort()
signals.forEach((signal) => {
validateAbortSignal(signal, 'signals')
signal.addEventListener('abort', abort, { once: true })
})
ac.signal.addEventListener(
'abort',
() => {
signals.forEach((signal) => signal.removeEventListener('abort', abort))
},
{ once: true }
)
return ac.signal
}
},
AbortSignalAny: AbortSignal.any || function AbortSignalAny(signals) {
// Fast path if there is only one signal.
if (signals.length === 1) {
return signals[0]
}
const ac = new AbortController()
const abort = () => ac.abort()
signals.forEach(signal => {
validateAbortSignal(signal, 'signals')
signal.addEventListener('abort', abort, { once: true })
})
ac.signal.addEventListener('abort', () => {
signals.forEach(signal => signal.removeEventListener('abort', abort))
}, { once: true })
return ac.signal
}
}

module.exports.promisify.custom = Symbol.for('nodejs.util.promisify.custom')

0 comments on commit 0d05403

Please sign in to comment.