2015-07-10 16:09:18 -07:00
|
|
|
import {
|
|
|
|
MessageBus,
|
|
|
|
MessageBusSource,
|
|
|
|
MessageBusSink,
|
|
|
|
SourceListener
|
|
|
|
} from "angular2/src/web-workers/shared/message_bus";
|
|
|
|
import {Type, BaseException} from "angular2/src/facade/lang";
|
|
|
|
import {Binding} from "angular2/di";
|
|
|
|
|
2015-08-07 13:17:54 -07:00
|
|
|
import {bootstrapWebWorkerCommon} from "angular2/src/web-workers/worker/application_common";
|
2015-07-10 16:09:18 -07:00
|
|
|
import {ApplicationRef} from "angular2/src/core/application";
|
2015-07-10 16:09:18 -07:00
|
|
|
import {Injectable} from "angular2/di";
|
2015-07-10 16:09:18 -07:00
|
|
|
|
2015-07-31 10:33:22 -07:00
|
|
|
// TODO(jteplitz602) remove this and compile with lib.webworker.d.ts (#3492)
|
|
|
|
var _postMessage: (message: any, transferrables?:[ArrayBuffer]) => void = <any>postMessage;
|
|
|
|
|
2015-07-10 16:09:18 -07:00
|
|
|
/**
|
|
|
|
* Bootstrapping a Webworker Application
|
|
|
|
*
|
|
|
|
* You instantiate the application side by calling bootstrapWebworker from your webworker index
|
|
|
|
* script.
|
|
|
|
* You can call bootstrapWebworker() exactly as you would call bootstrap() in a regular Angular
|
|
|
|
* application
|
|
|
|
* See the bootstrap() docs for more details.
|
|
|
|
*/
|
2015-08-07 13:17:54 -07:00
|
|
|
export function bootstrapWebWorker(
|
2015-07-10 16:09:18 -07:00
|
|
|
appComponentType: Type, componentInjectableBindings: List<Type | Binding | List<any>> = null):
|
|
|
|
Promise<ApplicationRef> {
|
2015-08-07 13:17:54 -07:00
|
|
|
var bus: WebWorkerMessageBus =
|
|
|
|
new WebWorkerMessageBus(new WebWorkerMessageBusSink(), new WebWorkerMessageBusSource());
|
2015-07-10 16:09:18 -07:00
|
|
|
|
2015-08-07 13:17:54 -07:00
|
|
|
return bootstrapWebWorkerCommon(appComponentType, bus, componentInjectableBindings);
|
2015-07-10 16:09:18 -07:00
|
|
|
}
|
|
|
|
|
2015-07-10 16:09:18 -07:00
|
|
|
@Injectable()
|
2015-08-07 13:17:54 -07:00
|
|
|
export class WebWorkerMessageBus implements MessageBus {
|
|
|
|
sink: WebWorkerMessageBusSink;
|
|
|
|
source: WebWorkerMessageBusSource;
|
2015-07-10 16:09:18 -07:00
|
|
|
|
2015-08-07 13:17:54 -07:00
|
|
|
constructor(sink: WebWorkerMessageBusSink, source: WebWorkerMessageBusSource) {
|
2015-07-10 16:09:18 -07:00
|
|
|
this.sink = sink;
|
|
|
|
this.source = source;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-07 13:17:54 -07:00
|
|
|
export class WebWorkerMessageBusSink implements MessageBusSink {
|
2015-07-31 10:33:22 -07:00
|
|
|
public send(message: Object) { _postMessage(message); }
|
2015-07-10 16:09:18 -07:00
|
|
|
}
|
|
|
|
|
2015-08-07 13:17:54 -07:00
|
|
|
export class WebWorkerMessageBusSource implements MessageBusSource {
|
2015-07-10 16:09:18 -07:00
|
|
|
private listenerStore: Map<int, SourceListener>;
|
|
|
|
private numListeners: int;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.numListeners = 0;
|
|
|
|
this.listenerStore = new Map<int, SourceListener>();
|
|
|
|
}
|
|
|
|
|
|
|
|
public addListener(fn: SourceListener): int {
|
|
|
|
addEventListener("message", fn);
|
|
|
|
this.listenerStore[++this.numListeners] = fn;
|
|
|
|
return this.numListeners;
|
|
|
|
}
|
|
|
|
|
|
|
|
public removeListener(index: int): void {
|
|
|
|
removeEventListener("message", this.listenerStore[index]);
|
|
|
|
this.listenerStore.delete(index);
|
|
|
|
}
|
2015-07-10 16:09:18 -07:00
|
|
|
}
|