Tiny refactor of sample caching service worker (#440)
* use Object.values function * make Set instead of using indexOf
This commit is contained in:
Родитель
124c26e9f4
Коммит
8597e80eaf
|
@ -33,16 +33,13 @@ self.addEventListener('activate', function(event) {
|
||||||
// Delete all caches that aren't named in CURRENT_CACHES.
|
// Delete all caches that aren't named in CURRENT_CACHES.
|
||||||
// While there is only one cache in this example, the same logic will handle the case where
|
// While there is only one cache in this example, the same logic will handle the case where
|
||||||
// there are multiple versioned caches.
|
// there are multiple versioned caches.
|
||||||
var expectedCacheNames = Object.keys(CURRENT_CACHES).map(function(key) {
|
var expectedCacheNamesSet = new Set(Object.values(CURRENT_CACHES));
|
||||||
return CURRENT_CACHES[key];
|
|
||||||
});
|
|
||||||
|
|
||||||
event.waitUntil(
|
event.waitUntil(
|
||||||
caches.keys().then(function(cacheNames) {
|
caches.keys().then(function(cacheNames) {
|
||||||
return Promise.all(
|
return Promise.all(
|
||||||
cacheNames.map(function(cacheName) {
|
cacheNames.map(function(cacheName) {
|
||||||
if (expectedCacheNames.indexOf(cacheName) === -1) {
|
if (!expectedCacheNamesSet.has(cacheName)) {
|
||||||
// If this cache name isn't present in the array of "expected" cache names, then delete it.
|
// If this cache name isn't present in the set of "expected" cache names, then delete it.
|
||||||
console.log('Deleting out of date cache:', cacheName);
|
console.log('Deleting out of date cache:', cacheName);
|
||||||
return caches.delete(cacheName);
|
return caches.delete(cacheName);
|
||||||
}
|
}
|
||||||
|
|
Загрузка…
Ссылка в новой задаче