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 3 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
39 changes: 39 additions & 0 deletions packages/happy-dom/src/fetch/AbortSignal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 AbortSignal.abort(signal.reason);
}
}

const anySignal = new AbortSignal();
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;
}
}
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 AbortSignal();
const signal2 = new AbortSignal();
const signal = 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 AbortSignal();
const signal2 = new AbortSignal();

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

const signal = 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