• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1const { RequestAbortedError } = require('../core/errors')
2
3const kListener = Symbol('kListener')
4const kSignal = Symbol('kSignal')
5
6function abort (self) {
7  if (self.abort) {
8    self.abort()
9  } else {
10    self.onError(new RequestAbortedError())
11  }
12}
13
14function addSignal (self, signal) {
15  self[kSignal] = null
16  self[kListener] = null
17
18  if (!signal) {
19    return
20  }
21
22  if (signal.aborted) {
23    abort(self)
24    return
25  }
26
27  self[kSignal] = signal
28  self[kListener] = () => {
29    abort(self)
30  }
31
32  if ('addEventListener' in self[kSignal]) {
33    self[kSignal].addEventListener('abort', self[kListener])
34  } else {
35    self[kSignal].addListener('abort', self[kListener])
36  }
37}
38
39function removeSignal (self) {
40  if (!self[kSignal]) {
41    return
42  }
43
44  if ('removeEventListener' in self[kSignal]) {
45    self[kSignal].removeEventListener('abort', self[kListener])
46  } else {
47    self[kSignal].removeListener('abort', self[kListener])
48  }
49
50  self[kSignal] = null
51  self[kListener] = null
52}
53
54module.exports = {
55  addSignal,
56  removeSignal
57}
58