Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library barback.asset_graph_manager; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 | |
| 9 import 'package:stack_trace/stack_trace.dart'; | |
| 10 | |
| 11 import 'asset.dart'; | |
| 12 import 'asset_provider.dart'; | |
| 13 import 'asset_graph.dart'; | |
| 14 import 'asset_id.dart'; | |
| 15 import 'errors.dart'; | |
| 16 import 'utils.dart'; | |
| 17 | |
| 18 // TODO(nweiz): come up with better names for this and AssetGraph. | |
|
Bob Nystrom
2013/07/11 23:03:40
Agreed. "Manager" is one of my biggest pet peeves.
nweiz
2013/07/15 22:11:44
I like "PackageGraph", but where does that leave A
Bob Nystrom
2013/07/16 17:36:01
How about:
AssetGraphManager -> AssetGraph
AssetG
nweiz
2013/07/16 19:39:39
2013/07/16 17:36:01, Bob Nystrom wrote:
Bob Nystrom
2013/07/16 22:23:17
SGTM.
| |
| 19 | |
| 20 /// The asset manager for an entire application. | |
| 21 /// | |
| 22 /// This tracks each package's [AssetGraph] and routes asset requests between | |
| 23 /// them. | |
| 24 class AssetGraphManager { | |
| 25 /// The provider that exposes asset and package information. | |
| 26 final AssetProvider provider; | |
| 27 | |
| 28 /// The [AssetGraph] for each package. | |
| 29 final _graphs = <String, AssetGraph>{}; | |
| 30 | |
| 31 /// The current [BuildResult] for each package's [AssetGraph]. | |
|
Bob Nystrom
2013/07/11 23:03:40
"current" -> "last"
nweiz
2013/07/15 22:11:44
I think "current" is slightly more accurate than "
| |
| 32 /// | |
| 33 /// The result for a given package will be `null` if that [AssetGraph] is | |
| 34 /// actively building. | |
| 35 final _graphResults = <String, BuildResult>{}; | |
| 36 | |
| 37 /// A stream that emits a [BuildResult] each time the build is completed, | |
| 38 /// whether or not it succeeded. | |
| 39 /// | |
| 40 /// This will emit a result only once every package's [AssetGraph] has | |
| 41 /// finished building. | |
| 42 /// | |
| 43 /// If an unexpected error in barback itself occurs, it will be emitted | |
| 44 /// through this stream's error channel. | |
| 45 Stream<BuildResult> get results => _resultsController.stream; | |
| 46 final _resultsController = new StreamController<BuildResult>.broadcast(); | |
| 47 | |
| 48 /// A stream that emits any errors from the asset graph or the transformers. | |
| 49 /// | |
| 50 /// This emits errors as they're detected. If an error occurs in one part of | |
| 51 /// the asset graph, unrelated parts will continue building. | |
| 52 /// | |
| 53 /// This will not emit programming errors from barback itself. Those will be | |
| 54 /// emitted through the [results] stream's error channel. | |
| 55 Stream get errors => _errors; | |
| 56 Stream _errors; | |
| 57 | |
| 58 /// Creates a new [AssetGraphManager] that will transform assets in all | |
| 59 /// packages made available by [provider]. | |
| 60 AssetGraphManager(this.provider) { | |
| 61 for (var package in provider.packages) { | |
| 62 var graph = new AssetGraph( | |
| 63 this, package, provider.getTransformers(package)); | |
|
Bob Nystrom
2013/07/11 23:03:40
Nit, but wrapping here looks weird to me. How abou
nweiz
2013/07/15 22:11:44
Done.
| |
| 64 _graphResults[package] = new BuildResult.success(); | |
|
Bob Nystrom
2013/07/11 23:03:40
Why is this initially success and not null? Docume
nweiz
2013/07/15 22:11:44
I wanted to make it consistent with the fact that
| |
| 65 _graphs[package] = graph; | |
| 66 | |
| 67 graph.results.listen((result) { | |
| 68 _graphResults[graph.package] = result; | |
| 69 // If any graph hasn't yet finished, the overall build isn't finished | |
| 70 // either. | |
| 71 if (_graphResults.values.any((result) => result == null)) return; | |
|
Bob Nystrom
2013/07/11 23:03:40
Having to walk this every time a package completes
nweiz
2013/07/15 22:11:44
I don't think there are likely to be enough packag
| |
| 72 | |
| 73 // Include all build errors for all graphs. If no graphs have errors, | |
| 74 // the result will automatically be considered a success. | |
| 75 _resultsController.add(new BuildResult(flatten( | |
| 76 _graphResults.values.map((result) => result.errors)))); | |
| 77 }, onError: _resultsController.addError); | |
| 78 } | |
| 79 | |
| 80 _errors = mergeStreams(_graphs.values.map((graph) => graph.errors)); | |
| 81 } | |
| 82 | |
| 83 /// Gets the asset identified by [id]. | |
| 84 /// | |
| 85 /// If [id] is for a generated or transformed asset, this will wait until | |
| 86 /// it has been created and return it. If the asset cannot be found, throws | |
| 87 /// [AssetNotFoundException]. | |
| 88 Future<Asset> getAssetById(AssetId id) { | |
| 89 var graph = _graphs[id.package]; | |
| 90 if (graph != null) return graph.getAssetById(id); | |
| 91 return new Future.error( | |
| 92 new AssetNotFoundException(id), | |
| 93 new Trace.current().vmTrace); | |
| 94 } | |
| 95 | |
| 96 /// Adds [sources] to the graph's known set of source assets. | |
| 97 /// | |
| 98 /// Begins applying any transforms that can consume any of the sources. If a | |
| 99 /// given source is already known, it is considered modified and all | |
| 100 /// transforms that use it will be re-applied. | |
| 101 void updateSources(Iterable<AssetId> sources) { | |
| 102 groupBy(sources, (id) => id.package).forEach((package, ids) { | |
| 103 var graph = _graphs[package]; | |
| 104 if (graph == null) throw new ArgumentError("Unknown package $package."); | |
| 105 _graphResults[package] = null; | |
| 106 graph.updateSources(ids); | |
| 107 }); | |
| 108 } | |
| 109 | |
| 110 /// Removes [removed] from the graph's known set of source assets. | |
| 111 void removeSources(Iterable<AssetId> sources) { | |
| 112 groupBy(sources, (id) => id.package).forEach((package, ids) { | |
| 113 var graph = _graphs[package]; | |
| 114 if (graph == null) throw new ArgumentError("Unknown package $package."); | |
| 115 _graphResults[package] = null; | |
| 116 graph.removeSources(ids); | |
| 117 }); | |
| 118 } | |
| 119 } | |
| OLD | NEW |