refactor(service-worker): avoid unnecessarily creating Promises in Driver#deleteAllCaches() (#40234)

This commit refactors `Driver#deleteAllCaches()` to use `Array#map()`
instead of `Array#reduce()` for running async operations in parallel.
This allows avoiding having to recursively wrap Promises with
`Promise.all()`.

PR Close #40234
This commit is contained in:
George Kalpakas 2021-01-08 13:59:34 +02:00 committed by atscott
parent b953a0c5a5
commit 2cf6fa2e9c

View File

@ -722,18 +722,16 @@ export class Driver implements Debuggable, UpdateSource {
} }
private async deleteAllCaches(): Promise<void> { private async deleteAllCaches(): Promise<void> {
await (await this.scope.caches.keys()) const cacheNames = await this.scope.caches.keys();
// The Chrome debugger is not able to render the syntax properly when the const ownCacheNames =
// code contains backticks. This is a known issue in Chrome and they have an cacheNames
// open [issue](https://bugs.chromium.org/p/chromium/issues/detail?id=659515) for that. // The Chrome debugger is not able to render the syntax properly when the
// As a work-around for the time being, we can use \\ ` at the end of the line. // code contains backticks. This is a known issue in Chrome and they have an
.filter(key => key.startsWith(`${this.adapter.cacheNamePrefix}:`)) // ` // open [issue](https://bugs.chromium.org/p/chromium/issues/detail?id=659515) for that.
.reduce(async (previous, key) => { // As a work-around for the time being, we can use \\ ` at the end of the line.
await Promise.all([ .filter(name => name.startsWith(`${this.adapter.cacheNamePrefix}:`)); // `
previous,
this.scope.caches.delete(key), await Promise.all(ownCacheNames.map(name => this.scope.caches.delete(name)));
]);
}, Promise.resolve());
} }
/** /**