2017-09-28 19:18:12 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2018-02-27 17:06:06 -05:00
|
|
|
import {Subject} from 'rxjs';
|
2017-09-28 19:18:12 -04:00
|
|
|
|
|
|
|
export class MockServiceWorkerContainer {
|
|
|
|
private onControllerChange: Function[] = [];
|
2017-10-26 13:29:36 -04:00
|
|
|
private onMessage: Function[] = [];
|
2017-09-28 19:18:12 -04:00
|
|
|
private registration: MockServiceWorkerRegistration|null = null;
|
|
|
|
controller: MockServiceWorker|null = null;
|
|
|
|
|
|
|
|
messages = new Subject();
|
|
|
|
|
2017-10-26 13:29:36 -04:00
|
|
|
addEventListener(event: 'controllerchange'|'message', handler: Function) {
|
|
|
|
if (event === 'controllerchange') {
|
|
|
|
this.onControllerChange.push(handler);
|
|
|
|
} else if (event === 'message') {
|
|
|
|
this.onMessage.push(handler);
|
|
|
|
}
|
2017-09-28 19:18:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
removeEventListener(event: 'controllerchange', handler: Function) {
|
2017-10-26 13:29:36 -04:00
|
|
|
if (event === 'controllerchange') {
|
|
|
|
this.onControllerChange = this.onControllerChange.filter(h => h !== handler);
|
|
|
|
} else if (event === 'message') {
|
|
|
|
this.onMessage = this.onMessage.filter(h => h !== handler);
|
|
|
|
}
|
2017-09-28 19:18:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async register(url: string): Promise<void> { return; }
|
|
|
|
|
|
|
|
async getRegistration(): Promise<ServiceWorkerRegistration> { return this.registration as any; }
|
|
|
|
|
|
|
|
setupSw(url: string = '/ngsw-worker.js'): void {
|
|
|
|
this.registration = new MockServiceWorkerRegistration();
|
|
|
|
this.controller = new MockServiceWorker(this, url);
|
|
|
|
this.onControllerChange.forEach(onChange => onChange(this.controller));
|
|
|
|
}
|
|
|
|
|
|
|
|
get mockRegistration(): Promise<MockServiceWorkerRegistration> {
|
|
|
|
return Promise.resolve(this.registration !);
|
|
|
|
}
|
2017-10-26 13:29:36 -04:00
|
|
|
|
|
|
|
sendMessage(value: Object): void {
|
|
|
|
this.onMessage.forEach(onMessage => onMessage({
|
|
|
|
data: value,
|
|
|
|
}));
|
|
|
|
}
|
2017-09-28 19:18:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export class MockServiceWorker {
|
|
|
|
constructor(private mock: MockServiceWorkerContainer, readonly scriptURL: string) {}
|
|
|
|
|
|
|
|
postMessage(value: Object) { this.mock.messages.next(value); }
|
|
|
|
}
|
|
|
|
|
2017-10-26 13:29:36 -04:00
|
|
|
export class MockServiceWorkerRegistration {}
|