Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions goldens/public-api/service-worker/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ export class SwUpdate {
// @deprecated
readonly available: Observable<UpdateAvailableEvent>;
checkForUpdate(): Promise<boolean>;
readonly hashes: Observable<{
latest: string;
current: string;
}>;
get isEnabled(): boolean;
readonly unrecoverable: Observable<UnrecoverableStateEvent>;
readonly versionUpdates: Observable<VersionEvent>;
Expand Down
6 changes: 3 additions & 3 deletions goldens/size-tracking/aio-payloads.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
"aio-local": {
"uncompressed": {
"runtime": 4325,
"main": 470131,
"main": 470791,
"polyfills": 33836,
"styles": 74561,
"light-theme": 92890,
"dark-theme": 93036
"light-theme": 92905,
"dark-theme": 93051
}
}
}
4 changes: 4 additions & 0 deletions packages/service-worker/src/low_level.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ export interface PushEvent {

export type IncomingEvent = UpdateActivatedEvent|UnrecoverableStateEvent|VersionEvent;

export type HashUpdateEvent = {
type: 'HASH_UPDATE'; hash: string;
};

export interface TypedEvent {
type: string;
}
Expand Down
22 changes: 20 additions & 2 deletions packages/service-worker/src/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
*/

import {Injectable} from '@angular/core';
import {NEVER, Observable} from 'rxjs';
import {BehaviorSubject, merge, NEVER, Observable, ReplaySubject} from 'rxjs';
import {filter, map} from 'rxjs/operators';

import {ERR_SW_NOT_SUPPORTED, NgswCommChannel, UnrecoverableStateEvent, UpdateActivatedEvent, UpdateAvailableEvent, VersionEvent, VersionReadyEvent} from './low_level';
import {ERR_SW_NOT_SUPPORTED, HashUpdateEvent, NgswCommChannel, UnrecoverableStateEvent, UpdateActivatedEvent, UpdateAvailableEvent, VersionEvent, VersionReadyEvent} from './low_level';

export type Writable<T> = {
-readonly[K in keyof T]: T[K];
};


/**
Expand Down Expand Up @@ -63,6 +66,12 @@ export class SwUpdate {
*/
readonly unrecoverable: Observable<UnrecoverableStateEvent>;

/**
* Emits a new value each time the hash of the manifest changes.
* When the `latest` hash is different from the `current`, it represents an available version
*/
readonly hashes: Observable<{latest: string, current: string}>;

/**
* True if the Service Worker is enabled (supported by the browser and enabled via
* `ServiceWorkerModule`).
Expand All @@ -77,6 +86,7 @@ export class SwUpdate {
this.available = NEVER;
this.activated = NEVER;
this.unrecoverable = NEVER;
this.hashes = NEVER;
return;
}
this.versionUpdates = this.sw.eventsOfType<VersionEvent>([
Expand All @@ -94,6 +104,14 @@ export class SwUpdate {
})));
this.activated = this.sw.eventsOfType<UpdateActivatedEvent>('UPDATE_ACTIVATED');
this.unrecoverable = this.sw.eventsOfType<UnrecoverableStateEvent>('UNRECOVERABLE_STATE');

this.hashes = new ReplaySubject(1);
merge(
this.sw.eventsOfType<HashUpdateEvent>('HASH_UPDATE')
.pipe(map(({hash}) => ({latest: hash, current: hash}))),
this.sw.eventsOfType<VersionReadyEvent>('VERSION_READY')
.pipe(map(evt => ({latest: evt.latestVersion.hash, current: evt.currentVersion.hash}))))
.subscribe((hashes) => (this.hashes as BehaviorSubject<any>).next(hashes));
}

/**
Expand Down
24 changes: 24 additions & 0 deletions packages/service-worker/test/comm_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {ngswCommChannelFactory, SwRegistrationOptions} from '@angular/service-wo
import {SwPush} from '@angular/service-worker/src/push';
import {SwUpdate} from '@angular/service-worker/src/update';
import {MockPushManager, MockPushSubscription, MockServiceWorkerContainer, MockServiceWorkerRegistration, patchDecodeBase64} from '@angular/service-worker/testing/mock';
import {take} from 'rxjs/operators';

{
describe('ServiceWorker library', () => {
Expand Down Expand Up @@ -517,6 +518,29 @@ import {MockPushManager, MockPushSubscription, MockServiceWorkerContainer, MockS
});
expect(() => TestBed.inject(SwUpdate)).not.toThrow();
});

it('get hashes', async () => {
mock.sendMessage({type: 'HASH_UPDATE', hash: 'X'});

const hashes1 = await update.hashes.pipe(take(1)).toPromise();
expect(hashes1.current).toEqual('X');
expect(hashes1.latest).toEqual('X');

mock.sendMessage({
type: 'VERSION_READY',
currentVersion: {
hash: 'A',
},
latestVersion: {
hash: 'B',
},
});

const hashes2 = await update.hashes.pipe(take(1)).toPromise();
expect(hashes2.current).toEqual('A');
expect(hashes2.latest).toEqual('B');
});

describe('with no SW', () => {
beforeEach(() => {
comm = new NgswCommChannel(undefined);
Expand Down
3 changes: 3 additions & 0 deletions packages/service-worker/worker/src/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,9 @@ export class Driver implements Debuggable, UpdateSource {
// Set the latest version.
this.latestHash = latest.latest;

// notify all clients of the latest hash
await this.broadcast({type: 'HASH_UPDATE', hash: this.latestHash});

// Finally, assert that the latest version is in fact loaded.
if (!this.versions.has(latest.latest)) {
throw new Error(
Expand Down
35 changes: 23 additions & 12 deletions packages/service-worker/worker/test/happy_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,9 @@ describe('Driver', () => {
await driver.initialized;

const client = scope.clients.getMock('default')!;
expect(client.messages).toEqual([]);
expect(client.messages).toEqual([
{type: 'HASH_UPDATE', hash: manifestHash},
]);

scope.updateServerState(serverUpdate);
expect(await driver.checkForUpdate()).toEqual(true);
Expand All @@ -487,6 +489,7 @@ describe('Driver', () => {
serverUpdate.assertNoOtherRequests();

expect(client.messages).toEqual([
{type: 'HASH_UPDATE', hash: manifestHash},
{
type: 'VERSION_DETECTED',
version: {hash: manifestUpdateHash, appData: {version: 'update'}},
Expand Down Expand Up @@ -529,14 +532,15 @@ describe('Driver', () => {
await driver.initialized;

const client = scope.clients.getMock('default')!;
expect(client.messages).toEqual([]);
expect(client.messages).toEqual([{type: 'HASH_UPDATE', hash: manifestHash}]);

scope.updateServerState(serverUpdate);
expect(await driver.checkForUpdate()).toEqual(true);
serverUpdate.clearRequests();
await driver.updateClient(client as any as Client);

expect(client.messages).toEqual([
{type: 'HASH_UPDATE', hash: manifestHash},
{type: 'VERSION_DETECTED', version: {hash: manifestUpdateHash, appData: {version: 'update'}}},
{
type: 'VERSION_READY',
Expand Down Expand Up @@ -651,6 +655,10 @@ describe('Driver', () => {
expect(await makeNavigationRequest(scope, '/file1')).toEqual('this is foo v2');

expect(client.messages).toEqual([
{
type: 'HASH_UPDATE',
hash: manifestHash,
},
{
type: 'VERSION_DETECTED',
version: {hash: manifestUpdateHash, appData: {version: 'update'}},
Expand Down Expand Up @@ -680,6 +688,7 @@ describe('Driver', () => {
serverUpdate.clearRequests();

expect(client.messages).toEqual([
{type: 'HASH_UPDATE', hash: manifestHash},
{
type: 'NO_NEW_VERSION_DETECTED',
version: {hash: manifestHash, appData: {version: 'original'}},
Expand Down Expand Up @@ -788,15 +797,17 @@ describe('Driver', () => {
title: 'This is a test',
options: {title: 'This is a test', body: 'Test body'},
}]);
expect(scope.clients.getMock('default')!.messages).toEqual([{
type: 'PUSH',
data: {
notification: {
title: 'This is a test',
body: 'Test body',
expect(scope.clients.getMock('default')!.messages).toEqual([
{type: 'HASH_UPDATE', hash: manifestHash}, {
type: 'PUSH',
data: {
notification: {
title: 'This is a test',
body: 'Test body',
},
},
},
}]);
}
]);
});

describe('notification click events', () => {
Expand All @@ -805,7 +816,7 @@ describe('Driver', () => {
await driver.initialized;
await scope.handleClick(
{title: 'This is a test with action', body: 'Test body with action'}, 'button');
const message = scope.clients.getMock('default')!.messages[0];
const message = scope.clients.getMock('default')!.messages[1];

expect(message.type).toEqual('NOTIFICATION_CLICK');
expect(message.data.action).toEqual('button');
Expand All @@ -820,7 +831,7 @@ describe('Driver', () => {
title: 'This is a test without action',
body: 'Test body without action',
});
const message = scope.clients.getMock('default')!.messages[0];
const message = scope.clients.getMock('default')!.messages[1];

expect(message.type).toEqual('NOTIFICATION_CLICK');
expect(message.data.action).toBe('');
Expand Down