Push updates to browser.

R=kasperl@google.com

Review URL: https://codereview.chromium.org//847573003

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@43063 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
ahe@google.com 2015-01-22 10:02:20 +00:00
parent 2260e65e5c
commit 3b1cb964ff
5 changed files with 58 additions and 13 deletions

View file

@ -26,6 +26,22 @@ if (!navigator.dartEnabled && (navigator.userAgent.indexOf('(Dart)') === -1)) {
// than one script.
document.currentScript = script;
parent.replaceChild(script, scripts[i]);
// Support for incremental compilation.
script.onload = function (event) {
var script = event.target;
if (self.$dart_unsafe_incremental_support) {
new WebSocket(script.src.replace(/^http/, 'ws')).onmessage =
function (event) {
var patch = String(event.data);
self.$dart_unsafe_incremental_support.patch(patch);
script.dispatchEvent(
new CustomEvent(
"dart_program_updated",
{ bubbles: true, detail: { patch: patch } }));
};
}
};
}
}
}

View file

@ -1,5 +1,5 @@
name: browser
version: 0.10.0+2
version: 0.10.1
author: "Dart Team <misc@dartlang.org>"
homepage: http://www.dartlang.org
description: >

View file

@ -92,7 +92,9 @@ compileToStream(
Future inputProvider(Uri uri) {
if (uri.scheme == "file") {
watcher.watchFile(uri);
if (!'$uri'.startsWith('$libraryRoot')) {
watcher.watchFile(uri);
}
}
return diagnosticHandler.provider(uri);
}
@ -123,13 +125,13 @@ compileToStream(
Map<Uri, Uri> changes = watcher.readChanges();
sw = new Stopwatch()..start();
await compiler.compileUpdates(changes);
String updates = await compiler.compileUpdates(changes);
sw.stop();
controller.add(
new CompilerEvent(
IncrementalKind.INCREMENTAL, compiler, outputProvider.output,
sw));
sw, updates: updates));
} on IncrementalCompilationFailed catch (error, trace) {
controller.addError(error, trace);
@ -192,7 +194,10 @@ class CompilerEvent {
final Stopwatch stopwatch;
CompilerEvent(this.kind, this.compiler, this._output, this.stopwatch);
final String updates;
CompilerEvent(
this.kind, this.compiler, this._output, this.stopwatch, {this.updates});
String operator[](String key) => _output[key];
}

View file

@ -150,7 +150,7 @@ class IncrementalCompiler {
jsAst.FunctionDeclaration mainRunner = jsAst.js.statement(r"""
function dartMainRunner(main, args) {
#helper.patch(#updates);
#helper.patch(#updates + "\n//# sourceURL=initial_patch.js\n");
return main(args);
}""", {'updates': updates, 'helper': backend.namer.accessIncrementalHelper});

View file

@ -10,6 +10,7 @@ import 'dart:async' show
Completer,
Future,
Stream,
StreamController,
StreamSubscription;
import 'dart:convert' show
@ -39,6 +40,9 @@ class Conversation {
static Map<Uri, Future<String>> generatedFiles =
new Map<Uri, Future<String>>();
static Map<Uri, StreamController<String>> updateControllers =
new Map<Uri, StreamController<String>>();
Conversation(this.request, this.response);
onClosed(_) {
@ -64,13 +68,14 @@ class Conversation {
}
Future handleSocket() async {
if (false && request.uri.path == '/ws/watch') {
StreamController<String> controller = updateControllers[request.uri];
if (controller != null) {
WebSocket socket = await WebSocketTransformer.upgrade(request);
socket.add(JSON.encode({'create': []}));
// WatchHandler handler = new WatchHandler(socket, files);
// handlers.add(handler);
// socket.listen(
// handler.onData, cancelOnError: true, onDone: handler.onDone);
print(
"Patches to ${request.uri} will be pushed to "
"${request.connectionInfo.remoteAddress.host}:"
"${request.connectionInfo.remotePort}.");
controller.stream.pipe(socket);
} else {
response.done
.then(onClosed)
@ -130,6 +135,10 @@ class Conversation {
response.headers.set(CONTENT_TYPE, 'image/x-icon');
} else if (path.endsWith('.appcache')) {
response.headers.set(CONTENT_TYPE, 'text/cache-manifest');
} else if (path.endsWith('.css')) {
response.headers.set(CONTENT_TYPE, 'text/css');
} else if (path.endsWith('.png')) {
response.headers.set(CONTENT_TYPE, 'image/png');
}
}
@ -155,7 +164,12 @@ class Conversation {
Uri outputUri = request.uri;
Completer<String> completer = new Completer<String>();
generatedFiles[outputUri] = completer.future;
print("Compiling $dartScript to $outputUri");
StreamController controller = updateControllers[outputUri];
if (controller != null) {
controller.close();
}
updateControllers[outputUri] = new StreamController<String>.broadcast();
print("Compiling $dartScript to $outputUri.");
StreamSubscription<CompilerEvent> subscription;
subscription = compile(dartScript).listen((CompilerEvent event) {
subscription.onData(
@ -164,6 +178,7 @@ class Conversation {
notFound(request.uri);
// TODO(ahe): Do something about this situation.
} else {
print("Done compiling $dartScript to $outputUri.");
completer.complete(event['.js']);
setContentType(outputUri.path);
response.write(event['.js']);
@ -184,6 +199,7 @@ class Conversation {
case IncrementalKind.INCREMENTAL:
generatedFiles[outputUri] = completer.future.then(
(String full) => '$full\n\n${event.compiler.allUpdates()}');
pushUpdates(event.updates);
break;
case IncrementalKind.ERROR:
@ -192,6 +208,14 @@ class Conversation {
}
}
void pushUpdates(String updates) {
if (updates == null) return;
StreamController<String> controller = updateControllers[request.uri];
if (controller == null) return;
print("Adding updates to controller");
controller.add(updates);
}
Future dispatch() async {
try {
return await WebSocketTransformer.isUpgradeRequest(request)