| 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.graph.phase_output; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 import 'dart:collection'; | |
| 9 | |
| 10 import '../asset/asset_forwarder.dart'; | |
| 11 import '../asset/asset_node.dart'; | |
| 12 import '../errors.dart'; | |
| 13 import 'phase.dart'; | |
| 14 | |
| 15 /// A class that handles a single output of a phase. | |
| 16 /// | |
| 17 /// Normally there's only a single [AssetNode] for a phase's output, but it's | |
| 18 /// possible that multiple transformers in the same phase emit assets with the | |
| 19 /// same id, causing collisions. This handles those collisions by forwarding the | |
| 20 /// chronologically first asset. | |
| 21 /// | |
| 22 /// When the asset being forwarding changes, the old value of [output] will be | |
| 23 /// marked as removed and a new value will replace it. Users of this class can | |
| 24 /// be notified of this using [onAsset]. | |
| 25 class PhaseOutput { | |
| 26 /// The phase for which this is an output. | |
| 27 final Phase _phase; | |
| 28 | |
| 29 /// A string describing the location of [this] in the transformer graph. | |
| 30 final String _location; | |
| 31 | |
| 32 /// The asset node for this output. | |
| 33 AssetNode get output => _outputForwarder.node; | |
| 34 AssetForwarder _outputForwarder; | |
| 35 | |
| 36 /// A stream that emits an [AssetNode] each time this output starts forwarding | |
| 37 /// a new asset. | |
| 38 Stream<AssetNode> get onAsset => _onAssetController.stream; | |
| 39 final _onAssetController = | |
| 40 new StreamController<AssetNode>.broadcast(sync: true); | |
| 41 | |
| 42 /// The assets for this output. | |
| 43 /// | |
| 44 /// If there's no collision, this will only have one element. Otherwise, it | |
| 45 /// will be ordered by which asset was added first. | |
| 46 final _assets = new Queue<AssetNode>(); | |
| 47 | |
| 48 /// The [AssetCollisionException] for this output, or null if there is no | |
| 49 /// collision currently. | |
| 50 AssetCollisionException get collisionException { | |
| 51 if (_assets.length == 1) return null; | |
| 52 return new AssetCollisionException( | |
| 53 _assets.where((asset) => asset.transform != null) | |
| 54 .map((asset) => asset.transform.info), | |
| 55 output.id); | |
| 56 } | |
| 57 | |
| 58 PhaseOutput(this._phase, AssetNode output, this._location) | |
| 59 : _outputForwarder = new AssetForwarder(output) { | |
| 60 assert(!output.state.isRemoved); | |
| 61 add(output); | |
| 62 } | |
| 63 | |
| 64 /// Adds an asset node as an output with this id. | |
| 65 void add(AssetNode node) { | |
| 66 assert(node.id == output.id); | |
| 67 assert(!output.state.isRemoved); | |
| 68 _assets.add(node); | |
| 69 _watchAsset(node); | |
| 70 } | |
| 71 | |
| 72 /// Removes all existing listeners on [output] without actually closing | |
| 73 /// [this]. | |
| 74 /// | |
| 75 /// This marks [output] as removed, but immediately replaces it with a new | |
| 76 /// [AssetNode] in the same state as the old output. This is used when adding | |
| 77 /// a new [Phase] to cause consumers of the prior phase's outputs to be to | |
| 78 /// start consuming the new phase's outputs instead. | |
| 79 void removeListeners() { | |
| 80 _outputForwarder.close(); | |
| 81 _outputForwarder = new AssetForwarder(_assets.first); | |
| 82 _onAssetController.add(output); | |
| 83 } | |
| 84 | |
| 85 /// Watches [node] to adjust [_assets] and [output] when it's removed. | |
| 86 void _watchAsset(AssetNode node) { | |
| 87 node.whenRemoved(() { | |
| 88 if (_assets.length == 1) { | |
| 89 assert(_assets.single == node); | |
| 90 _outputForwarder.close(); | |
| 91 _onAssetController.close(); | |
| 92 return; | |
| 93 } | |
| 94 | |
| 95 // If there was more than one asset, we're resolving a collision -- | |
| 96 // possibly partially. | |
| 97 var wasFirst = _assets.first == node; | |
| 98 _assets.remove(node); | |
| 99 | |
| 100 // If this was the first asset, we replace it with the next asset | |
| 101 // (chronologically). | |
| 102 if (wasFirst) removeListeners(); | |
| 103 | |
| 104 // If there's still a collision, report it. This lets the user know if | |
| 105 // they've successfully resolved the collision or not. | |
| 106 if (_assets.length > 1) { | |
| 107 // TODO(nweiz): report this through the output asset. | |
| 108 _phase.cascade.reportError(collisionException); | |
| 109 } | |
| 110 }); | |
| 111 } | |
| 112 | |
| 113 String toString() => "phase output in $_location for $output"; | |
| 114 } | |
| OLD | NEW |