-
Notifications
You must be signed in to change notification settings - Fork 27.1k
fix(service-worker): listen for messages on the right event source #19954
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -85,7 +85,7 @@ export function main() { | |
| driver = new Driver(scope, scope, new CacheDatabase(scope, scope)); | ||
|
|
||
| scope.clients.add('default'); | ||
| scope.clients.getMock('default') !.queue.subscribe(msg => { reg.sendMessage(msg); }); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can remove |
||
| scope.clients.getMock('default') !.queue.subscribe(msg => { mock.sendMessage(msg); }); | ||
|
|
||
| mock.messages.subscribe(msg => { scope.handleMessage(msg, 'default'); }); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,17 +10,26 @@ import {Subject} from 'rxjs/Subject'; | |
|
|
||
| export class MockServiceWorkerContainer { | ||
| private onControllerChange: Function[] = []; | ||
| private onMessage: Function[] = []; | ||
| private registration: MockServiceWorkerRegistration|null = null; | ||
| controller: MockServiceWorker|null = null; | ||
|
|
||
| messages = new Subject(); | ||
|
|
||
| addEventListener(event: 'controllerchange', handler: Function) { | ||
| this.onControllerChange.push(handler); | ||
| addEventListener(event: 'controllerchange'|'message', handler: Function) { | ||
| if (event === 'controllerchange') { | ||
| this.onControllerChange.push(handler); | ||
| } else if (event === 'message') { | ||
| this.onMessage.push(handler); | ||
| } | ||
| } | ||
|
|
||
| removeEventListener(event: 'controllerchange', handler: Function) { | ||
| this.onControllerChange = this.onControllerChange.filter(h => h !== handler); | ||
| if (event === 'controllerchange') { | ||
| this.onControllerChange = this.onControllerChange.filter(h => h !== handler); | ||
| } else if (event === 'message') { | ||
| this.onMessage = this.onMessage.filter(h => h !== handler); | ||
| } | ||
| } | ||
|
|
||
| async register(url: string): Promise<void> { return; } | ||
|
|
@@ -36,6 +45,12 @@ export class MockServiceWorkerContainer { | |
| get mockRegistration(): Promise<MockServiceWorkerRegistration> { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And I don't think you need this getter any more either 😃 |
||
| return Promise.resolve(this.registration !); | ||
| } | ||
|
|
||
| sendMessage(value: Object): void { | ||
| this.onMessage.forEach(onMessage => onMessage({ | ||
| data: value, | ||
| })); | ||
| } | ||
| } | ||
|
|
||
| export class MockServiceWorker { | ||
|
|
@@ -44,21 +59,4 @@ export class MockServiceWorker { | |
| postMessage(value: Object) { this.mock.messages.next(value); } | ||
| } | ||
|
|
||
| export class MockServiceWorkerRegistration { | ||
| private onMessage: Function[] = []; | ||
| messages: Object[] = []; | ||
|
|
||
| constructor() {} | ||
|
|
||
| addEventListener(event: 'message', handler: Function) { this.onMessage.push(handler); } | ||
|
|
||
| removeEventListener(event: 'message', handler: Function) { | ||
| this.onMessage = this.onMessage.filter(h => h !== handler); | ||
| } | ||
|
|
||
| sendMessage(value: Object): void { | ||
| this.onMessage.forEach(onMessage => onMessage({ | ||
| data: value, | ||
| })); | ||
| } | ||
| } | ||
| export class MockServiceWorkerRegistration {} | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can remove
regandMockServiceWorkerRegistrationfrom this file. It is not really used any more.