Skip to content

Commit

Permalink
Replaced all page sw.active calls
Browse files Browse the repository at this point in the history
* Replaced all `ServiceWorker.active` with a new getAvailableServiceWorker helper method.
* Updated in place `availableWorker` to use this.
  • Loading branch information
jkasten2 committed Feb 17, 2021
1 parent 8d69de5 commit 42bec5f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
10 changes: 10 additions & 0 deletions src/helpers/page/ServiceWorkerUtilHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,14 @@ export default class ServiceWorkerUtilHelper {
return null;
}
}

// A ServiceWorkerRegistration will have a ServiceWorker in 1 of 3 states, get which ever is available.
static getAvailableServiceWorker(registration: ServiceWorkerRegistration): ServiceWorker {
const availableWorker = registration.active || registration.installing || registration.waiting;
// This should never throw unless ServiceWorkerRegistration is pointing to a worker that is completely gone.
if (!availableWorker) {
throw new Error("Could not find an available ServiceWorker instance!");
}
return availableWorker;
}
}
16 changes: 5 additions & 11 deletions src/libraries/WorkerMessenger.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { InvalidArgumentError, InvalidArgumentReason } from '../errors/InvalidArgumentError';
import SdkEnvironment from '../managers/SdkEnvironment';
import { ServiceWorkerActiveState } from '../helpers/ServiceWorkerHelper';
import { WindowEnvironmentKind } from '../models/WindowEnvironmentKind';

import { Serializable } from '../models/Serializable';
import Environment from '../Environment';
import Log from './Log';
import { ContextSWInterface } from '../models/ContextSW';
import ServiceWorkerUtilHelper from '../helpers/page/ServiceWorkerUtilHelper';

/**
* NOTE: This file contains a mix of code that runs in ServiceWorker and Page contexts
Expand Down Expand Up @@ -146,20 +146,14 @@ export class WorkerMessenger {
public async directPostMessageToSW(command: WorkerMessengerCommand, payload?: WorkerMessengerPayload): Promise<void> {
Log.debug(`[Worker Messenger] [Page -> SW] Direct command '${command.toString()}' to service worker.`);

const serviceWorker = await this.context.serviceWorkerManager.getRegistration();
if (!serviceWorker) {
const workerRegistration = await this.context.serviceWorkerManager.getRegistration();
if (!workerRegistration) {
Log.error("`[Worker Messenger] [Page -> SW] Could not get ServiceWorkerRegistration to postMessage!");
return;
}

// ServiceWorker will be in 1 of 3 states; The postMessage payload will still arrive at the SW even if it isn't active
const availableWorker = serviceWorker.active || serviceWorker.waiting || serviceWorker.installing;
if (!availableWorker) {
Log.error("`[Worker Messenger] [Page -> SW] ServiceWorkerRegistration found but it could find an available ServiceWorker instance to postMessage!");
return;
}

availableWorker.postMessage({
// The postMessage payload will still arrive at the SW even if it isn't active yet.
ServiceWorkerUtilHelper.getAvailableServiceWorker(workerRegistration).postMessage({
command: command,
payload: payload
});
Expand Down
8 changes: 3 additions & 5 deletions src/managers/ServiceWorkerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,14 @@ export class ServiceWorkerManager {

// Get the file name of the active ServiceWorker
private static activeSwFileName(workerRegistration: ServiceWorkerRegistration): string | null {
if (!workerRegistration.active)
return null;

const workerScriptPath = new URL(workerRegistration.active.scriptURL).pathname;
const serviceWorker = ServiceWorkerUtilHelper.getAvailableServiceWorker(workerRegistration);
const workerScriptPath = new URL(serviceWorker.scriptURL).pathname;
const swFileName = new Path(workerScriptPath).getFileName();

// If the current service worker is Akamai's
if (swFileName == "akam-sw.js") {
// Check if its importing a ServiceWorker under it's "othersw" query param
const searchParams = new URLSearchParams(new URL(workerRegistration.active.scriptURL).search);
const searchParams = new URLSearchParams(new URL(serviceWorker.scriptURL).search);
const importedSw = searchParams.get("othersw");
if (importedSw) {
Log.debug("Found a ServiceWorker under Akamai's akam-sw.js?othersw=", importedSw);
Expand Down

0 comments on commit 42bec5f

Please sign in to comment.