Skip to content

Commit

Permalink
Fix bug of queue stalling on aborted fetch (not actually seen happening)
Browse files Browse the repository at this point in the history
  • Loading branch information
robatwilliams committed Jan 11, 2024
1 parent 0410a92 commit 4186c0e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/functions/ConcurrencyLimitedFetch.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export default class ConcurrencyLimitedFetch {

if (task.args.options.signal.aborted) {
task.reject(task.args.options.signal.reason);
this._process();
return;
}

Expand Down
23 changes: 23 additions & 0 deletions src/functions/ConcurrencyLimitedFetch.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,29 @@ describe('ConcurrencyLimitedFetch', () => {
await assert.rejects(aborted, 'AbortError: This operation was aborted');
assert.strictEqual(fetch.mock.callCount(), 10);
});

it('fetches the next queued when encountering an aborted in the queue', async (t) => {
const fetcher = new ConcurrencyLimitedFetch();

t.mock.method(global, 'fetch', () => new Promise(() => {}));
for (let i = 0; i < 9; i++) {
fetcher.fetch('', makeFetchOptions());
}

// Will resolve on next flush
t.mock.method(global, 'fetch', () => Promise.resolve());
fetcher.fetch('', makeFetchOptions());

const abortController = new AbortController();
assert.rejects(fetcher.fetch('', makeFetchOptions({ abortController })));
abortController.abort();

// Will be fetched when 10th resolves
fetcher.fetch('', makeFetchOptions());

await flushPromises();
assert.strictEqual(fetch.mock.callCount(), 2);
});
});

function makeFetchOptions({ abortController } = {}) {
Expand Down

0 comments on commit 4186c0e

Please sign in to comment.