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

Adds in explicit self/window in a few places #2274

Merged
merged 1 commit into from
Nov 7, 2019
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
4 changes: 2 additions & 2 deletions packages/workbox-core/src/_private/cacheWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ const putWrapper = async ({
return;
}

const cache = await caches.open(cacheName);
const cache = await self.caches.open(cacheName);

const updatePlugins = pluginUtils.filter(
plugins, pluginEvents.CACHE_DID_UPDATE);
Expand Down Expand Up @@ -155,7 +155,7 @@ const matchWrapper = async ({
matchOptions,
plugins = [],
} : MatchWrapperOptions) : Promise<Response|undefined> => {
const cache = await caches.open(cacheName);
const cache = await self.caches.open(cacheName);

const effectiveRequest = await _getEffectiveRequest({
plugins, request, mode: 'read'});
Expand Down
2 changes: 1 addition & 1 deletion packages/workbox-core/src/clientsClaim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ declare var self: ServiceWorkerGlobalScope;
* @alias workbox.core.clientsClaim
*/
export const clientsClaim = () => {
addEventListener('activate', () => self.clients.claim());
self.addEventListener('activate', () => self.clients.claim());
};
2 changes: 1 addition & 1 deletion packages/workbox-core/src/skipWaiting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ declare var self: ServiceWorkerGlobalScope;
export const skipWaiting = () => {
// We need to explicitly call `self.skipWaiting()` here because we're
// shadowing `skipWaiting` with this local function.
addEventListener('install', () => self.skipWaiting());
self.addEventListener('install', () => self.skipWaiting());
};
2 changes: 1 addition & 1 deletion packages/workbox-expiration/src/CacheExpiration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class CacheExpiration {
minTimestamp, this._maxEntries);

// Delete URLs from the cache
const cache = await caches.open(this._cacheName);
const cache = await self.caches.open(this._cacheName);
for (const url of urlsExpired) {
await cache.delete(url);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/workbox-expiration/src/ExpirationPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ class ExpirationPlugin implements WorkboxPlugin {
// Do this one at a time instead of all at once via `Promise.all()` to
// reduce the chance of inconsistency if a promise rejects.
for (const [cacheName, cacheExpiration] of this._cacheExpirations) {
await caches.delete(cacheName);
await self.caches.delete(cacheName);
await cacheExpiration.delete();
}

Expand Down
6 changes: 3 additions & 3 deletions packages/workbox-precaching/src/PrecacheController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class PrecacheController {
const toBePrecached: {cacheKey: string, url: string}[] = [];
const alreadyPrecached: string[] = [];

const cache = await caches.open(this._cacheName);
const cache = await self.caches.open(this._cacheName);
const alreadyCachedRequests = await cache.keys();
const existingCacheKeys = new Set(alreadyCachedRequests.map(
(request) => request.url));
Expand Down Expand Up @@ -189,7 +189,7 @@ class PrecacheController {
* @return {Promise<workbox.precaching.CleanupResult>}
*/
async activate() {
const cache = await caches.open(this._cacheName);
const cache = await self.caches.open(this._cacheName);
const currentlyCachedRequests = await cache.keys();
const expectedCacheKeys = new Set(this._urlsToCacheKeys.values());

Expand Down Expand Up @@ -352,7 +352,7 @@ class PrecacheController {
const url = request instanceof Request ? request.url : request;
const cacheKey = this.getCacheKeyForURL(url);
if (cacheKey) {
const cache = await caches.open(this._cacheName);
const cache = await self.caches.open(this._cacheName);
return cache.match(cacheKey);
}
return undefined;
Expand Down
2 changes: 1 addition & 1 deletion packages/workbox-precaching/src/cleanupOutdatedCaches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import './_version.js';
*/
export const cleanupOutdatedCaches = () => {
// See https://github.com/Microsoft/TypeScript/issues/28357#issuecomment-436484705
addEventListener('activate', ((event: ExtendableEvent) => {
self.addEventListener('activate', ((event: ExtendableEvent) => {
const cacheName = cacheNames.getPrecacheName();

event.waitUntil(deleteOutdatedCaches(cacheName).then((cachesDeleted) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/workbox-precaching/src/precache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const precache = (entries: Array<PrecacheEntry|string>) => {
// method is called multiple times) because event listeners are implemented
// as a set, where each listener must be unique.
// See https://github.com/Microsoft/TypeScript/issues/28357#issuecomment-436484705
addEventListener('install', installListener as EventListener);
addEventListener('activate', activateListener as EventListener);
self.addEventListener('install', installListener as EventListener);
self.addEventListener('activate', activateListener as EventListener);
}
};
4 changes: 2 additions & 2 deletions packages/workbox-precaching/src/utils/addFetchListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const addFetchListener = ({
const cacheName = cacheNames.getPrecacheName();

// See https://github.com/Microsoft/TypeScript/issues/28357#issuecomment-436484705
addEventListener('fetch', ((event: FetchEvent) => {
self.addEventListener('fetch', ((event: FetchEvent) => {
const precachedURL = getCacheKeyForURL(event.request.url, {
cleanURLs,
directoryIndex,
Expand All @@ -72,7 +72,7 @@ export const addFetchListener = ({
return;
}

let responsePromise = caches.open(cacheName).then((cache) => {
let responsePromise = self.caches.open(cacheName).then((cache) => {
return cache.match(precachedURL);
}).then((cachedResponse) => {
if (cachedResponse) {
Expand Down
4 changes: 2 additions & 2 deletions packages/workbox-precaching/src/utils/deleteOutdatedCaches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const SUBSTRING_TO_FIND = '-precache-';
const deleteOutdatedCaches = async (
currentPrecacheName: string,
substringToFind:string = SUBSTRING_TO_FIND) => {
const cacheNames = await caches.keys();
const cacheNames = await self.caches.keys();

const cacheNamesToDelete = cacheNames.filter((cacheName) => {
return cacheName.includes(substringToFind) &&
Expand All @@ -44,7 +44,7 @@ const deleteOutdatedCaches = async (
});

await Promise.all(
cacheNamesToDelete.map((cacheName) => caches.delete(cacheName)));
cacheNamesToDelete.map((cacheName) => self.caches.delete(cacheName)));

return cacheNamesToDelete;
};
Expand Down
2 changes: 1 addition & 1 deletion packages/workbox-window/src/Workbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class Workbox extends WorkboxEventTarget {
}

if (!immediate && document.readyState !== 'complete') {
await new Promise((res) => addEventListener('load', res));
await new Promise((res) => window.addEventListener('load', res));
jeffposnick marked this conversation as resolved.
Show resolved Hide resolved
}

// Set this flag to true if any service worker was controlling the page
Expand Down