refactor(WebWorker): move XHR worker side

This commit is contained in:
Victor Berchet 2016-05-19 16:10:44 -07:00
parent 54f8308999
commit e8e61de28d
6 changed files with 4 additions and 96 deletions

View File

@ -1,7 +1,7 @@
import {WORKER_APP_STATIC_APPLICATION_PROVIDERS} from '../static/worker_app';
import {workerAppPlatform} from '../common/worker_app';
import {COMPILER_PROVIDERS, XHR} from '@angular/compiler';
import {WebWorkerXHRImpl} from '../../web_workers/worker/xhr_impl';
import {XHRImpl} from '../../xhr/xhr_impl';
import {isPresent} from '../../facade/lang';
import {
@ -14,8 +14,7 @@ import {
export const WORKER_APP_APPLICATION_PROVIDERS: Array<any /*Type | Provider | any[]*/> = [
WORKER_APP_STATIC_APPLICATION_PROVIDERS,
COMPILER_PROVIDERS,
WebWorkerXHRImpl,
/* @ts2dart_Provider */ {provide: XHR, useExisting: WebWorkerXHRImpl}
/* @ts2dart_Provider */ {provide: XHR, useClass: XHRImpl},
];
export function bootstrapApp(

View File

@ -1,17 +1,11 @@
import {XHR} from "@angular/compiler";
import {XHRImpl} from "../../xhr/xhr_impl";
import {MessageBasedXHRImpl} from "../../web_workers/ui/xhr_impl";
import {WORKER_RENDER_STATIC_APPLICATION_PROVIDERS} from "../static/worker_render";
import {ApplicationRef, ReflectiveInjector} from "@angular/core";
import {workerRenderPlatform, WORKER_SCRIPT, WORKER_RENDER_STARTABLE_MESSAGING_SERVICE} from "../common/worker_render";
import {workerRenderPlatform, WORKER_SCRIPT} from "../common/worker_render";
import {isPresent} from "../../facade/lang";
import {PromiseWrapper} from "../../facade/async";
export const WORKER_RENDER_APPLICATION_PROVIDERS: Array<any /*Type | Provider | any[]*/> = [
WORKER_RENDER_STATIC_APPLICATION_PROVIDERS,
/* @ts2dart_Provider */ {provide: XHR, useClass: XHRImpl},
MessageBasedXHRImpl,
/* @ts2dart_Provider */ {provide: WORKER_RENDER_STARTABLE_MESSAGING_SERVICE, useExisting: MessageBasedXHRImpl, multi: true},
WORKER_RENDER_STATIC_APPLICATION_PROVIDERS
];
export function bootstrapRender(

View File

@ -5,4 +5,3 @@
export const RENDERER_CHANNEL = "ng-Renderer";
export const EVENT_CHANNEL = "ng-Events";
export const ROUTER_CHANNEL = "ng-Router";
export const XHR_CHANNEL = "ng-XHR";

View File

@ -1,24 +0,0 @@
import {Injectable} from '@angular/core';
import {PRIMITIVE} from '../shared/serializer';
import {ServiceMessageBrokerFactory} from '../shared/service_message_broker';
import {XHR_CHANNEL} from '../shared/messaging_api';
import {XHR} from '@angular/compiler';
import {FunctionWrapper} from '../../facade/lang';
/**
* XHR requests triggered on the worker side are executed on the UI side.
*
* This is only strictly required for Dart where the isolates do not have access to XHRs.
*
* @internal
*/
@Injectable()
export class MessageBasedXHRImpl {
constructor(private _brokerFactory: ServiceMessageBrokerFactory, private _xhr: XHR) {}
start(): void {
var broker = this._brokerFactory.createMessageBroker(XHR_CHANNEL);
broker.registerMethod("get", [PRIMITIVE], FunctionWrapper.bind(this._xhr.get, this._xhr),
PRIMITIVE);
}
}

View File

@ -1,31 +0,0 @@
import {Injectable} from '@angular/core';
import {XHR} from '@angular/compiler';
import {
FnArg,
UiArguments,
ClientMessageBroker,
ClientMessageBrokerFactory
} from '../shared/client_message_broker';
import {XHR_CHANNEL} from '../shared/messaging_api';
/**
* Implementation of compiler/xhr that relays XHR requests to the UI side where they are sent
* and the result is proxied back to the worker.
*
* This is only strictly required for Dart where isolates do not have access to the XHRs.
*/
@Injectable()
export class WebWorkerXHRImpl extends XHR {
private _messageBroker: ClientMessageBroker;
constructor(messageBrokerFactory: ClientMessageBrokerFactory) {
super();
this._messageBroker = messageBrokerFactory.createMessageBroker(XHR_CHANNEL);
}
get(url: string): Promise<string> {
var fnArgs: FnArg[] = [new FnArg(url, null)];
var args: UiArguments = new UiArguments("get", fnArgs);
return this._messageBroker.runOnService(args, String);
}
}

View File

@ -1,29 +0,0 @@
import {inject, describe, it, expect} from '@angular/core/testing/testing_internal';
import {AsyncTestCompleter} from '@angular/core/testing/testing_internal';
import {SpyMessageBroker} from '@angular/platform-browser/test/web_workers/worker/spies';
import {WebWorkerXHRImpl} from '@angular/platform-browser/src/web_workers/worker/xhr_impl';
import {PromiseWrapper} from '../../../src/facade/async';
import {
MockMessageBrokerFactory,
expectBrokerCall
} from '@angular/platform-browser/test/web_workers/shared/web_worker_test_util';
export function main() {
describe("WebWorkerXHRImpl", () => {
it("should pass requests through the broker and return the response",
inject([AsyncTestCompleter], (async) => {
const URL = "http://www.example.com/test";
const RESPONSE = "Example response text";
var messageBroker = new SpyMessageBroker();
expectBrokerCall(messageBroker, "get", [URL],
(_) => { return PromiseWrapper.wrap(() => { return RESPONSE; }); });
var xhrImpl = new WebWorkerXHRImpl(new MockMessageBrokerFactory(<any>messageBroker));
xhrImpl.get(URL).then((response) => {
expect(response).toEqual(RESPONSE);
async.done();
});
}));
});
}