Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(530)

Unified Diff: dart/pkg/dart2js_incremental/lib/compiler.dart

Issue 798583004: Refactor to use streams for reusability. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: dart/pkg/dart2js_incremental/lib/compiler.dart
diff --git a/dart/pkg/dart2js_incremental/lib/compiler.dart b/dart/pkg/dart2js_incremental/lib/compiler.dart
index 0f486b45250b3d75ecca49472f21b35ffa3c87b4..ef386f4209370affcfeec834f7e38c2c6bfc5d80 100644
--- a/dart/pkg/dart2js_incremental/lib/compiler.dart
+++ b/dart/pkg/dart2js_incremental/lib/compiler.dart
@@ -5,9 +5,11 @@
import 'dart:io';
import 'dart:async' show
- StreamSubscription,
EventSink,
- Future;
+ Future,
+ Stream,
+ StreamController,
+ StreamSubscription;
import 'package:dart2js_incremental/dart2js_incremental.dart' show
IncrementalCompilationFailed,
@@ -18,8 +20,56 @@ import 'package:compiler/src/source_file_provider.dart' show
import 'watcher.dart';
-main(List<String> arguments) async {
- Uri originalInput = Uri.base.resolve(arguments.first);
+main(List<String> arguments) {
+ int updateCount = 0;
+ StreamSubscription<CompilerEvent> subscritpion =
kasperl 2015/01/20 13:58:31 subscription
ahe 2015/01/20 14:04:33 Done.
+ compile(Uri.base.resolve(arguments.first)).listen(null);
+ subscritpion.onData((CompilerEvent event) {
+ switch (event.kind) {
+ case IncrementalKind.FULL:
+ updateCount = 0;
+ print('// Compiled JavaScript:');
+ print(event['.js']);
+ break;
+
+ case IncrementalKind.INCREMENTAL:
+ Stopwatch sw = event.stopwatch..start();
+ String updates = '${event.compiler.allUpdates()}';
+ sw.stop();
+
+ print('// Patch after ${++updateCount} updates,');
+ print('// computed in ${sw.elapsedMicroseconds/1000000} seconds:');
+ print(updates);
+ break;
+
+ case IncrementalKind.ERROR:
+ updateCount = 0;
+ print("Compilation failed");
+ break;
+
+ default:
+ throw "Unknown kind: ${event.kind}";
+ }
+ });
+ subscritpion.onError((error, StackTrace trace) {
+ if (error is IncrementalCompilationFailed) {
+ print("Incremental compilation failed due to:\n${error.reason}");
+ } else {
+ throw error;
+ }
+ });
+}
+
+Stream<CompilerEvent> compile(Uri originalInput) {
+ StreamController<CompilerEvent> controller =
+ new StreamController<CompilerEvent>();
+ compileToStream(originalInput, controller);
+ return controller.stream;
+}
+
+compileToStream(
+ Uri originalInput,
+ StreamController<CompilerEvent> controller) async {
var watcher = new Watcher();
Uri libraryRoot = Uri.base.resolve('sdk/');
@@ -48,6 +98,7 @@ main(List<String> arguments) async {
}
while (true) {
+ Stopwatch sw = new Stopwatch()..start();
IncrementalCompiler compiler = new IncrementalCompiler(
libraryRoot: libraryRoot,
packageRoot: packageRoot,
@@ -55,29 +106,33 @@ main(List<String> arguments) async {
diagnosticHandler: resilientDiagnosticHandler,
outputProvider: outputProvider);
- if (!await compiler.compile(originalInput)) {
- print("Compilation failed");
+ bool success = await compiler.compile(originalInput);
+ sw.stop();
+ if (success) {
+ controller.add(
+ new CompilerEvent(
+ IncrementalKind.FULL, compiler, outputProvider.output, sw));
} else {
- print('// Compiled JavaScript:');
- print(outputProvider['.js']);
+ controller.add(
+ new CompilerEvent(
+ IncrementalKind.ERROR, compiler, outputProvider.output, sw));
}
- int updateCount = 0;
while (await watcher.hasChanges()) {
try {
Map<Uri, Uri> changes = watcher.readChanges();
- Stopwatch sw = new Stopwatch()..start();
+ sw = new Stopwatch()..start();
await compiler.compileUpdates(changes);
+ sw.stop();
- String updates = '${compiler.allUpdates()}';
+ controller.add(
+ new CompilerEvent(
+ IncrementalKind.INCREMENTAL, compiler, outputProvider.output,
+ sw));
- sw.stop();
- print('// Patch after ${++updateCount} updates,');
- print('// computed in ${sw.elapsedMicroseconds/1000000} seconds:');
- print(updates);
- } on IncrementalCompilationFailed catch (error) {
- print("Incremental compilation failed due to:\n${error.reason}");
+ } on IncrementalCompilationFailed catch (error, trace) {
+ controller.addError(error, trace);
break;
}
}
@@ -121,3 +176,23 @@ class StringEventSink implements EventSink<String> {
}
}
}
+
+enum IncrementalKind {
+ FULL,
+ INCREMENTAL,
+ ERROR,
+}
+
+class CompilerEvent {
+ final IncrementalKind kind;
+
+ final IncrementalCompiler compiler;
+
+ final Map<String, String> _output;
+
+ final Stopwatch stopwatch;
+
+ CompilerEvent(this.kind, this.compiler, this._output, this.stopwatch);
+
+ String operator[](String key) => _output[key];
+}
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698