From 77942cc690a4721c73cde3f6a16e6fb0caa1be2b Mon Sep 17 00:00:00 2001 From: George Kalpakas Date: Mon, 2 Apr 2018 22:12:02 +0300 Subject: [PATCH] refactor(service-worker): simplify/improve `NgswCommChannel` typings (#23138) PR Close #23138 --- packages/service-worker/src/low_level.ts | 62 +++++++++++------------- packages/service-worker/src/push.ts | 4 +- 2 files changed, 31 insertions(+), 35 deletions(-) diff --git a/packages/service-worker/src/low_level.ts b/packages/service-worker/src/low_level.ts index 98fb3437ba..68febef187 100644 --- a/packages/service-worker/src/low_level.ts +++ b/packages/service-worker/src/low_level.ts @@ -33,6 +33,14 @@ export interface UpdateActivatedEvent { current: {hash: string, appData?: Object}; } +/** + * An event emitted when a `PushEvent` is received by the service worker. + */ +export interface PushEvent { + type: 'PUSH'; + data: any; +} + export type IncomingEvent = UpdateAvailableEvent | UpdateActivatedEvent; export interface TypedEvent { type: string; } @@ -72,30 +80,23 @@ export class NgswCommChannel { if (!serviceWorker) { this.worker = this.events = this.registration = errorObservable(ERR_SW_NOT_SUPPORTED); } else { - const controllerChangeEvents = - >(fromEvent(serviceWorker, 'controllerchange')); - const controllerChanges = >( - controllerChangeEvents.pipe(map(() => serviceWorker.controller))); + const controllerChangeEvents = fromEvent(serviceWorker, 'controllerchange'); + const controllerChanges = controllerChangeEvents.pipe(map(() => serviceWorker.controller)); + const currentController = defer(() => of (serviceWorker.controller)); + const controllerWithChanges = concat(currentController, controllerChanges); - const currentController = - >(defer(() => of (serviceWorker.controller))); - - const controllerWithChanges = - >(concat(currentController, controllerChanges)); - this.worker = >( - controllerWithChanges.pipe(filter((c: ServiceWorker) => !!c))); + this.worker = controllerWithChanges.pipe(filter(c => !!c)); this.registration = >( this.worker.pipe(switchMap(() => serviceWorker.getRegistration()))); - const rawEvents = fromEvent(serviceWorker, 'message'); - - const rawEventPayload = rawEvents.pipe(map((event: MessageEvent) => event.data)); - const eventsUnconnected = - (rawEventPayload.pipe(filter((event: Object) => !!event && !!(event as any)['type']))); + const rawEvents = fromEvent(serviceWorker, 'message'); + const rawEventPayload = rawEvents.pipe(map(event => event.data)); + const eventsUnconnected = rawEventPayload.pipe(filter(event => event && event.type)); const events = eventsUnconnected.pipe(publish()) as ConnectableObservable; - this.events = events; events.connect(); + + this.events = events; } } @@ -130,19 +131,16 @@ export class NgswCommChannel { /** * @internal */ - // TODO(i): the typings and casts in this method are wonky, we should revisit it and make the - // types flow correctly - eventsOfType(type: string): Observable { - return >this.events.pipe(filter((event) => { return event.type === type; })); + eventsOfType(type: T['type']): Observable { + const filterFn = (event: TypedEvent): event is T => event.type === type; + return this.events.pipe(filter(filterFn)); } /** * @internal */ - // TODO(i): the typings and casts in this method are wonky, we should revisit it and make the - // types flow correctly - nextEventOfType(type: string): Observable { - return >(this.eventsOfType(type).pipe(take(1))); + nextEventOfType(type: T['type']): Observable { + return this.eventsOfType(type).pipe(take(1)); } /** @@ -150,14 +148,12 @@ export class NgswCommChannel { */ waitForStatus(nonce: number): Promise { return this.eventsOfType('STATUS') - .pipe( - filter((event: StatusEvent) => event.nonce === nonce), take(1), - map((event: StatusEvent) => { - if (event.status) { - return undefined; - } - throw new Error(event.error !); - })) + .pipe(filter(event => event.nonce === nonce), take(1), map(event => { + if (event.status) { + return undefined; + } + throw new Error(event.error !); + })) .toPromise(); } diff --git a/packages/service-worker/src/push.ts b/packages/service-worker/src/push.ts index 2a9f244c79..8cb287d4f8 100644 --- a/packages/service-worker/src/push.ts +++ b/packages/service-worker/src/push.ts @@ -10,7 +10,7 @@ import {Injectable} from '@angular/core'; import {NEVER, Observable, Subject, merge} from 'rxjs'; import {map, switchMap, take} from 'rxjs/operators'; -import {ERR_SW_NOT_SUPPORTED, NgswCommChannel} from './low_level'; +import {ERR_SW_NOT_SUPPORTED, NgswCommChannel, PushEvent} from './low_level'; /** @@ -34,7 +34,7 @@ export class SwPush { this.subscription = NEVER; return; } - this.messages = this.sw.eventsOfType('PUSH').pipe(map((message: any) => message.data)); + this.messages = this.sw.eventsOfType('PUSH').pipe(map(message => message.data)); this.pushManager = this.sw.registration.pipe( map((registration: ServiceWorkerRegistration) => { return registration.pushManager; }));