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 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
16 changes: 10 additions & 6 deletions packages/workbox-strategies/src/StrategyHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,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 +534,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 +556,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 Promise.allSettled(promises);
const firstRejection = result.find((i) => i.status === 'rejected');
if (firstRejection) {
throw (firstRejection as PromiseRejectedResult).reason;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"composite": true,
"declaration": true,
"lib": ["es2017", "webworker"],
"lib": ["es2020", "webworker"],
"module": "esnext",
"moduleResolution": "node",
"noFallthroughCasesInSwitch": true,
Expand Down