From b09ee424bfcbe57d5ae01f8957e869545e73a9b3 Mon Sep 17 00:00:00 2001 From: Peter Bacon Darwin Date: Thu, 2 Mar 2017 12:20:26 +0000 Subject: [PATCH] refactor(aio): reorganize search service for easier testing --- aio/src/app/app.component.ts | 3 +++ aio/src/app/search/search.service.spec.ts | 24 +++++++++++++++++++++++ aio/src/app/search/search.service.ts | 12 ++++++++---- aio/src/app/shared/web-worker.ts | 8 +++----- 4 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 aio/src/app/search/search.service.spec.ts diff --git a/aio/src/app/app.component.ts b/aio/src/app/app.component.ts index 0e33a3ea75..813804bba2 100644 --- a/aio/src/app/app.component.ts +++ b/aio/src/app/app.component.ts @@ -101,6 +101,9 @@ export class AppComponent implements OnInit { } ngOnInit() { + this.searchService.initWorker('app/search/search-worker.js'); + this.searchService.loadIndex(); + this.onResize(window.innerWidth); } diff --git a/aio/src/app/search/search.service.spec.ts b/aio/src/app/search/search.service.spec.ts new file mode 100644 index 0000000000..7b7566ec0a --- /dev/null +++ b/aio/src/app/search/search.service.spec.ts @@ -0,0 +1,24 @@ +import { ReflectiveInjector, NgZone } from '@angular/core'; +import { SearchService } from './search.service'; + +xdescribe('SearchService', () => { + + let injector: ReflectiveInjector; + + beforeEach(() => { + injector = ReflectiveInjector.resolveAndCreate([ + SearchService, + { provide: NgZone, useFactory: () => new NgZone({ enableLongStackTrace: false }) } + ]); + }); + + describe('loadIndex', () => { + it('should send a "load-index" message to the worker', () => {}); + it('should connect the `ready` property to the response to the "load-index" message', () => {}); + }); + + describe('search', () => { + it('should send a "query-index" message to the worker', () => {}); + it('should push the response to the `searchResults` observable', () => {}); + }); +}); diff --git a/aio/src/app/search/search.service.ts b/aio/src/app/search/search.service.ts index d84e7af67e..391b719465 100644 --- a/aio/src/app/search/search.service.ts +++ b/aio/src/app/search/search.service.ts @@ -4,7 +4,7 @@ Use of this source code is governed by an MIT-style license that can be found in the LICENSE file at http://angular.io/license */ -import { NgZone, Injectable } from '@angular/core'; +import { NgZone, Injectable, Type } from '@angular/core'; import { Observable } from 'rxjs/Observable'; import { Subject } from 'rxjs/Subject'; import 'rxjs/add/operator/publishLast'; @@ -16,7 +16,6 @@ export interface QueryResults { results: Object[]; } -const SEARCH_WORKER_URL = 'app/search/search-worker.js'; @Injectable() export class SearchService { @@ -25,8 +24,13 @@ export class SearchService { private resultsSubject = new Subject(); get searchResults() { return this.resultsSubject.asObservable(); } - constructor(private zone: NgZone) { - this.worker = new WebWorkerClient(SEARCH_WORKER_URL, zone); + constructor(private zone: NgZone) {} + + initWorker(workerUrl) { + this.worker = new WebWorkerClient(new Worker(workerUrl), this.zone); + } + + loadIndex() { const ready = this.ready = this.worker.sendMessage('load-index').publishLast(); // trigger the index to be loaded immediately ready.connect(); diff --git a/aio/src/app/shared/web-worker.ts b/aio/src/app/shared/web-worker.ts index c5fe6b27f0..95bf6e05ab 100644 --- a/aio/src/app/shared/web-worker.ts +++ b/aio/src/app/shared/web-worker.ts @@ -14,18 +14,16 @@ export interface WebWorkerMessage { } export class WebWorkerClient { - worker: Worker; - private messageId = 0; + private nextId = 0; - constructor(url: string, private zone: NgZone) { - this.worker = new Worker(url); + constructor(private worker: Worker, private zone: NgZone) { } sendMessage(type: string, payload?: any): Observable { return new Observable(subscriber => { - const id = this.messageId++; + const id = this.nextId++; const handleMessage = (response: MessageEvent) => { const {type: responseType, id: responseId, payload: responsePayload} = response.data as WebWorkerMessage;