feat(WebWorker): Expose MessageBroker API

Closes #3942
This commit is contained in:
Jason Teplitz
2015-09-01 10:55:11 -07:00
parent 6532171997
commit 358908e605
31 changed files with 267 additions and 275 deletions

View File

@ -0,0 +1,12 @@
library angular2.examples.message_broker.background_index;
import "package:angular2/web_worker/worker.dart";
import "package:angular2/src/core/reflection/reflection_capabilities.dart";
import "package:angular2/src/core/reflection/reflection.dart";
import "index_common.dart" show App;
import "dart:isolate";
main(List<String> args, SendPort replyTo) {
reflector.reflectionCapabilities = new ReflectionCapabilities();
bootstrapWebWorker(replyTo, App).catchError((error) => throw error);
}

View File

@ -0,0 +1,6 @@
import {bootstrapWebWorker} from "angular2/web_worker/worker";
import {App} from "./index_common";
export function main() {
bootstrapWebWorker(App);
}

View File

@ -0,0 +1,22 @@
library angular2.examples.message_broker.index;
import "package:angular2/web_worker/ui.dart";
import "package:angular2/src/core/reflection/reflection_capabilities.dart";
import "package:angular2/src/core/reflection/reflection.dart";
import "dart:html";
main() {
reflector.reflectionCapabilities = new ReflectionCapabilities();
const ECHO_CHANNEL = "ECHO";
bootstrap("background_index.dart").then((instance) {
var broker = instance.app.createClientMessageBroker(ECHO_CHANNEL);
querySelector("#send_echo").addEventListener("click", (e) {
var val = (querySelector("#echo_input") as InputElement).value;
var args = new UiArguments("echo", [new FnArg(val, PRIMITIVE)]);
broker.runOnService(args, PRIMITIVE).then((echo_result) {
querySelector("#echo_result")
.appendHtml("<span class='response'>${echo_result}</span>");
});
});
});
}

View File

@ -0,0 +1,12 @@
<!doctype html>
<html>
<title>Message Broker Example</title>
<body>
<app></app>
<input type="text" id="echo_input" />
<button type="button" id="send_echo">Send Echo</button>
<p id="echo_result"></p>
<p id="ui_result"></p>
$SCRIPTS$
</body>
</html>

View File

@ -0,0 +1,25 @@
import {bootstrap, UiArguments, FnArg, PRIMITIVE} from "angular2/web_worker/ui";
const ECHO_CHANNEL = "ECHO";
var instance = bootstrap("loader.js");
var broker = instance.app.createClientMessageBroker(ECHO_CHANNEL);
document.getElementById("send_echo")
.addEventListener("click", (e) => {
var val = (<HTMLInputElement>document.getElementById("echo_input")).value;
// TODO(jteplitz602): Replace default constructors with real constructors
// once they're in the .d.ts file (#3926)
var args = new UiArguments();
args.method = "echo";
var fnArg = new FnArg();
fnArg.value = val;
fnArg.type = PRIMITIVE;
args.args = [fnArg];
broker.runOnService(args, PRIMITIVE)
.then((echo_result: string) => {
document.getElementById("echo_result").innerHTML =
`<span class='response'>${echo_result}</span>`;
});
});

View File

@ -0,0 +1,17 @@
import {PromiseWrapper} from "angular2/src/core/facade/async";
import {Component, View, ServiceMessageBrokerFactory, PRIMITIVE} from "angular2/web_worker/worker";
const ECHO_CHANNEL = "ECHO";
@Component({selector: 'app', viewBindings: [ServiceMessageBrokerFactory]})
@View({template: "<h1>WebWorker MessageBroker Test</h1>"})
export class App {
constructor(private _serviceBrokerFactory: ServiceMessageBrokerFactory) {
var broker = _serviceBrokerFactory.createMessageBroker(ECHO_CHANNEL);
broker.registerMethod("echo", [PRIMITIVE], this._echo, PRIMITIVE);
}
private _echo(val: string) {
return PromiseWrapper.wrap(() => { return val; });
}
}

View File

@ -0,0 +1,19 @@
$SCRIPTS$
System.config({
baseURL: '/',
defaultJSExtensions: true,
paths: {'rx': 'examples/src/web_workers/message_broker/rx.js'}
});
System.import("examples/src/web_workers/message_broker/background_index")
.then(
function(m) {
console.log("running main");
try {
m.main();
} catch (e) {
console.error(e);
}
},
function(error) { console.error("error loading background", error); });