| 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.test.transformer.mock; | 5 library barback.test.transformer.mock; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:barback/barback.dart'; | 9 import 'package:barback/barback.dart'; |
| 10 import 'package:barback/src/utils.dart'; | 10 import 'package:barback/src/utils.dart'; |
| 11 import 'package:scheduled_test/scheduled_test.dart'; | 11 import 'package:scheduled_test/scheduled_test.dart'; |
| 12 | 12 |
| 13 /// The abstract base class for transformers used to test barback. | 13 /// The abstract base class for transformers used to test barback. |
| 14 /// | 14 /// |
| 15 /// This adds the ability to pause and resume different components of the | 15 /// This adds the ability to pause and resume different components of the |
| 16 /// transformers, and to tell whether they're running, when they start running, | 16 /// transformers, and to tell whether they're running, when they start running, |
| 17 /// and how many times they've run. | 17 /// and how many times they've run. |
| 18 /// | 18 /// |
| 19 /// Transformers extending this should override [doIsPrimary] and [doApply] | 19 /// Transformers extending this should override [doIsPrimary] and [doApply] |
| 20 /// rather than [isPrimary] and [apply], and they should use [getInput] and | 20 /// rather than [isPrimary] and [apply], and they should use [getInput] and |
| 21 /// [getPrimary] rather than [transform.getInput] and [transform.primaryInput]. | 21 /// [getPrimary] rather than [transform.getInput] and [transform.primaryInput]. |
| 22 abstract class MockTransformer extends Transformer { | 22 abstract class MockTransformer extends Transformer { |
| 23 /// The number of times the transformer has been applied. | 23 /// The number of times the transformer has been applied. |
| 24 /// | 24 /// |
| 25 /// This is scheduled. The Future will complete at the point in the schedule | 25 /// This is scheduled. The Future will complete at the point in the schedule |
| 26 /// that this is called. | 26 /// that this is called. |
| 27 Future<int> get numRuns => schedule(() => _numRuns); | 27 Future<int> get numRuns => schedule(() => _numRuns); |
| 28 var _numRuns = 0; | 28 var _numRuns = 0; |
| 29 | 29 |
| 30 Future<int> get maxParallelRuns => schedule(() => _maxParallelRuns); |
| 31 var _maxParallelRuns = 0; |
| 32 |
| 30 /// The number of currently running transforms. | 33 /// The number of currently running transforms. |
| 31 int _runningTransforms = 0; | 34 int _runningTransforms = 0; |
| 32 | 35 |
| 33 /// A completer for pausing the transformer before it finishes running [apply]
. | 36 /// A completer for pausing the transformer before it finishes running [apply]
. |
| 34 Completer _apply; | 37 Completer _apply; |
| 35 | 38 |
| 36 /// Completers for pausing the transformer before it finishes running | 39 /// Completers for pausing the transformer before it finishes running |
| 37 /// [isPrimary]. | 40 /// [isPrimary]. |
| 38 final _isPrimary = new Map<AssetId, Completer>(); | 41 final _isPrimary = new Map<AssetId, Completer>(); |
| 39 | 42 |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 return _isPrimary[asset.id].future; | 172 return _isPrimary[asset.id].future; |
| 170 } | 173 } |
| 171 }).then((_) => result); | 174 }).then((_) => result); |
| 172 }); | 175 }); |
| 173 } | 176 } |
| 174 | 177 |
| 175 Future apply(Transform transform) { | 178 Future apply(Transform transform) { |
| 176 _numRuns++; | 179 _numRuns++; |
| 177 if (_runningTransforms == 0) _started.complete(); | 180 if (_runningTransforms == 0) _started.complete(); |
| 178 _runningTransforms++; | 181 _runningTransforms++; |
| 182 if (_runningTransforms > _maxParallelRuns) { |
| 183 _maxParallelRuns = _runningTransforms; |
| 184 } |
| 179 return newFuture(() => doApply(transform)).then((_) { | 185 return newFuture(() => doApply(transform)).then((_) { |
| 180 if (_apply != null) return _apply.future; | 186 if (_apply != null) return _apply.future; |
| 181 }).whenComplete(() { | 187 }).whenComplete(() { |
| 182 _runningTransforms--; | 188 _runningTransforms--; |
| 183 if (_runningTransforms == 0) _started = new Completer(); | 189 if (_runningTransforms == 0) _started = new Completer(); |
| 184 }); | 190 }); |
| 185 } | 191 } |
| 186 | 192 |
| 187 /// The wrapped version of [isPrimary] for subclasses to override. | 193 /// The wrapped version of [isPrimary] for subclasses to override. |
| 188 Future<bool> doIsPrimary(Asset asset); | 194 Future<bool> doIsPrimary(Asset asset); |
| 189 | 195 |
| 190 /// The wrapped version of [doApply] for subclasses to override. | 196 /// The wrapped version of [doApply] for subclasses to override. |
| 191 Future doApply(Transform transform); | 197 Future doApply(Transform transform); |
| 192 } | 198 } |
| OLD | NEW |