@@ -5,7 +5,7 @@ import 'dart:async';
55import 'dart:core' ;
66import 'package:angular2/src/web_workers/shared/message_bus.dart'
77 show MessageBus;
8- import 'package:angular2/src/web_workers/ui/impl.dart' show bootstrapUICommon;
8+ import 'package:angular2/src/web_workers/ui/impl.dart' show bootstrapUICommon, WebWorkerApplication ;
99import 'package:angular2/src/web_workers/shared/isolate_message_bus.dart' ;
1010
1111/**
@@ -14,26 +14,24 @@ import 'package:angular2/src/web_workers/shared/isolate_message_bus.dart';
1414 * You instantiate a WebWorker application by calling bootstrap with the URI of your worker's index script
1515 * Note: The WebWorker script must call bootstrapWebworker once it is set up to complete the bootstrapping process
1616 */
17- Future <MessageBus > bootstrap (String uri) {
18- return spawnWebWorker (Uri .parse (uri)).then ((bus) {
19- bootstrapUICommon (bus);
20- return bus;
21- });
17+ Future <IsolateInstance > bootstrap (String uri) async {
18+ var instance = await spawnWebWorker (Uri .parse (uri));
19+ instance.app = bootstrapUICommon (instance.bus);
20+ return instance;
2221}
2322
2423/**
2524 * To be called from the main thread to spawn and communicate with the worker thread
2625 */
27- Future <MessageBus > spawnWebWorker (Uri uri) {
26+ Future <IsolateInstance > spawnWebWorker (Uri uri) async {
2827 var receivePort = new ReceivePort ();
2928 var isolateEndSendPort = receivePort.sendPort;
30- return Isolate .spawnUri (uri, const [], isolateEndSendPort).then ((_) {
31- var source = new UIMessageBusSource (receivePort);
32- return source.sink.then ((sendPort) {
33- var sink = new IsolateMessageBusSink (sendPort);
34- return new IsolateMessageBus (sink, source);
35- });
36- });
29+ var isolate = await Isolate .spawnUri (uri, const [], isolateEndSendPort);
30+ var source = new UIMessageBusSource (receivePort);
31+ var sendPort = await source.sink;
32+ var sink = new IsolateMessageBusSink (sendPort);
33+ var bus = new IsolateMessageBus (sink, source);
34+ return new IsolateInstance (null , isolate, bus);
3735}
3836
3937class UIMessageBusSource extends IsolateMessageBusSource {
@@ -43,3 +41,15 @@ class UIMessageBusSource extends IsolateMessageBusSource {
4341 return message is SendPort ;
4442 });
4543}
44+
45+ /**
46+ * Wrapper class that exposes the {@link WebWorkerApplication}
47+ * Isolate instance and underyling {@link MessageBus} for lower level message passing.
48+ */
49+ class IsolateInstance {
50+ WebWorkerApplication app;
51+ final Isolate isolate;
52+ final MessageBus bus;
53+
54+ IsolateInstance (this .app, this .isolate, this .bus);
55+ }
0 commit comments