Chromium Code Reviews| Index: pkg/barback/lib/src/asset_graph_manager.dart |
| diff --git a/pkg/barback/lib/src/asset_graph_manager.dart b/pkg/barback/lib/src/asset_graph_manager.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..732d787cf48d9a4530941b0dd0662fafa9135799 |
| --- /dev/null |
| +++ b/pkg/barback/lib/src/asset_graph_manager.dart |
| @@ -0,0 +1,119 @@ |
| +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +library barback.asset_graph_manager; |
| + |
| +import 'dart:async'; |
| + |
| +import 'package:stack_trace/stack_trace.dart'; |
| + |
| +import 'asset.dart'; |
| +import 'asset_provider.dart'; |
| +import 'asset_graph.dart'; |
| +import 'asset_id.dart'; |
| +import 'errors.dart'; |
| +import 'utils.dart'; |
| + |
| +// 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.
|
| + |
| +/// The asset manager for an entire application. |
| +/// |
| +/// This tracks each package's [AssetGraph] and routes asset requests between |
| +/// them. |
| +class AssetGraphManager { |
| + /// The provider that exposes asset and package information. |
| + final AssetProvider provider; |
| + |
| + /// The [AssetGraph] for each package. |
| + final _graphs = <String, AssetGraph>{}; |
| + |
| + /// 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 "
|
| + /// |
| + /// The result for a given package will be `null` if that [AssetGraph] is |
| + /// actively building. |
| + final _graphResults = <String, BuildResult>{}; |
| + |
| + /// A stream that emits a [BuildResult] each time the build is completed, |
| + /// whether or not it succeeded. |
| + /// |
| + /// This will emit a result only once every package's [AssetGraph] has |
| + /// finished building. |
| + /// |
| + /// If an unexpected error in barback itself occurs, it will be emitted |
| + /// through this stream's error channel. |
| + Stream<BuildResult> get results => _resultsController.stream; |
| + final _resultsController = new StreamController<BuildResult>.broadcast(); |
| + |
| + /// A stream that emits any errors from the asset graph or the transformers. |
| + /// |
| + /// This emits errors as they're detected. If an error occurs in one part of |
| + /// the asset graph, unrelated parts will continue building. |
| + /// |
| + /// This will not emit programming errors from barback itself. Those will be |
| + /// emitted through the [results] stream's error channel. |
| + Stream get errors => _errors; |
| + Stream _errors; |
| + |
| + /// Creates a new [AssetGraphManager] that will transform assets in all |
| + /// packages made available by [provider]. |
| + AssetGraphManager(this.provider) { |
| + for (var package in provider.packages) { |
| + var graph = new AssetGraph( |
| + 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.
|
| + _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
|
| + _graphs[package] = graph; |
| + |
| + graph.results.listen((result) { |
| + _graphResults[graph.package] = result; |
| + // If any graph hasn't yet finished, the overall build isn't finished |
| + // either. |
| + 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
|
| + |
| + // Include all build errors for all graphs. If no graphs have errors, |
| + // the result will automatically be considered a success. |
| + _resultsController.add(new BuildResult(flatten( |
| + _graphResults.values.map((result) => result.errors)))); |
| + }, onError: _resultsController.addError); |
| + } |
| + |
| + _errors = mergeStreams(_graphs.values.map((graph) => graph.errors)); |
| + } |
| + |
| + /// Gets the asset identified by [id]. |
| + /// |
| + /// If [id] is for a generated or transformed asset, this will wait until |
| + /// it has been created and return it. If the asset cannot be found, throws |
| + /// [AssetNotFoundException]. |
| + Future<Asset> getAssetById(AssetId id) { |
| + var graph = _graphs[id.package]; |
| + if (graph != null) return graph.getAssetById(id); |
| + return new Future.error( |
| + new AssetNotFoundException(id), |
| + new Trace.current().vmTrace); |
| + } |
| + |
| + /// Adds [sources] to the graph's known set of source assets. |
| + /// |
| + /// Begins applying any transforms that can consume any of the sources. If a |
| + /// given source is already known, it is considered modified and all |
| + /// transforms that use it will be re-applied. |
| + void updateSources(Iterable<AssetId> sources) { |
| + groupBy(sources, (id) => id.package).forEach((package, ids) { |
| + var graph = _graphs[package]; |
| + if (graph == null) throw new ArgumentError("Unknown package $package."); |
| + _graphResults[package] = null; |
| + graph.updateSources(ids); |
| + }); |
| + } |
| + |
| + /// Removes [removed] from the graph's known set of source assets. |
| + void removeSources(Iterable<AssetId> sources) { |
| + groupBy(sources, (id) => id.package).forEach((package, ids) { |
| + var graph = _graphs[package]; |
| + if (graph == null) throw new ArgumentError("Unknown package $package."); |
| + _graphResults[package] = null; |
| + graph.removeSources(ids); |
| + }); |
| + } |
| +} |