mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
refactor: clean up composeSignals early-return structure (#10844)
Flips the main if-block to an early-return pattern to reduce nesting. Explicitly initializes aborted to false. Adds a guard clause in unsubscribe to avoid duplicate cleanup. Preserves the existing manual event-listener approach because AbortSignal.any() introduces event-loop timing differences that break fetch adapter stream-abort tests. Closes #10815. Co-authored-by: mixelburg <mixelburg@gmail.com> Co-authored-by: Jay <jasonsaayman@gmail.com>
This commit is contained in:
@@ -3,54 +3,55 @@ import AxiosError from '../core/AxiosError.js';
|
|||||||
import utils from '../utils.js';
|
import utils from '../utils.js';
|
||||||
|
|
||||||
const composeSignals = (signals, timeout) => {
|
const composeSignals = (signals, timeout) => {
|
||||||
const { length } = (signals = signals ? signals.filter(Boolean) : []);
|
signals = signals ? signals.filter(Boolean) : [];
|
||||||
|
|
||||||
if (timeout || length) {
|
if (!timeout && !signals.length) {
|
||||||
let controller = new AbortController();
|
return;
|
||||||
|
|
||||||
let aborted;
|
|
||||||
|
|
||||||
const onabort = function (reason) {
|
|
||||||
if (!aborted) {
|
|
||||||
aborted = true;
|
|
||||||
unsubscribe();
|
|
||||||
const err = reason instanceof Error ? reason : this.reason;
|
|
||||||
controller.abort(
|
|
||||||
err instanceof AxiosError
|
|
||||||
? err
|
|
||||||
: new CanceledError(err instanceof Error ? err.message : err)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let timer =
|
|
||||||
timeout &&
|
|
||||||
setTimeout(() => {
|
|
||||||
timer = null;
|
|
||||||
onabort(new AxiosError(`timeout of ${timeout}ms exceeded`, AxiosError.ETIMEDOUT));
|
|
||||||
}, timeout);
|
|
||||||
|
|
||||||
const unsubscribe = () => {
|
|
||||||
if (signals) {
|
|
||||||
timer && clearTimeout(timer);
|
|
||||||
timer = null;
|
|
||||||
signals.forEach((signal) => {
|
|
||||||
signal.unsubscribe
|
|
||||||
? signal.unsubscribe(onabort)
|
|
||||||
: signal.removeEventListener('abort', onabort);
|
|
||||||
});
|
|
||||||
signals = null;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
signals.forEach((signal) => signal.addEventListener('abort', onabort));
|
|
||||||
|
|
||||||
const { signal } = controller;
|
|
||||||
|
|
||||||
signal.unsubscribe = () => utils.asap(unsubscribe);
|
|
||||||
|
|
||||||
return signal;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const controller = new AbortController();
|
||||||
|
|
||||||
|
let aborted = false;
|
||||||
|
|
||||||
|
const onabort = function (reason) {
|
||||||
|
if (!aborted) {
|
||||||
|
aborted = true;
|
||||||
|
unsubscribe();
|
||||||
|
const err = reason instanceof Error ? reason : this.reason;
|
||||||
|
controller.abort(
|
||||||
|
err instanceof AxiosError
|
||||||
|
? err
|
||||||
|
: new CanceledError(err instanceof Error ? err.message : err)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let timer =
|
||||||
|
timeout &&
|
||||||
|
setTimeout(() => {
|
||||||
|
timer = null;
|
||||||
|
onabort(new AxiosError(`timeout of ${timeout}ms exceeded`, AxiosError.ETIMEDOUT));
|
||||||
|
}, timeout);
|
||||||
|
|
||||||
|
const unsubscribe = () => {
|
||||||
|
if (!signals) { return; }
|
||||||
|
timer && clearTimeout(timer);
|
||||||
|
timer = null;
|
||||||
|
signals.forEach((signal) => {
|
||||||
|
signal.unsubscribe
|
||||||
|
? signal.unsubscribe(onabort)
|
||||||
|
: signal.removeEventListener('abort', onabort);
|
||||||
|
});
|
||||||
|
signals = null;
|
||||||
|
};
|
||||||
|
|
||||||
|
signals.forEach((signal) => signal.addEventListener('abort', onabort));
|
||||||
|
|
||||||
|
const { signal } = controller;
|
||||||
|
|
||||||
|
signal.unsubscribe = () => utils.asap(unsubscribe);
|
||||||
|
|
||||||
|
return signal;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default composeSignals;
|
export default composeSignals;
|
||||||
|
|||||||
Reference in New Issue
Block a user