Skip to content

Commit

Permalink
Provides minor adjustments to the "getWorkerByRegistration" refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Aug 10, 2020
1 parent 050eafe commit 00eac3a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
20 changes: 13 additions & 7 deletions src/setupWorker/start/utils/getWorkerByRegistration.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
/**
* Attempts to resolve a Service Worker instance from any of its states:
* active, installing, or waiting.
* Attempts to resolve a Service Worker instance from a given registration,
* regardless of its state (active, installing, waiting).
*/
export const getWorkerByRegistration = (
registration: ServiceWorkerRegistration,
absoluteWorkerUrl: string,
): ServiceWorker | null => {
return (
[registration.active, registration.installing, registration.waiting].find(
(worker) => worker?.scriptURL === absoluteWorkerUrl,
) || null
)
const allStates = [
registration.active,
registration.installing,
registration.waiting,
]
const existingStates = allStates.filter(Boolean) as ServiceWorker[]
const mockWorker = existingStates.find((worker) => {
return worker.scriptURL === absoluteWorkerUrl
})

return mockWorker || null
}
12 changes: 7 additions & 5 deletions src/setupWorker/start/utils/getWorkerInstance.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getWorkerByRegistration } from './getWorkerByRegistration'
import { until } from '@open-draft/until'
import { getWorkerByRegistration } from './getWorkerByRegistration'
import { ServiceWorkerInstanceTuple } from '../../glossary'
import { getAbsoluteWorkerUrl } from '../../../utils/getAbsoluteWorkerUrl'

Expand All @@ -16,9 +16,9 @@ export const getWorkerInstance = async (

const [, mockRegistrations] = await until(async () => {
const registrations = await navigator.serviceWorker.getRegistrations()
return registrations.filter((registration) =>
getWorkerByRegistration(registration, absoluteWorkerUrl),
)
return registrations.filter((registration) => {
return getWorkerByRegistration(registration, absoluteWorkerUrl)
})
})

if (!navigator.serviceWorker.controller && mockRegistrations.length > 0) {
Expand All @@ -31,7 +31,7 @@ export const getWorkerInstance = async (
location.reload()
}

const existingRegistration = mockRegistrations[0]
const [existingRegistration] = mockRegistrations

if (existingRegistration) {
// Update existing service worker to ensure it's up-to-date
Expand All @@ -47,6 +47,8 @@ export const getWorkerInstance = async (
async () => {
const registration = await navigator.serviceWorker.register(url, options)
return [
// Compare existing worker registration by its worker URL,
// to prevent irrelevant workers to resolve here (such as Codesandbox worker).
getWorkerByRegistration(registration, absoluteWorkerUrl),
registration,
]
Expand Down

0 comments on commit 00eac3a

Please sign in to comment.