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

Strategy unhandled rejection #3320

Merged
merged 5 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions packages/workbox-core/src/_private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

// We either expose defaults or we expose every named export.
import {allSettled} from './_private/allSettled.js';
import {assert} from './_private/assert.js';
import {cacheNames} from './_private/cacheNames.js';
import {cacheMatchIgnoreParams} from './_private/cacheMatchIgnoreParams.js';
Expand All @@ -25,6 +26,7 @@ import {WorkboxError} from './_private/WorkboxError.js';
import './_version.js';

export {
allSettled,
assert,
cacheMatchIgnoreParams,
cacheNames,
Expand Down
46 changes: 46 additions & 0 deletions packages/workbox-core/src/_private/allSettled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import '../_version.js';

export interface PromiseResolution<T> {
status: 'fulfilled';
value: T;
}

export interface PromiseRejection {
status: 'rejected';
reason: unknown;
}

export type PromiseResult<T> = PromiseResolution<T> | PromiseRejection;

/**
* Promise.allSettled polyfill based on
*
* https://github.com/es-shims/Promise.allSettled/blob/main/implementation.js
*
* which is (c) 2019 Jordan Harband and used under the terms of the MIT license.
*
* @private
*/
function allSettled<T>(
joshkel marked this conversation as resolved.
Show resolved Hide resolved
iterable: Iterable<Promise<T>>,
): Promise<PromiseResult<T>[]> {
const values = Array.from(iterable);
return Promise.all(
values.map(function (item) {
const onFulfill = function (value: T) {
return {status: 'fulfilled' as const, value: value};
};
const onReject = function (reason: unknown) {
return {status: 'rejected' as const, reason: reason};
};
const itemPromise = Promise.resolve(item);
try {
return itemPromise.then(onFulfill, onReject);
} catch (e) {
return Promise.reject(e);
}
}),
);
}

export {allSettled};
20 changes: 14 additions & 6 deletions packages/workbox-strategies/src/StrategyHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
https://opensource.org/licenses/MIT.
*/

import {
allSettled,
PromiseRejection,
} from 'workbox-core/_private/allSettled.js';
import {assert} from 'workbox-core/_private/assert.js';
import {cacheMatchIgnoreParams} from 'workbox-core/_private/cacheMatchIgnoreParams.js';
import {Deferred} from 'workbox-core/_private/Deferred.js';
Expand All @@ -29,7 +33,7 @@ function toRequest(input: RequestInfo) {
}

/**
* A class created every time a Strategy instance instance calls
* A class created every time a Strategy instance calls
* {@link workbox-strategies.Strategy~handle} or
* {@link workbox-strategies.Strategy~handleAll} that wraps all fetch and
* cache actions around plugin callbacks and keeps track of when the strategy
Expand Down Expand Up @@ -534,7 +538,7 @@ class StrategyHandler {
/**
* Adds a promise to the
* [extend lifetime promises]{@link https://w3c.github.io/ServiceWorker/#extendableevent-extend-lifetime-promises}
* of the event event associated with the request being handled (usually a
* of the event associated with the request being handled (usually a
* `FetchEvent`).
*
* Note: you can await
Expand All @@ -556,13 +560,17 @@ class StrategyHandler {
*
* Note: any work done after `doneWaiting()` settles should be manually
* passed to an event's `waitUntil()` method (not this handler's
* `waitUntil()` method), otherwise the service worker thread my be killed
* `waitUntil()` method), otherwise the service worker thread may be killed
* prior to your work completing.
*/
async doneWaiting(): Promise<void> {
let promise;
while ((promise = this._extendLifetimePromises.shift())) {
await promise;
while (this._extendLifetimePromises.length) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose the while loop needs to be retained in case additional promises are added to the array while awaiting the ones that were removed during the first iteration?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. (That was my rationale, at least.)

const promises = this._extendLifetimePromises.splice(0);
const result = await allSettled(promises);
joshkel marked this conversation as resolved.
Show resolved Hide resolved
const firstRejection = result.find((i) => i.status === 'rejected');
if (firstRejection) {
throw (firstRejection as PromiseRejection).reason;
}
}
}

Expand Down