2015-07-10 16:09:18 -07:00
|
|
|
library angular2.src.web_workers.ui;
|
|
|
|
|
|
|
|
import 'dart:isolate';
|
|
|
|
import 'dart:async';
|
2015-07-10 16:09:18 -07:00
|
|
|
import 'dart:core';
|
|
|
|
import 'package:angular2/src/web-workers/shared/message_bus.dart'
|
2015-07-10 16:09:18 -07:00
|
|
|
show MessageBus, MessageBusSink, MessageBusSource;
|
2015-07-27 17:26:28 -07:00
|
|
|
import 'package:angular2/src/web-workers/ui/impl.dart' show bootstrapUICommon;
|
2015-07-10 16:09:18 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Bootstrapping a WebWorker
|
2015-07-10 16:09:18 -07:00
|
|
|
*
|
2015-07-10 16:09:18 -07:00
|
|
|
* You instantiate a WebWorker application by calling bootstrap with the URI of your worker's index script
|
2015-07-10 16:09:18 -07:00
|
|
|
* Note: The WebWorker script must call bootstrapWebworker once it is set up to complete the bootstrapping process
|
2015-07-10 16:09:18 -07:00
|
|
|
*/
|
2015-07-31 10:33:22 -07:00
|
|
|
Future<MessageBus> bootstrap(String uri) {
|
2015-08-07 13:17:54 -07:00
|
|
|
return spawnWebWorker(Uri.parse(uri)).then((bus) {
|
2015-07-10 16:09:18 -07:00
|
|
|
bootstrapUICommon(bus);
|
2015-07-31 10:33:22 -07:00
|
|
|
return bus;
|
2015-07-10 16:09:18 -07:00
|
|
|
});
|
2015-07-10 16:09:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* To be called from the main thread to spawn and communicate with the worker thread
|
|
|
|
*/
|
2015-08-07 13:17:54 -07:00
|
|
|
Future<UIMessageBus> spawnWebWorker(Uri uri) {
|
2015-07-10 16:09:18 -07:00
|
|
|
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 UIMessageBusSink(sendPort);
|
|
|
|
return new UIMessageBus(sink, source);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class UIMessageBus extends MessageBus {
|
|
|
|
final UIMessageBusSink sink;
|
|
|
|
final UIMessageBusSource source;
|
|
|
|
|
|
|
|
UIMessageBus(UIMessageBusSink sink, UIMessageBusSource source)
|
|
|
|
: sink = sink,
|
|
|
|
source = source;
|
|
|
|
}
|
|
|
|
|
|
|
|
class UIMessageBusSink extends MessageBusSink {
|
|
|
|
final SendPort _port;
|
|
|
|
|
|
|
|
UIMessageBusSink(SendPort port) : _port = port;
|
|
|
|
|
|
|
|
void send(message) {
|
|
|
|
_port.send(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class UIMessageBusSource extends MessageBusSource {
|
|
|
|
final ReceivePort _port;
|
|
|
|
final Stream rawDataStream;
|
2015-07-27 17:26:28 -07:00
|
|
|
Map<int, StreamSubscription> _listenerStore =
|
|
|
|
new Map<int, StreamSubscription>();
|
2015-07-10 16:09:18 -07:00
|
|
|
int _numListeners = 0;
|
2015-07-10 16:09:18 -07:00
|
|
|
|
|
|
|
UIMessageBusSource(ReceivePort port)
|
|
|
|
: _port = port,
|
|
|
|
rawDataStream = port.asBroadcastStream();
|
|
|
|
|
|
|
|
Future<SendPort> get sink => rawDataStream.firstWhere((message) {
|
2015-08-04 12:05:30 -07:00
|
|
|
return message is SendPort;
|
|
|
|
});
|
2015-07-10 16:09:18 -07:00
|
|
|
|
2015-07-27 17:26:28 -07:00
|
|
|
int addListener(Function fn) {
|
|
|
|
var subscription = rawDataStream.listen((message) {
|
2015-07-10 16:09:18 -07:00
|
|
|
fn({"data": message});
|
|
|
|
});
|
2015-07-10 16:09:18 -07:00
|
|
|
|
|
|
|
_listenerStore[++_numListeners] = subscription;
|
|
|
|
return _numListeners;
|
|
|
|
}
|
|
|
|
|
2015-07-27 17:26:28 -07:00
|
|
|
void removeListener(int index) {
|
2015-07-10 16:09:18 -07:00
|
|
|
_listenerStore[index].cancel();
|
|
|
|
_listenerStore.remove(index);
|
2015-07-10 16:09:18 -07:00
|
|
|
}
|
|
|
|
}
|