diff --git a/packages/service-worker/src/low_level.ts b/packages/service-worker/src/low_level.ts index 68febef187..d854e9647f 100644 --- a/packages/service-worker/src/low_level.ts +++ b/packages/service-worker/src/low_level.ts @@ -61,19 +61,10 @@ function errorObservable(message: string): Observable { * @experimental */ export class NgswCommChannel { - /** - * @internal - */ readonly worker: Observable; - /** - * @internal - */ readonly registration: Observable; - /** - * @internal - */ readonly events: Observable; constructor(private serviceWorker: ServiceWorkerContainer|undefined) { @@ -100,9 +91,6 @@ export class NgswCommChannel { } } - /** - * @internal - */ postMessage(action: string, payload: Object): Promise { return this.worker .pipe(take(1), tap((sw: ServiceWorker) => { @@ -114,38 +102,23 @@ export class NgswCommChannel { .then(() => undefined); } - /** - * @internal - */ postMessageWithStatus(type: string, payload: Object, nonce: number): Promise { const waitForStatus = this.waitForStatus(nonce); const postMessage = this.postMessage(type, payload); return Promise.all([waitForStatus, postMessage]).then(() => undefined); } - /** - * @internal - */ generateNonce(): number { return Math.round(Math.random() * 10000000); } - /** - * @internal - */ eventsOfType(type: T['type']): Observable { const filterFn = (event: TypedEvent): event is T => event.type === type; return this.events.pipe(filter(filterFn)); } - /** - * @internal - */ nextEventOfType(type: T['type']): Observable { return this.eventsOfType(type).pipe(take(1)); } - /** - * @internal - */ waitForStatus(nonce: number): Promise { return this.eventsOfType('STATUS') .pipe(filter(event => event.nonce === nonce), take(1), map(event => { diff --git a/packages/service-worker/src/push.ts b/packages/service-worker/src/push.ts index 8cb287d4f8..e18fc21c13 100644 --- a/packages/service-worker/src/push.ts +++ b/packages/service-worker/src/push.ts @@ -20,13 +20,27 @@ import {ERR_SW_NOT_SUPPORTED, NgswCommChannel, PushEvent} from './low_level'; */ @Injectable() export class SwPush { + /** + * Emits the payloads of the received push notification messages. + */ readonly messages: Observable; + + /** + * Emits the currently active + * [PushSubscription](https://developer.mozilla.org/en-US/docs/Web/API/PushSubscription) + * associated to the Service Worker registration or `null` if there is no subscription. + */ readonly subscription: Observable; + /** + * True if the Service Worker is enabled (supported by the browser and enabled via + * `ServiceWorkerModule`). + */ + get isEnabled(): boolean { return this.sw.isEnabled; } + // TODO(issue/24571): remove '!'. private pushManager !: Observable; - private subscriptionChanges: Subject = - new Subject(); + private subscriptionChanges = new Subject(); constructor(private sw: NgswCommChannel) { if (!sw.isEnabled) { @@ -34,22 +48,15 @@ export class SwPush { this.subscription = NEVER; return; } + this.messages = this.sw.eventsOfType('PUSH').pipe(map(message => message.data)); - this.pushManager = this.sw.registration.pipe( - map((registration: ServiceWorkerRegistration) => { return registration.pushManager; })); + this.pushManager = this.sw.registration.pipe(map(registration => registration.pushManager)); - const workerDrivenSubscriptions = this.pushManager.pipe( - switchMap((pm: PushManager) => pm.getSubscription().then(sub => { return sub; }))); + const workerDrivenSubscriptions = this.pushManager.pipe(switchMap(pm => pm.getSubscription())); this.subscription = merge(workerDrivenSubscriptions, this.subscriptionChanges); } - /** - * Returns true if the Service Worker is enabled (supported by the browser and enabled via - * ServiceWorkerModule). - */ - get isEnabled(): boolean { return this.sw.isEnabled; } - requestSubscription(options: {serverPublicKey: string}): Promise { if (!this.sw.isEnabled) { return Promise.reject(new Error(ERR_SW_NOT_SUPPORTED)); @@ -62,7 +69,7 @@ export class SwPush { } pushOptions.applicationServerKey = applicationServerKey; - return this.pushManager.pipe(switchMap((pm: PushManager) => pm.subscribe(pushOptions)), take(1)) + return this.pushManager.pipe(switchMap(pm => pm.subscribe(pushOptions)), take(1)) .toPromise() .then(sub => { this.subscriptionChanges.next(sub); diff --git a/packages/service-worker/src/update.ts b/packages/service-worker/src/update.ts index 92ba165fae..aee2090634 100644 --- a/packages/service-worker/src/update.ts +++ b/packages/service-worker/src/update.ts @@ -21,9 +21,22 @@ import {ERR_SW_NOT_SUPPORTED, NgswCommChannel, UpdateActivatedEvent, UpdateAvail */ @Injectable() export class SwUpdate { + /** + * Emits an `UpdateAvailableEvent` event whenever a new app version is available. + */ readonly available: Observable; + + /** + * Emits an `UpdateActivatedEvent` event whenever the app has been updated to a new version. + */ readonly activated: Observable; + /** + * True if the Service Worker is enabled (supported by the browser and enabled via + * `ServiceWorkerModule`). + */ + get isEnabled(): boolean { return this.sw.isEnabled; } + constructor(private sw: NgswCommChannel) { if (!sw.isEnabled) { this.available = NEVER; @@ -34,12 +47,6 @@ export class SwUpdate { this.activated = this.sw.eventsOfType('UPDATE_ACTIVATED'); } - /** - * Returns true if the Service Worker is enabled (supported by the browser and enabled via - * ServiceWorkerModule). - */ - get isEnabled(): boolean { return this.sw.isEnabled; } - checkForUpdate(): Promise { if (!this.sw.isEnabled) { return Promise.reject(new Error(ERR_SW_NOT_SUPPORTED));