Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [#1468] Add AbortSignal.any() static method #1469

Merged
merged 5 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion packages/happy-dom/src/fetch/AbortSignal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export default class AbortSignal extends EventTarget {
* @returns AbortSignal instance.
*/
public static abort(reason?: Error): AbortSignal {
const signal = new this[PropertySymbol.window].AbortSignal();
const signal = new this();
(<Error>signal.reason) =
reason ||
new this[PropertySymbol.window].DOMException(
Expand All @@ -86,4 +86,43 @@ export default class AbortSignal extends EventTarget {
(<boolean>signal.aborted) = true;
return signal;
}

/**
* Takes an iterable of abort signals and returns an AbortSignal that is
* aborted when any of the input iterable abort signals are aborted.
*
* The abort reason will be set to the reason of the first signal that is
* aborted. If any of the given abort signals are already aborted then so will
* be the returned AbortSignal.
*
* @param [signals] Iterable of abort signals.
* @returns AbortSignal instance.
*/
public static any(signals: AbortSignal[]): AbortSignal {
for (const signal of signals) {
if (signal.aborted) {
return this.abort(signal.reason);
}
}

const anySignal = new this();
const handlers = new Map<AbortSignal, () => void>();

const stopListening = (): void => {
for (const signal of signals) {
signal.removeEventListener('abort', handlers.get(signal));
}
};

for (const signal of signals) {
const handler = (): void => {
stopListening();
anySignal[PropertySymbol.abort](signal.reason);
};
handlers.set(signal, handler);
signal.addEventListener('abort', handler);
}

return anySignal;
}
}
6 changes: 1 addition & 5 deletions packages/happy-dom/src/fetch/Fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,7 @@ export default class Fetch {
this.#window.location.protocol === 'https:'
) {
throw new this.#window.DOMException(
`Mixed Content: The page at '${
this.#window.location.href
}' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint '${
this.request.url
}'. This request has been blocked; the content must be served over HTTPS.`,
`Mixed Content: The page at '${this.#window.location.href}' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint '${this.request.url}'. This request has been blocked; the content must be served over HTTPS.`,
DOMExceptionNameEnum.securityError
);
}
Expand Down
6 changes: 1 addition & 5 deletions packages/happy-dom/src/fetch/SyncFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,7 @@ export default class SyncFetch {
this.#window.location.protocol === 'https:'
) {
throw new this.#window.DOMException(
`Mixed Content: The page at '${
this.#window.location.href
}' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint '${
this.request.url
}'. This request has been blocked; the content must be served over HTTPS.`,
`Mixed Content: The page at '${this.#window.location.href}' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint '${this.request.url}'. This request has been blocked; the content must be served over HTTPS.`,
DOMExceptionNameEnum.securityError
);
}
Expand Down
31 changes: 31 additions & 0 deletions packages/happy-dom/test/fetch/AbortSignal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,37 @@ describe('AbortSignal', () => {
});
});

describe('AbortSignal.any()', () => {
it('Returns a signal that is asynchronously aborted when one of the supplied signals is asynchronously aborted.', () => {
const signal1 = new window.AbortSignal();
const signal2 = new window.AbortSignal();
const signal = window.AbortSignal.any([signal1, signal2]);

expect(signal.aborted).toBe(false);

const reason2 = new Error('abort reason 2');
signal2[PropertySymbol.abort](reason2);
const reason1 = new Error('abort reason 1');
signal1[PropertySymbol.abort](reason1);

expect(signal.aborted).toBe(true);
expect(signal.reason).toBe(reason2);
});

it('Returns a signal that is already aborted when one of the supplied signals is already aborted.', () => {
const signal1 = new window.AbortSignal();
const signal2 = new window.AbortSignal();

const reason2 = new Error('abort reason 2');
signal2[PropertySymbol.abort](reason2);

const signal = window.AbortSignal.any([signal1, signal2]);

expect(signal.aborted).toBe(true);
expect(signal.reason).toBe(reason2);
});
});

describe('AbortSignal[Symbol.toStringTag]', () => {
it('Returns AbortSignal string.', () => {
const description = 'AbortSignal';
Expand Down
Loading