Skip to content

Commit

Permalink
fix(net): Allow preventDefault() on retry event (#8058)
Browse files Browse the repository at this point in the history
Fixes #8041
  • Loading branch information
avelad committed Feb 17, 2025
1 parent fc1b96a commit a514199
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/net/networking_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -716,8 +716,16 @@ shaka.net.NetworkingEngine = class extends shaka.util.FakeEventTarget {
if (error.severity == shaka.util.Error.Severity.RECOVERABLE) {
const data = (new Map()).set('error', error);
const event = new shaka.util.FakeEvent('retry', data);
// A user can call preventDefault() on a cancelable event.
event.cancelable = true;
this.dispatchEvent(event);

if (event.defaultPrevented) {
// If the caller uses preventDefault() on the 'retry' event, don't
// retry any more.
throw error;
}

// Move to the next URI.
index = (index + 1) % request.uris.length;
return this.send_(
Expand Down
26 changes: 26 additions & 0 deletions test/net/networking_engine_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ describe('NetworkingEngine', /** @suppress {accessControls} */ () => {
afterEach(() => {
shaka.net.NetworkingEngine.unregisterScheme('resolve');
shaka.net.NetworkingEngine.unregisterScheme('reject');
networkingEngine.destroy();
});

afterAll(() => {
Expand Down Expand Up @@ -142,6 +143,31 @@ describe('NetworkingEngine', /** @suppress {accessControls} */ () => {
expect(rejectScheme).toHaveBeenCalledTimes(3);
});

it('allow abort retry', async () => {
const request = createRequest('reject://foo', {
maxAttempts: 2,
baseDelay: 0,
backoffFactor: 0,
fuzzFactor: 0,
timeout: 0,
stallTimeout: 0,
connectionTimeout: 0,
});
rejectScheme.and.callFake(() => {
if (rejectScheme.calls.count() == 1) {
return shaka.util.AbortableOperation.failed(error);
} else {
return shaka.util.AbortableOperation.completed(createResponse());
}
});
networkingEngine.addEventListener('retry', (event) => {
event.preventDefault();
});
await expectAsync(networkingEngine.request(requestType, request).promise)
.toBeRejected();
expect(rejectScheme).toHaveBeenCalledTimes(1);
});

describe('backoff', () => {
const baseDelay = 200;
const realRandom = Math.random;
Expand Down

0 comments on commit a514199

Please sign in to comment.