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

Side by Side Diff: pkg/barback/lib/src/phase_output.dart

Issue 26413003: Make PhaseOutput emit a new AssetNode whenever it changes outputs. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Code review change Created 7 years, 2 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/barback/lib/src/phase.dart ('k') | pkg/barback/test/package_graph/group_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library barback.phase_output; 5 library barback.phase_output;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 import 'dart:collection'; 8 import 'dart:collection';
9 9
10 import 'asset_cascade.dart'; 10 import 'asset_cascade.dart';
11 import 'asset_forwarder.dart';
11 import 'asset_node.dart'; 12 import 'asset_node.dart';
12 import 'errors.dart'; 13 import 'errors.dart';
13 import 'phase.dart'; 14 import 'phase.dart';
14 import 'utils.dart'; 15 import 'utils.dart';
15 16
16 /// A class that handles a single output of a phase. 17 /// A class that handles a single output of a phase.
17 /// 18 ///
18 /// Normally there's only a single [AssetNode] for a phase's output, but it's 19 /// Normally there's only a single [AssetNode] for a phase's output, but it's
19 /// possible that multiple transformers in the same phase emit assets with the 20 /// possible that multiple transformers in the same phase emit assets with the
20 /// same id, causing collisions. This handles those collisions by forwarding the 21 /// same id, causing collisions. This handles those collisions by forwarding the
21 /// chronologically first asset. 22 /// chronologically first asset.
23 ///
24 /// When the asset being forwarding changes, the old value of [output] will be
25 /// marked as removed and a new value will replace it. Users of this class can
26 /// be notified of this using [onAsset].
22 class PhaseOutput { 27 class PhaseOutput {
23 /// The phase for which this is an output. 28 /// The phase for which this is an output.
24 final Phase _phase; 29 final Phase _phase;
25 30
26 /// The asset node for this output. 31 /// The asset node for this output.
27 AssetNode get output => _outputController.node; 32 AssetNode get output => _outputForwarder.node;
28 AssetNodeController _outputController; 33 AssetNodeForwarder _outputForwarder;
34
35 /// A stream that emits an [AssetNode] each time this output starts forwarding
36 /// a new asset.
37 Stream<AssetNode> get onAsset => _onAssetController.stream;
38 final _onAssetController = new StreamController<AssetNode>();
29 39
30 /// The assets for this output. 40 /// The assets for this output.
31 /// 41 ///
32 /// If there's no collision, this will only have one element. Otherwise, it 42 /// If there's no collision, this will only have one element. Otherwise, it
33 /// will be ordered by which asset was added first. 43 /// will be ordered by which asset was added first.
34 final _assets = new Queue<AssetNode>(); 44 final _assets = new Queue<AssetNode>();
35 45
36 /// The [AssetCollisionException] for this output, or null if there is no 46 /// The [AssetCollisionException] for this output, or null if there is no
37 /// collision currently. 47 /// collision currently.
38 AssetCollisionException get collisionException { 48 AssetCollisionException get collisionException {
39 if (_assets.length == 1) return null; 49 if (_assets.length == 1) return null;
40 return new AssetCollisionException( 50 return new AssetCollisionException(
41 _assets.where((asset) => asset.transform != null) 51 _assets.where((asset) => asset.transform != null)
42 .map((asset) => asset.transform.info), 52 .map((asset) => asset.transform.info),
43 output.id); 53 output.id);
44 } 54 }
45 55
46 PhaseOutput(this._phase, AssetNode output) 56 PhaseOutput(this._phase, AssetNode output)
47 : _outputController = new AssetNodeController.from(output) { 57 : _outputForwarder = new AssetForwarder(output) {
48 assert(!output.state.isRemoved); 58 assert(!output.state.isRemoved);
49 add(output); 59 add(output);
50 } 60 }
51 61
52 /// Adds an asset node as an output with this id. 62 /// Adds an asset node as an output with this id.
53 void add(AssetNode node) { 63 void add(AssetNode node) {
54 assert(node.id == output.id); 64 assert(node.id == output.id);
55 assert(!output.state.isRemoved); 65 assert(!output.state.isRemoved);
56 _assets.add(node); 66 _assets.add(node);
57 _watchAsset(node); 67 _watchAsset(node);
58 } 68 }
59 69
60 /// Removes all existing listeners on [output] without actually closing 70 /// Removes all existing listeners on [output] without actually closing
61 /// [this]. 71 /// [this].
62 /// 72 ///
63 /// This marks [output] as removed, but immediately replaces it with a new 73 /// This marks [output] as removed, but immediately replaces it with a new
64 /// [AssetNode] in the same state as the old output. This is used when adding 74 /// [AssetNode] in the same state as the old output. This is used when adding
65 /// a new [Phase] to cause consumers of the prior phase's outputs to be to 75 /// a new [Phase] to cause consumers of the prior phase's outputs to be to
66 /// start consuming the new phase's outputs instead. 76 /// start consuming the new phase's outputs instead.
67 void removeListeners() { 77 void removeListeners() {
68 _outputController.setRemoved(); 78 _outputForwarder.close();
69 _outputController = new AssetNodeController.from(_assets.first); 79 _outputForwarder = new AssetForwarder(_assets.first);
80 _onAssetController.add(output);
70 } 81 }
71 82
72 /// Watches [node] for state changes and adjusts [_assets] and [output] 83 /// Watches [node] to adjust [_assets] and [output] when it's removed.
73 /// appropriately when they occur.
74 void _watchAsset(AssetNode node) { 84 void _watchAsset(AssetNode node) {
75 node.onStateChange.listen((state) { 85 node.whenRemoved.then((_) {
76 if (state.isRemoved) { 86 if (_assets.length == 1) {
77 _removeAsset(node); 87 assert(_assets.single == node);
88 _outputForwarder.close();
89 _onAssetController.close();
78 return; 90 return;
79 } 91 }
80 if (_assets.first != node) return;
81 92
82 if (state.isAvailable) { 93 // If there was more than one asset, we're resolving a collision --
83 _outputController.setAvailable(node.asset); 94 // possibly partially.
84 } else { 95 var wasFirst = _assets.first == node;
85 assert(state.isDirty); 96 _assets.remove(node);
86 _outputController.setDirty(); 97
98 // If this was the first asset, we replace it with the next asset
99 // (chronologically).
100 if (wasFirst) removeListeners();
101
102 // If there's still a collision, report it. This lets the user know if
103 // they've successfully resolved the collision or not.
104 if (_assets.length > 1) {
105 // Pump the event queue to ensure that the removal of the input triggers
106 // a new build to which we can attach the error.
107 // TODO(nweiz): report this through the output asset.
108 newFuture(() => _phase.cascade.reportError(collisionException));
87 } 109 }
88 }); 110 });
89 } 111 }
90
91 /// Removes [node] as an output.
92 void _removeAsset(AssetNode node) {
93 if (_assets.length == 1) {
94 assert(_assets.single == node);
95 _outputController.setRemoved();
96 return;
97 }
98
99 // If there was more than one asset, we're resolving a collision --
100 // possibly partially.
101 var wasFirst = _assets.first == node;
102 _assets.remove(node);
103
104 // If this was the first asset, we replace it with the next asset
105 // (chronologically).
106 if (wasFirst) {
107 var newOutput = _assets.first;
108 _outputController.setOrigin(newOutput.origin);
109 if (newOutput.state.isAvailable) {
110 if (output.state.isAvailable) _outputController.setDirty();
111 _outputController.setAvailable(newOutput.asset);
112 } else {
113 assert(newOutput.isDirty);
114 if (!output.state.isDirty) _outputController.setDirty();
115 }
116 }
117
118 // If there's still a collision, report it. This lets the user know
119 // if they've successfully resolved the collision or not.
120 if (_assets.length > 1) {
121 // Pump the event queue to ensure that the removal of the input triggers
122 // a new build to which we can attach the error.
123 // TODO(nweiz): report this through the output asset.
124 newFuture(() => _phase.cascade.reportError(collisionException));
125 }
126 }
127 } 112 }
OLDNEW
« no previous file with comments | « pkg/barback/lib/src/phase.dart ('k') | pkg/barback/test/package_graph/group_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698