Chromium Code Reviews| 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.asset_cascade; | 5 library barback.asset_cascade; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'asset.dart'; | 9 import 'asset.dart'; |
| 10 import 'asset_id.dart'; | 10 import 'asset_id.dart'; |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 41 /// cascade's package's source assets. | 41 /// cascade's package's source assets. |
| 42 final _sourceControllerMap = new Map<AssetId, AssetNodeController>(); | 42 final _sourceControllerMap = new Map<AssetId, AssetNodeController>(); |
| 43 | 43 |
| 44 /// Futures for source assets that are currently being loaded. | 44 /// Futures for source assets that are currently being loaded. |
| 45 /// | 45 /// |
| 46 /// These futures are cancelable so that if an asset is updated after a load | 46 /// These futures are cancelable so that if an asset is updated after a load |
| 47 /// has been kicked off, the previous load can be ignored in favor of a new | 47 /// has been kicked off, the previous load can be ignored in favor of a new |
| 48 /// one. | 48 /// one. |
| 49 final _loadingSources = new Map<AssetId, CancelableFuture<Asset>>(); | 49 final _loadingSources = new Map<AssetId, CancelableFuture<Asset>>(); |
| 50 | 50 |
| 51 /// The list of phases in this cascade. | |
| 52 /// | |
| 53 /// This will always contain at least one phase, and the first phase will | |
| 54 /// never have any transformers. This ensures that every transformer can | |
| 55 /// request inputs from a previous phase. | |
| 51 final _phases = <Phase>[]; | 56 final _phases = <Phase>[]; |
| 52 | 57 |
| 58 /// The subscription to the [Phase.onDone] stream of the last [Phase] in | |
| 59 /// [_phases]. | |
| 60 StreamSubscription _phaseOnDoneSubscription; | |
| 61 | |
| 53 /// A stream that emits any errors from the cascade or the transformers. | 62 /// A stream that emits any errors from the cascade or the transformers. |
| 54 /// | 63 /// |
| 55 /// This emits errors as they're detected. If an error occurs in one part of | 64 /// This emits errors as they're detected. If an error occurs in one part of |
| 56 /// the cascade, unrelated parts will continue building. | 65 /// the cascade, unrelated parts will continue building. |
| 57 Stream<BarbackException> get errors => _errorsController.stream; | 66 Stream<BarbackException> get errors => _errorsController.stream; |
| 58 final _errorsController = | 67 final _errorsController = |
| 59 new StreamController<BarbackException>.broadcast(sync: true); | 68 new StreamController<BarbackException>.broadcast(sync: true); |
| 60 | 69 |
| 61 /// A stream that emits an event whenever any transforms in this cascade logs | 70 /// A stream that emits an event whenever any transforms in this cascade logs |
| 62 /// an entry. | 71 /// an entry. |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 162 }); | 171 }); |
| 163 } | 172 } |
| 164 | 173 |
| 165 /// Sets this cascade's transformer phases to [transformers]. | 174 /// Sets this cascade's transformer phases to [transformers]. |
| 166 /// | 175 /// |
| 167 /// Elements of the inner iterable of [transformers] must be either | 176 /// Elements of the inner iterable of [transformers] must be either |
| 168 /// [Transformer]s or [TransformerGroup]s. | 177 /// [Transformer]s or [TransformerGroup]s. |
| 169 void updateTransformers(Iterable<Iterable> transformersIterable) { | 178 void updateTransformers(Iterable<Iterable> transformersIterable) { |
| 170 var transformers = transformersIterable.toList(); | 179 var transformers = transformersIterable.toList(); |
| 171 | 180 |
| 172 for (var i = 0; i < transformers.length; i++) { | 181 // Always preserve a single phase with no transformers at the beginning of |
| 182 // the cascade so that [TransfomNode]s in the first populated phase will | |
|
Bob Nystrom
2014/03/15 01:03:55
Transfom -> Transform
| |
| 183 // have something to request assets from. | |
| 184 for (var i = 1; i <= transformers.length; i++) { | |
|
Bob Nystrom
2014/03/15 01:03:55
I think it would be clearer to keep i ranging from
| |
| 173 if (_phases.length > i) { | 185 if (_phases.length > i) { |
| 174 _phases[i].updateTransformers(transformers[i]); | 186 _phases[i].updateTransformers(transformers[i - 1]); |
| 175 continue; | 187 continue; |
| 176 } | 188 } |
| 177 | 189 |
| 178 var phase = _phases.last.addPhase(); | 190 var phase = _phases.last.addPhase(); |
| 179 _addPhase(phase); | 191 _addPhase(phase); |
| 180 phase.updateTransformers(transformers[i]); | 192 phase.updateTransformers(transformers[i - 1]); |
| 181 } | 193 } |
| 182 | 194 |
| 183 if (transformers.length == 0) { | 195 for (var i = transformers.length + 1; i < _phases.length; i++) { |
| 184 _phases.last.updateTransformers([]); | 196 _phases[i].remove(); |
| 185 } else { | |
| 186 for (var i = transformers.length; i < _phases.length; i++) { | |
| 187 _phases[i].remove(); | |
| 188 } | |
| 189 _phases.removeRange(transformers.length, _phases.length); | |
| 190 } | 197 } |
| 198 _phases.removeRange(transformers.length + 1, _phases.length); | |
| 199 | |
| 200 _phaseOnDoneSubscription.cancel(); | |
| 201 _phaseOnDoneSubscription = _phases.last.onDone | |
| 202 .listen(_onDoneController.add); | |
| 191 } | 203 } |
| 192 | 204 |
| 193 /// Force all [LazyTransformer]s' transforms in this cascade to begin | 205 /// Force all [LazyTransformer]s' transforms in this cascade to begin |
| 194 /// producing concrete assets. | 206 /// producing concrete assets. |
| 195 void forceAllTransforms() { | 207 void forceAllTransforms() { |
| 196 for (var phase in _phases) { | 208 for (var phase in _phases) { |
| 197 phase.forceAllTransforms(); | 209 phase.forceAllTransforms(); |
| 198 } | 210 } |
| 199 } | 211 } |
| 200 | 212 |
| 201 void reportError(BarbackException error) { | 213 void reportError(BarbackException error) { |
| 202 _errorsController.add(error); | 214 _errorsController.add(error); |
| 203 } | 215 } |
| 204 | 216 |
| 205 /// Add [phase] to the end of [_phases] and watch its streams. | 217 /// Add [phase] to the end of [_phases] and watch its streams. |
| 206 void _addPhase(Phase phase) { | 218 void _addPhase(Phase phase) { |
| 207 _onLogPool.add(phase.onLog); | 219 _onLogPool.add(phase.onLog); |
| 208 phase.onDone.listen((_) { | 220 if (_phaseOnDoneSubscription != null) _phaseOnDoneSubscription.cancel(); |
| 209 if (!isDirty) _onDoneController.add(null); | 221 _phaseOnDoneSubscription = phase.onDone.listen(_onDoneController.add); |
| 210 }); | |
| 211 | 222 |
| 212 _phases.add(phase); | 223 _phases.add(phase); |
| 213 } | 224 } |
| 214 | 225 |
| 215 String toString() => "cascade for $package"; | 226 String toString() => "cascade for $package"; |
| 216 } | 227 } |
| OLD | NEW |