2015-07-10 16:09:18 -07:00
|
|
|
library angular2.src.web_workers.worker;
|
|
|
|
|
|
|
|
import "package:angular2/src/web-workers/shared/message_bus.dart"
|
|
|
|
show MessageBus, MessageBusSource, MessageBusSink;
|
2015-07-10 16:09:18 -07:00
|
|
|
import "package:angular2/src/web-workers/worker/application_common.dart"
|
2015-07-27 17:26:28 -07:00
|
|
|
show bootstrapWebworkerCommon;
|
2015-07-10 16:09:18 -07:00
|
|
|
import "package:angular2/src/facade/async.dart" show Future;
|
|
|
|
import "package:angular2/src/core/application.dart" show ApplicationRef;
|
|
|
|
import "package:angular2/src/facade/lang.dart" show Type, BaseException;
|
|
|
|
import "dart:isolate";
|
|
|
|
import "dart:async";
|
2015-07-10 16:09:18 -07:00
|
|
|
import 'dart:core';
|
2015-07-10 16:09:18 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Bootstrapping a Webworker Application
|
2015-07-10 16:09:18 -07:00
|
|
|
*
|
2015-07-10 16:09:18 -07:00
|
|
|
* You instantiate the application side by calling bootstrapWebworker from your webworker index
|
|
|
|
* script.
|
|
|
|
* You must supply a SendPort for communicating with the UI side in order to instantiate
|
|
|
|
* the application.
|
|
|
|
* Other than the SendPort you can call bootstrapWebworker() exactly as you would call
|
|
|
|
* bootstrap() in a regular Angular application
|
|
|
|
* See the bootstrap() docs for more details.
|
|
|
|
*/
|
|
|
|
Future<ApplicationRef> bootstrapWebworker(
|
|
|
|
SendPort replyTo, Type appComponentType,
|
2015-07-10 16:09:18 -07:00
|
|
|
[List<dynamic> componentInjectableBindings = null]) {
|
|
|
|
ReceivePort rPort = new ReceivePort();
|
|
|
|
WorkerMessageBus bus = new WorkerMessageBus.fromPorts(replyTo, rPort);
|
2015-07-27 17:26:28 -07:00
|
|
|
return bootstrapWebworkerCommon(
|
|
|
|
appComponentType, bus, componentInjectableBindings);
|
2015-07-10 16:09:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
class WorkerMessageBus extends MessageBus {
|
|
|
|
final WorkerMessageBusSink sink;
|
|
|
|
final WorkerMessageBusSource source;
|
|
|
|
|
|
|
|
WorkerMessageBus(this.sink, this.source);
|
|
|
|
|
|
|
|
WorkerMessageBus.fromPorts(SendPort sPort, ReceivePort rPort)
|
|
|
|
: sink = new WorkerMessageBusSink(sPort, rPort),
|
|
|
|
source = new WorkerMessageBusSource(rPort);
|
|
|
|
}
|
|
|
|
|
|
|
|
class WorkerMessageBusSink extends MessageBusSink {
|
|
|
|
final SendPort _port;
|
|
|
|
|
|
|
|
WorkerMessageBusSink(SendPort sPort, ReceivePort rPort) : _port = sPort {
|
|
|
|
this.send(rPort.sendPort);
|
|
|
|
}
|
|
|
|
|
|
|
|
void send(dynamic message) {
|
|
|
|
this._port.send(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class WorkerMessageBusSource 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
|
|
|
|
|
|
|
WorkerMessageBusSource(ReceivePort rPort)
|
|
|
|
: _port = rPort,
|
|
|
|
rawDataStream = rPort.asBroadcastStream();
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|