| OLD | NEW |
| 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.graph.group_runner; | 5 library barback.group_runner; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import '../asset/asset_node.dart'; | |
| 10 import '../log.dart'; | |
| 11 import '../transformer/transformer_group.dart'; | |
| 12 import '../utils/stream_pool.dart'; | |
| 13 import 'asset_cascade.dart'; | 9 import 'asset_cascade.dart'; |
| 14 import 'node_status.dart'; | 10 import 'asset_node.dart'; |
| 11 import 'log.dart'; |
| 15 import 'phase.dart'; | 12 import 'phase.dart'; |
| 13 import 'stream_pool.dart'; |
| 14 import 'transformer_group.dart'; |
| 16 | 15 |
| 17 /// A class that processes all of the phases in a single transformer group. | 16 /// A class that processes all of the phases in a single transformer group. |
| 18 /// | 17 /// |
| 19 /// A group takes many inputs, processes them, and emits many outputs. | 18 /// A group takes many inputs, processes them, and emits many outputs. |
| 20 class GroupRunner { | 19 class GroupRunner { |
| 21 /// The group this runner runs. | 20 /// The group this runner runs. |
| 22 final TransformerGroup _group; | 21 final TransformerGroup _group; |
| 23 | 22 |
| 24 /// A string describing the location of [this] in the transformer graph. | 23 /// A string describing the location of [this] in the transformer graph. |
| 25 final String _location; | 24 final String _location; |
| 26 | 25 |
| 27 /// The phases defined by this group. | 26 /// The phases defined by this group. |
| 28 final _phases = new List<Phase>(); | 27 final _phases = new List<Phase>(); |
| 29 | 28 |
| 30 /// How far along [this] is in processing its assets. | 29 /// Whether [this] is dirty and still has more processing to do. |
| 31 NodeStatus get status { | 30 bool get isDirty { |
| 32 // Just check the last phase, since it will check all the previous phases | 31 // Just check the last phase, since it will check all the previous phases |
| 33 // itself. | 32 // itself. |
| 34 return _phases.last.status; | 33 return _phases.last.isDirty; |
| 35 } | 34 } |
| 36 | 35 |
| 37 /// A stream that emits an event every time the group's status changes. | 36 /// A stream that emits an event whenever [this] is no longer dirty. |
| 38 Stream<NodeStatus> get onStatusChange => _onStatusChange; | 37 /// |
| 39 Stream _onStatusChange; | 38 /// This is synchronous in order to guarantee that it will emit an event as |
| 39 /// soon as [isDirty] flips from `true` to `false`. |
| 40 Stream get onDone => _onDone; |
| 41 Stream _onDone; |
| 40 | 42 |
| 41 /// A stream that emits any new assets emitted by [this]. | 43 /// A stream that emits any new assets emitted by [this]. |
| 42 /// | 44 /// |
| 43 /// Assets are emitted synchronously to ensure that any changes are thoroughly | 45 /// Assets are emitted synchronously to ensure that any changes are thoroughly |
| 44 /// propagated as soon as they occur. | 46 /// propagated as soon as they occur. |
| 45 Stream<AssetNode> get onAsset => _onAsset; | 47 Stream<AssetNode> get onAsset => _onAsset; |
| 46 Stream<AssetNode> _onAsset; | 48 Stream<AssetNode> _onAsset; |
| 47 | 49 |
| 48 /// A stream that emits an event whenever any transforms in this group logs | 50 /// A stream that emits an event whenever any transforms in this group logs |
| 49 /// an entry. | 51 /// an entry. |
| 50 Stream<LogEntry> get onLog => _onLogPool.stream; | 52 Stream<LogEntry> get onLog => _onLogPool.stream; |
| 51 final _onLogPool = new StreamPool<LogEntry>.broadcast(); | 53 final _onLogPool = new StreamPool<LogEntry>.broadcast(); |
| 52 | 54 |
| 53 GroupRunner(Phase previous, this._group, this._location) { | 55 GroupRunner(AssetCascade cascade, this._group, this._location) { |
| 54 _addPhase(previous.addPhase(_location), []); | 56 _addPhase(new Phase(cascade, _location), []); |
| 55 for (var phase in _group.phases) { | 57 for (var phase in _group.phases) { |
| 56 _addPhase(_phases.last.addPhase(), phase); | 58 _addPhase(_phases.last.addPhase(), phase); |
| 57 } | 59 } |
| 58 | 60 |
| 59 _onAsset = _phases.last.onAsset; | 61 _onAsset = _phases.last.onAsset; |
| 60 _onStatusChange = _phases.last.onStatusChange; | 62 _onDone = _phases.last.onDone; |
| 61 } | 63 } |
| 62 | 64 |
| 63 /// Add a phase with [contents] to [this]'s list of phases. | 65 /// Add a phase with [contents] to [this]'s list of phases. |
| 64 /// | 66 /// |
| 65 /// [contents] should be an inner [Iterable] from a [TransformGroup.phases] | 67 /// [contents] should be an inner [Iterable] from a [TransformGroup.phases] |
| 66 /// value. | 68 /// value. |
| 67 void _addPhase(Phase phase, Iterable contents) { | 69 void _addPhase(Phase phase, Iterable contents) { |
| 68 _phases.add(phase); | 70 _phases.add(phase); |
| 69 _onLogPool.add(phase.onLog); | 71 _onLogPool.add(phase.onLog); |
| 70 phase.updateTransformers(contents); | 72 phase.updateTransformers(contents); |
| 71 } | 73 } |
| 72 | 74 |
| 73 /// Force all [LazyTransformer]s' transforms in this group to begin producing | 75 /// Force all [LazyTransformer]s' transforms in this group to begin producing |
| 74 /// concrete assets. | 76 /// concrete assets. |
| 75 void forceAllTransforms() { | 77 void forceAllTransforms() { |
| 76 for (var phase in _phases) { | 78 for (var phase in _phases) { |
| 77 phase.forceAllTransforms(); | 79 phase.forceAllTransforms(); |
| 78 } | 80 } |
| 79 } | 81 } |
| 80 | 82 |
| 83 /// Adds a new asset as an input for this group. |
| 84 void addInput(AssetNode node) { |
| 85 _phases.first.addInput(node); |
| 86 } |
| 87 |
| 81 /// Removes this group and all sub-phases within it. | 88 /// Removes this group and all sub-phases within it. |
| 82 void remove() { | 89 void remove() { |
| 83 _onLogPool.close(); | |
| 84 for (var phase in _phases) { | 90 for (var phase in _phases) { |
| 85 phase.remove(); | 91 phase.remove(); |
| 86 } | 92 } |
| 87 } | 93 } |
| 88 | 94 |
| 89 String toString() => "group in phase $_location for $_group"; | 95 String toString() => "group in phase $_location for $_group"; |
| 90 } | 96 } |
| OLD | NEW |