@ -5,7 +5,7 @@ import 'dart:async';
|
||||
import 'dart:core';
|
||||
import 'package:angular2/src/web_workers/shared/message_bus.dart'
|
||||
show MessageBus;
|
||||
import 'package:angular2/src/web_workers/ui/impl.dart' show bootstrapUICommon;
|
||||
import 'package:angular2/src/web_workers/ui/impl.dart' show bootstrapUICommon, WebWorkerApplication;
|
||||
import 'package:angular2/src/web_workers/shared/isolate_message_bus.dart';
|
||||
|
||||
/**
|
||||
@ -14,26 +14,24 @@ import 'package:angular2/src/web_workers/shared/isolate_message_bus.dart';
|
||||
* You instantiate a WebWorker application by calling bootstrap with the URI of your worker's index script
|
||||
* Note: The WebWorker script must call bootstrapWebworker once it is set up to complete the bootstrapping process
|
||||
*/
|
||||
Future<MessageBus> bootstrap(String uri) {
|
||||
return spawnWebWorker(Uri.parse(uri)).then((bus) {
|
||||
bootstrapUICommon(bus);
|
||||
return bus;
|
||||
});
|
||||
Future<IsolateInstance> bootstrap(String uri) async {
|
||||
var instance = await spawnWebWorker(Uri.parse(uri));
|
||||
instance.app = bootstrapUICommon(instance.bus);
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* To be called from the main thread to spawn and communicate with the worker thread
|
||||
*/
|
||||
Future<MessageBus> spawnWebWorker(Uri uri) {
|
||||
Future<IsolateInstance> spawnWebWorker(Uri uri) async {
|
||||
var receivePort = new ReceivePort();
|
||||
var isolateEndSendPort = receivePort.sendPort;
|
||||
return Isolate.spawnUri(uri, const [], isolateEndSendPort).then((_) {
|
||||
var source = new UIMessageBusSource(receivePort);
|
||||
return source.sink.then((sendPort) {
|
||||
var sink = new IsolateMessageBusSink(sendPort);
|
||||
return new IsolateMessageBus(sink, source);
|
||||
});
|
||||
});
|
||||
var isolate = await Isolate.spawnUri(uri, const [], isolateEndSendPort);
|
||||
var source = new UIMessageBusSource(receivePort);
|
||||
var sendPort = await source.sink;
|
||||
var sink = new IsolateMessageBusSink(sendPort);
|
||||
var bus = new IsolateMessageBus(sink, source);
|
||||
return new IsolateInstance(null, isolate, bus);
|
||||
}
|
||||
|
||||
class UIMessageBusSource extends IsolateMessageBusSource {
|
||||
@ -43,3 +41,15 @@ class UIMessageBusSource extends IsolateMessageBusSource {
|
||||
return message is SendPort;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper class that exposes the {@link WebWorkerApplication}
|
||||
* Isolate instance and underyling {@link MessageBus} for lower level message passing.
|
||||
*/
|
||||
class IsolateInstance {
|
||||
WebWorkerApplication app;
|
||||
final Isolate isolate;
|
||||
final MessageBus bus;
|
||||
|
||||
IsolateInstance(this.app, this.isolate, this.bus);
|
||||
}
|
||||
|
@ -4,8 +4,9 @@ import {
|
||||
PostMessageBusSource
|
||||
} from 'angular2/src/web_workers/shared/post_message_bus';
|
||||
import {MessageBus} from 'angular2/src/web_workers/shared/message_bus';
|
||||
import {BaseException} from "angular2/src/core/facade/lang";
|
||||
import {bootstrapUICommon} from "angular2/src/web_workers/ui/impl";
|
||||
import {BaseException} from 'angular2/src/core/facade/lang';
|
||||
import {bootstrapUICommon, WebWorkerApplication} from 'angular2/src/web_workers/ui/impl';
|
||||
export {WebWorkerApplication} from 'angular2/src/web_workers/ui/impl';
|
||||
export * from 'angular2/src/web_workers/shared/message_bus';
|
||||
|
||||
/**
|
||||
@ -16,15 +17,24 @@ export * from 'angular2/src/web_workers/shared/message_bus';
|
||||
* Note: The WebWorker script must call bootstrapWebworker once it is set up to complete the
|
||||
* bootstrapping process
|
||||
*/
|
||||
export function bootstrap(uri: string): MessageBus {
|
||||
var messageBus = spawnWebWorker(uri);
|
||||
bootstrapUICommon(messageBus);
|
||||
return messageBus;
|
||||
export function bootstrap(uri: string): WebWorkerInstance {
|
||||
var instance = spawnWebWorker(uri);
|
||||
instance.app = bootstrapUICommon(instance.bus);
|
||||
return instance;
|
||||
}
|
||||
|
||||
export function spawnWebWorker(uri: string): MessageBus {
|
||||
export function spawnWebWorker(uri: string): WebWorkerInstance {
|
||||
var webWorker: Worker = new Worker(uri);
|
||||
var sink = new PostMessageBusSink(webWorker);
|
||||
var source = new PostMessageBusSource(webWorker);
|
||||
return new PostMessageBus(sink, source);
|
||||
var bus = new PostMessageBus(sink, source);
|
||||
return new WebWorkerInstance(null, webWorker, bus);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper class that exposes the {@link WebWorkerApplication}
|
||||
* Isolate instance and underyling {@link MessageBus} for lower level message passing.
|
||||
*/
|
||||
export class WebWorkerInstance {
|
||||
constructor(public app: WebWorkerApplication, public worker: Worker, public bus: MessageBus) {}
|
||||
}
|
||||
|
@ -2,7 +2,6 @@
|
||||
// There should be a way to refactor application so that this file is unnecessary. See #3277
|
||||
import {Injector, bind, Binding} from "angular2/di";
|
||||
import {Reflector, reflector} from 'angular2/src/core/reflection/reflection';
|
||||
import {ListWrapper} from 'angular2/src/core/facade/collection';
|
||||
import {
|
||||
Parser,
|
||||
Lexer,
|
||||
@ -61,13 +60,14 @@ import {
|
||||
RenderViewWithFragmentsStore
|
||||
} from 'angular2/src/web_workers/shared/render_view_with_fragments_store';
|
||||
import {AnchorBasedAppRootUrl} from 'angular2/src/core/services/anchor_based_app_root_url';
|
||||
import {WebWorkerMain} from 'angular2/src/web_workers/ui/impl';
|
||||
import {WebWorkerApplication} from 'angular2/src/web_workers/ui/impl';
|
||||
import {MessageBus} from 'angular2/src/web_workers/shared/message_bus';
|
||||
import {MessageBasedRenderCompiler} from 'angular2/src/web_workers/ui/render_compiler';
|
||||
import {MessageBasedRenderer} from 'angular2/src/web_workers/ui/renderer';
|
||||
import {MessageBasedXHRImpl} from 'angular2/src/web_workers/ui/xhr_impl';
|
||||
import {WebWorkerSetup} from 'angular2/src/web_workers/ui/setup';
|
||||
import {ServiceMessageBrokerFactory} from 'angular2/src/web_workers/shared/service_message_broker';
|
||||
import {ClientMessageBrokerFactory} from 'angular2/src/web_workers/shared/client_message_broker';
|
||||
|
||||
var _rootInjector: Injector;
|
||||
|
||||
@ -134,12 +134,13 @@ function _injectorBindings(): any[] {
|
||||
Testability,
|
||||
AnchorBasedAppRootUrl,
|
||||
bind(AppRootUrl).toAlias(AnchorBasedAppRootUrl),
|
||||
WebWorkerMain,
|
||||
WebWorkerApplication,
|
||||
WebWorkerSetup,
|
||||
MessageBasedRenderCompiler,
|
||||
MessageBasedXHRImpl,
|
||||
MessageBasedRenderer,
|
||||
ServiceMessageBrokerFactory
|
||||
ServiceMessageBrokerFactory,
|
||||
ClientMessageBrokerFactory
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -15,25 +15,43 @@ import {WebWorkerSetup} from 'angular2/src/web_workers/ui/setup';
|
||||
import {MessageBasedRenderCompiler} from 'angular2/src/web_workers/ui/render_compiler';
|
||||
import {MessageBasedRenderer} from 'angular2/src/web_workers/ui/renderer';
|
||||
import {MessageBasedXHRImpl} from 'angular2/src/web_workers/ui/xhr_impl';
|
||||
import {
|
||||
ClientMessageBrokerFactory,
|
||||
ClientMessageBroker,
|
||||
} from 'angular2/src/web_workers/shared/client_message_broker';
|
||||
import {
|
||||
ServiceMessageBrokerFactory,
|
||||
ServiceMessageBroker
|
||||
} from 'angular2/src/web_workers/shared/service_message_broker';
|
||||
|
||||
/**
|
||||
* Creates a zone, sets up the DI bindings
|
||||
* And then creates a new WebWorkerMain object to handle messages from the worker
|
||||
*/
|
||||
export function bootstrapUICommon(bus: MessageBus) {
|
||||
export function bootstrapUICommon(bus: MessageBus): WebWorkerApplication {
|
||||
BrowserDomAdapter.makeCurrent();
|
||||
var zone = createNgZone();
|
||||
wtfInit();
|
||||
zone.run(() => {
|
||||
return zone.run(() => {
|
||||
var injector = createInjector(zone, bus);
|
||||
// necessary to kick off all the message based components
|
||||
injector.get(WebWorkerMain);
|
||||
injector.get(MessageBasedRenderCompiler).start();
|
||||
injector.get(MessageBasedRenderer).start();
|
||||
injector.get(MessageBasedXHRImpl).start();
|
||||
injector.get(WebWorkerSetup).start();
|
||||
return injector.get(WebWorkerApplication);
|
||||
});
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class WebWorkerMain {
|
||||
constructor(public renderCompiler: MessageBasedRenderCompiler,
|
||||
public renderer: MessageBasedRenderer, public xhr: MessageBasedXHRImpl,
|
||||
public setup: WebWorkerSetup) {}
|
||||
export class WebWorkerApplication {
|
||||
constructor(private _clientMessageBrokerFactory: ClientMessageBrokerFactory,
|
||||
private _serviceMessageBrokerFactory: ServiceMessageBrokerFactory) {}
|
||||
|
||||
createClientMessageBroker(channel: string): ClientMessageBroker {
|
||||
return this._clientMessageBrokerFactory.createMessageBroker(channel);
|
||||
}
|
||||
|
||||
createServiceMessageBroker(channel: string): ServiceMessageBroker {
|
||||
return this._serviceMessageBrokerFactory.createMessageBroker(channel);
|
||||
}
|
||||
}
|
||||
|
@ -13,8 +13,11 @@ import {ServiceMessageBrokerFactory} from 'angular2/src/web_workers/shared/servi
|
||||
|
||||
@Injectable()
|
||||
export class MessageBasedRenderCompiler {
|
||||
constructor(brokerFactory: ServiceMessageBrokerFactory, private _renderCompiler: RenderCompiler) {
|
||||
var broker = brokerFactory.createMessageBroker(RENDER_COMPILER_CHANNEL);
|
||||
constructor(private _brokerFactory: ServiceMessageBrokerFactory,
|
||||
private _renderCompiler: RenderCompiler) {}
|
||||
|
||||
start(): void {
|
||||
var broker = this._brokerFactory.createMessageBroker(RENDER_COMPILER_CHANNEL);
|
||||
broker.registerMethod("compileHost", [RenderDirectiveMetadata],
|
||||
bind(this._renderCompiler.compileHost, this._renderCompiler),
|
||||
ProtoViewDto);
|
||||
|
@ -19,11 +19,13 @@ import {ServiceMessageBrokerFactory} from 'angular2/src/web_workers/shared/servi
|
||||
|
||||
@Injectable()
|
||||
export class MessageBasedRenderer {
|
||||
constructor(brokerFactory: ServiceMessageBrokerFactory, private _bus: MessageBus,
|
||||
constructor(private _brokerFactory: ServiceMessageBrokerFactory, private _bus: MessageBus,
|
||||
private _serializer: Serializer,
|
||||
private _renderViewWithFragmentsStore: RenderViewWithFragmentsStore,
|
||||
private _renderer: Renderer) {
|
||||
var broker = brokerFactory.createMessageBroker(RENDERER_CHANNEL);
|
||||
private _renderer: Renderer) {}
|
||||
|
||||
start(): void {
|
||||
var broker = this._brokerFactory.createMessageBroker(RENDERER_CHANNEL);
|
||||
broker.registerMethod("createRootHostView",
|
||||
[RenderProtoViewRef, PRIMITIVE, PRIMITIVE, PRIMITIVE],
|
||||
bind(this._createRootHostView, this));
|
||||
|
@ -7,14 +7,19 @@ import {StringWrapper} from 'angular2/src/core/facade/lang';
|
||||
|
||||
@Injectable()
|
||||
export class WebWorkerSetup {
|
||||
constructor(bus: MessageBus, anchorBasedAppRootUrl: AnchorBasedAppRootUrl) {
|
||||
var rootUrl = anchorBasedAppRootUrl.value;
|
||||
var sink = bus.to(SETUP_CHANNEL);
|
||||
var source = bus.from(SETUP_CHANNEL);
|
||||
rootUrl: string;
|
||||
|
||||
constructor(private _bus: MessageBus, anchorBasedAppRootUrl: AnchorBasedAppRootUrl) {
|
||||
this.rootUrl = anchorBasedAppRootUrl.value;
|
||||
}
|
||||
|
||||
start(): void {
|
||||
var sink = this._bus.to(SETUP_CHANNEL);
|
||||
var source = this._bus.from(SETUP_CHANNEL);
|
||||
|
||||
ObservableWrapper.subscribe(source, (message: string) => {
|
||||
if (StringWrapper.equals(message, "ready")) {
|
||||
ObservableWrapper.callNext(sink, {"rootUrl": rootUrl});
|
||||
ObservableWrapper.callNext(sink, {"rootUrl": this.rootUrl});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -7,8 +7,10 @@ import {bind} from './bind';
|
||||
|
||||
@Injectable()
|
||||
export class MessageBasedXHRImpl {
|
||||
constructor(brokerFactory: ServiceMessageBrokerFactory, private _xhr: XHR) {
|
||||
var broker = brokerFactory.createMessageBroker(XHR_CHANNEL);
|
||||
constructor(private _brokerFactory: ServiceMessageBrokerFactory, private _xhr: XHR) {}
|
||||
|
||||
start(): void {
|
||||
var broker = this._brokerFactory.createMessageBroker(XHR_CHANNEL);
|
||||
broker.registerMethod("get", [PRIMITIVE], bind(this._xhr.get, this._xhr), PRIMITIVE);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user