Skip to content

Commit

Permalink
feat: use flux standard actions for worker <-> client communication
Browse files Browse the repository at this point in the history
  • Loading branch information
alxhub committed Jun 30, 2017
1 parent 1204330 commit 6fe895e
Show file tree
Hide file tree
Showing 39 changed files with 2,446 additions and 3,514 deletions.
5 changes: 4 additions & 1 deletion integration/service-worker/e2e/server/page-object.ts
Expand Up @@ -126,8 +126,11 @@ export class HarnessPageObject {
.then(JSON.parse);
}

subscribeToUpdates(): void {
subscribeToUpdates(): Promise<boolean> {
this.reset();
this.selectAction('COMPANION_SUBSCRIBE_TO_UPDATES');
return this
.asyncResult
.then(JSON.parse);
}
}
24 changes: 0 additions & 24 deletions integration/service-worker/e2e/server/push.ts

This file was deleted.

8 changes: 3 additions & 5 deletions integration/service-worker/e2e/test/sanity_e2e.ts
@@ -1,5 +1,4 @@
import {create, Server} from '../server/server';
import {sendPush} from '../server/push';
import {HarnessPageObject} from '../server/page-object';

import fs = require('fs');
Expand Down Expand Up @@ -78,7 +77,6 @@ const FORCED_UPDATE_MANIFEST = {
};

