Urara-Blog/node_modules/.pnpm-store/v3/files/e1/ed8d68ca978e494719a3d19f7546a3fa7d2485a24ea1ad4de5263fcf0bdc8cfbe03d48d7216151c8b7d598a7bdcde4800855351dbd53948a90c8b939e933e0
2022-08-14 01:14:53 +08:00

57 lines
1 KiB
Text

const { RequestAbortedError } = require('../core/errors')
const kListener = Symbol('kListener')
const kSignal = Symbol('kSignal')
function abort (self) {
if (self.abort) {
self.abort()
} else {
self.onError(new RequestAbortedError())
}
}
function addSignal (self, signal) {
self[kSignal] = null
self[kListener] = null
if (!signal) {
return
}
if (signal.aborted) {
abort(self)
return
}
self[kSignal] = signal
self[kListener] = () => {
abort(self)
}
if ('addEventListener' in self[kSignal]) {
self[kSignal].addEventListener('abort', self[kListener])
} else {
self[kSignal].addListener('abort', self[kListener])
}
}
function removeSignal (self) {
if (!self[kSignal]) {
return
}
if ('removeEventListener' in self[kSignal]) {
self[kSignal].removeEventListener('abort', self[kListener])
} else {
self[kSignal].removeListener('abort', self[kListener])
}
self[kSignal] = null
self[kListener] = null
}
module.exports = {
addSignal,
removeSignal
}