source: imaps-frontend/node_modules/axios/lib/helpers/composeSignals.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.3 KB
Line 
1import CanceledError from "../cancel/CanceledError.js";
2import AxiosError from "../core/AxiosError.js";
3import utils from '../utils.js';
4
5const composeSignals = (signals, timeout) => {
6 const {length} = (signals = signals ? signals.filter(Boolean) : []);
7
8 if (timeout || length) {
9 let controller = new AbortController();
10
11 let aborted;
12
13 const onabort = function (reason) {
14 if (!aborted) {
15 aborted = true;
16 unsubscribe();
17 const err = reason instanceof Error ? reason : this.reason;
18 controller.abort(err instanceof AxiosError ? err : new CanceledError(err instanceof Error ? err.message : err));
19 }
20 }
21
22 let timer = timeout && setTimeout(() => {
23 timer = null;
24 onabort(new AxiosError(`timeout ${timeout} of ms exceeded`, AxiosError.ETIMEDOUT))
25 }, timeout)
26
27 const unsubscribe = () => {
28 if (signals) {
29 timer && clearTimeout(timer);
30 timer = null;
31 signals.forEach(signal => {
32 signal.unsubscribe ? signal.unsubscribe(onabort) : signal.removeEventListener('abort', onabort);
33 });
34 signals = null;
35 }
36 }
37
38 signals.forEach((signal) => signal.addEventListener('abort', onabort));
39
40 const {signal} = controller;
41
42 signal.unsubscribe = () => utils.asap(unsubscribe);
43
44 return signal;
45 }
46}
47
48export default composeSignals;
Note: See TracBrowser for help on using the repository browser.