beforeAll(done => {
console.log(process.cwd());
create(8080, 'dist').then(s => {
server = s;
done();
Expand Down Expand Up @@ -212,16 +210,16 @@ describe('world sanity', () => {
.then(updates => {
expect(updates.length).toBe(1);
const update = updates[0];
expect(update['type']).toBe('pending');
const hash = update['version'];
expect(update['next']).toBeTruthy();
const hash = update['next']['manifestHash'];
po.reset();
return po.forceUpdate(hash);
})
.then(() => po.updates)
.then(updates => JSON.parse(updates))
.then(updates => {
expect(updates.length).toBe(2);
expect(updates[1].type).toBe('activation');
expect(updates[1]['previous']).toBeTruthy();
})
.then(() => po.request('/hello.txt'))
.then(result => expect(result).toBe('And again'))
Expand Down
4 changes: 2 additions & 2 deletions integration/service-worker/src/app/app.module.ts
@@ -1,7 +1,7 @@
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import {FormsModule} from '@angular/forms';
import {ServiceWorkerModule} from '@angular/service-worker';
import {NgswModule} from '@angular/service-worker';


import { AppComponent } from './app.component';
Expand All @@ -15,7 +15,7 @@ import {ControllerCmp} from './controller.component';
imports: [
BrowserModule,
FormsModule,
ServiceWorkerModule,
NgswModule,
],
bootstrap: [AppComponent]
})
Expand Down
70 changes: 37 additions & 33 deletions integration/service-worker/src/app/controller.component.ts
@@ -1,6 +1,8 @@
import {Component} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import {NgServiceWorker} from '@angular/service-worker';
import {NgswModule, NgswPush, NgswUpdate, NgswDebug, NgswAppVersion} from '@angular/service-worker';

import 'rxjs/add/observable/merge';

import 'rxjs/add/operator/map';
import 'rxjs/add/operator/scan';
Expand Down Expand Up @@ -85,9 +87,7 @@ export class ControllerCmp {
updateSub = null;
pushes = [];

constructor(public sw: NgServiceWorker) {
sw.log().subscribe(message => this.log.push(message));
}
constructor(public swPush: NgswPush, public swDebug: NgswDebug, public swUpdate: NgswUpdate) {}

actionSelected(action): void {
console.log('set action', action);
Expand Down Expand Up @@ -115,12 +115,14 @@ export class ControllerCmp {
break;
case 'CHECK_FOR_UPDATES':
this
.sw
.checkForUpdate()
.swUpdate
.available
.take(1)
.subscribe(res => {
this.result = JSON.stringify(res);
this.alert = true;
});
this.swUpdate.check();
break;
case 'CACHE_KEYS':
this.loadCacheKeys();
Expand All @@ -130,54 +132,56 @@ export class ControllerCmp {
break;
case 'COMPANION_PING':
this
.sw
.swDebug
.ping()
.subscribe(undefined, undefined, () => {
.then(() => {
this.result = 'pong';
this.alert = true;
});
break;
case 'COMPANION_REG_PUSH':
this
.sw
.registerForPush({applicationServerKey: 'BLRl_fG1TCTc1D2JwzOpdZjaRcJucXtG8TAd5g9vuYjl6KUUDxgoRjQPCgjZfY-_Rusd_qtjNvanHXeFvOFlxH4'})
.subscribe(handler => {
this.result = JSON.stringify(handler.toJSON());
.swPush
.requestSubscription({serverPublicKey: 'BLRl_fG1TCTc1D2JwzOpdZjaRcJucXtG8TAd5g9vuYjl6KUUDxgoRjQPCgjZfY-_Rusd_qtjNvanHXeFvOFlxH4'})
.then(sub => {
this.result = JSON.stringify(sub.toJSON());
this.alert = true;
});
break;
case 'COMPANION_WAIT_FOR_PUSH':
this.pushSub = this
.sw
.push
.swPush
.messages
.take(1)
.map(value => JSON.stringify(value))
.subscribe(value => {
this.result = value;
this.alert = true;
});
(this.sw as any).send({cmd: 'fakePush', push: JSON.stringify({
notification: {
title: 'push notification test',
body: 'this is a test of push notifications',
requireInteraction: true
},
message: 'hello from the server',
})}).subscribe();
(this
.swDebug as any)
.fakePush(JSON.stringify({
notification: {
title: 'push notification test',
body: 'this is a test of push notifications',
requireInteraction: true
},
message: 'hello from the server',
}));
break;
case 'COMPANION_SUBSCRIBE_TO_UPDATES':
this.updateSub = this
.sw
.updates
this.updateSub = Observable
.merge(this.swUpdate.available, this.swUpdate.activated)
.scan((acc, value) => acc.concat(value), [])
.startWith([])
.map(value => JSON.stringify(value))
.subscribe(value => {
this.updates = value;
if (value.length > 2) {
this.updateAlert = true;
this.updates = JSON.stringify(value);
if (value.length > 0) {
this.updateAlert = true;
}
});
this.result = 'true';
this.alert = true;
break;
default:
this.result = null;
Expand Down Expand Up @@ -205,12 +209,12 @@ export class ControllerCmp {
});
}

forceUpdate(version: string): void {
forceUpdate(version: NgswAppVersion): void {
this
.sw
.swUpdate
.activateUpdate(version)
.subscribe(value => {
this.result = JSON.stringify(value);
.then(() => {
this.result = 'activated';
this.alert = true;
})
}
Expand Down
1 change: 1 addition & 0 deletions integration/service-worker/tsconfig.json
Expand Up @@ -15,6 +15,7 @@
],
"types": [
"jasmine",
"node",
"protractor"
],
"lib": [
Expand Down
47 changes: 46 additions & 1 deletion npm-shrinkwrap.clean.json
@@ -1,6 +1,6 @@
{
"name": "angular-srcs",
"version": "4.2.0-beta.1",
"version": "4.3.0-beta.1",
"dependencies": {
"@types/angularjs": {
"version": "1.5.13-alpha"
Expand Down Expand Up @@ -32,6 +32,9 @@
"@types/systemjs": {
"version": "0.19.32"
},
"@types/vinyl": {
"version": "2.0.0"
},
"abbrev": {
"version": "1.0.9"
},
Expand Down Expand Up @@ -1577,9 +1580,37 @@
"clone": {
"version": "1.0.2"
},
"clone-buffer": {
"version": "1.0.0"
},
"clone-stats": {
"version": "0.0.1"
},
"cloneable-readable": {
"version": "1.0.0",
"dependencies": {
"isarray": {
"version": "1.0.0"
},
"readable-stream": {
"version": "2.3.3",
"dependencies": {
"inherits": {
"version": "2.0.3"
}
}
},
"safe-buffer": {
"version": "5.1.1"
},
"string_decoder": {
"version": "1.0.3"
},
"through2": {
"version": "2.0.3"
}
}
},
"cmd-shim": {
"version": "2.0.2",
"dependencies": {
Expand Down Expand Up @@ -4243,6 +4274,9 @@
"registry-url": {
"version": "3.1.0"
},
"remove-trailing-separator": {
"version": "1.0.2"
},
"repeat-element": {
"version": "1.1.2"
},
Expand Down Expand Up @@ -5019,6 +5053,17 @@
"vhost": {
"version": "3.0.2"
},
"vinyl": {
"version": "2.0.2",
"dependencies": {
"clone-stats": {
"version": "1.0.0"
},
"replace-ext": {
"version": "1.0.0"
}
}
},
"vinyl-fs": {
"version": "0.3.14",
"dependencies": {
Expand Down

0 comments on commit 6fe895e

Please sign in to comment.