From 447e7e901bad614ce119648c8eab2cbf1954f938 Mon Sep 17 00:00:00 2001 From: David Lechner Date: Thu, 28 Jan 2021 14:00:09 -0600 Subject: [PATCH] pass registration to reload action --- src/actions/app.ts | 8 +++++--- src/sagas/app.test.ts | 7 +------ src/sagas/app.ts | 13 +++---------- src/sagas/notification.ts | 11 ++++++++--- 4 files changed, 17 insertions(+), 22 deletions(-) diff --git a/src/actions/app.ts b/src/actions/app.ts index e4143d25..48e370d7 100644 --- a/src/actions/app.ts +++ b/src/actions/app.ts @@ -26,11 +26,13 @@ export enum AppActionType { } /** Action that requests the app to reload. */ -export type AppReloadAction = Action; +export type AppReloadAction = Action & { + registration: ServiceWorkerRegistration; +}; /** Creates an action that requests the app to reload. */ -export function reload(): AppReloadAction { - return { type: AppActionType.Reload }; +export function reload(registration: ServiceWorkerRegistration): AppReloadAction { + return { type: AppActionType.Reload, registration }; } /** Action that indicates the app has just started. */ diff --git a/src/sagas/app.test.ts b/src/sagas/app.test.ts index 44fa538e..6443a394 100644 --- a/src/sagas/app.test.ts +++ b/src/sagas/app.test.ts @@ -13,11 +13,6 @@ test('reload', async () => { unregister: jest.fn(), }; - // @ts-expect-error: navigator.serviceWorker is not implemented in JSDOM - navigator.serviceWorker = { - getRegistrations: jest.fn().mockResolvedValue([registration]), - }; - // @ts-expect-error: JSDOM implementation of location.reload() causes error delete window.location; // @ts-expect-error: JSDOM implementation of location.reload() causes error @@ -25,7 +20,7 @@ test('reload', async () => { reload: jest.fn(), }; - saga.put(reload()); + saga.put(reload(registration as ServiceWorkerRegistration)); // yield to allow generators to complete await delay(0); diff --git a/src/sagas/app.ts b/src/sagas/app.ts index 192b92de..8e8ced7a 100644 --- a/src/sagas/app.ts +++ b/src/sagas/app.ts @@ -2,17 +2,10 @@ // Copyright (c) 2021 The Pybricks Authors import { call, takeEvery } from 'typed-redux-saga/macro'; -import { AppActionType } from '../actions/app'; - -function* reload(): Generator { - // unregister the service worker so that when the page reloads, it uses - // the new version - const registrations = yield* call(() => navigator.serviceWorker.getRegistrations()); - - for (const r of registrations) { - yield* call(() => r.unregister()); - } +import { AppActionType, AppReloadAction } from '../actions/app'; +function* reload(action: AppReloadAction): Generator { + yield* call(() => action.registration.unregister()); location.reload(); } diff --git a/src/sagas/notification.ts b/src/sagas/notification.ts index d976198b..30e637c2 100644 --- a/src/sagas/notification.ts +++ b/src/sagas/notification.ts @@ -33,7 +33,10 @@ import { } from '../actions/lwp3-bootloader'; import { MpyActionType, MpyDidFailToCompileAction } from '../actions/mpy'; import { NotificationActionType, NotificationAddAction } from '../actions/notification'; -import { ServiceWorkerActionType } from '../actions/service-worker'; +import { + ServiceWorkerAction, + ServiceWorkerActionType, +} from '../actions/service-worker'; import Notification from '../components/Notification'; import UnexpectedErrorNotification from '../components/UnexpectedErrorNotification'; import { MessageId } from '../components/notification-i18n'; @@ -323,7 +326,9 @@ function* addNotification(action: NotificationAddAction): Generator { }); } -function* showServiceWorkerUpdate(): Generator { +function* showServiceWorkerUpdate( + updateAction: ServiceWorkerAction, +): Generator { const ch = channel>(); const action = dispatchAction( MessageId.ServiceWorkerUpdateAction, @@ -343,7 +348,7 @@ function* showServiceWorkerUpdate(): Generator { yield* take(ch); - yield* put(reload()); + yield* put(reload(updateAction.registration)); } export default function* (): Generator {