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 import 'dart:async'; |
| 6 |
| 7 import 'package:barback/barback.dart'; |
| 8 import 'package:barback/src/utils.dart'; |
| 9 |
| 10 /// The abstract base class for transformers used to test barback. |
| 11 /// |
| 12 /// This adds the ability to pause and resume different components of the |
| 13 /// transformers, and to tell whether they're running, when they start running, |
| 14 /// and how many times they've run. |
| 15 /// |
| 16 /// Transformers extending this should override [doIsPrimary] and [doApply] |
| 17 /// rather than [isPrimary] and [apply], and they should use [getInput] and |
| 18 /// [getPrimary] rather than [transform.getInput] and [transform.primaryInput]. |
| 19 abstract class MockTransformer extends Transformer { |
| 20 /// The number of times the transformer has been applied. |
| 21 int get numRuns => _numRuns; |
| 22 var _numRuns = 0; |
| 23 |
| 24 /// The number of currently running transforms. |
| 25 int _runningTransforms = 0; |
| 26 |
| 27 // A completer for pausing the transformer before it finishes running [apply]. |
| 28 Completer _apply; |
| 29 |
| 30 // Completers for pausing the transformer before it finishes running |
| 31 // [isPrimary]. |
| 32 final _isPrimary = new Map<AssetId, Completer>(); |
| 33 |
| 34 // Completers for pausing the transformer before it finishes getting inputs |
| 35 // the [Transform]. |
| 36 final _getInput = new Map<AssetId, Completer>(); |
| 37 |
| 38 /// A future that completes when this transformer begins running. |
| 39 /// |
| 40 /// Once this transformer finishes running, this is reset to a new completer, |
| 41 /// so it can be used multiple times. |
| 42 Future get started => _started.future; |
| 43 var _started = new Completer(); |
| 44 |
| 45 /// `true` if any transforms are currently running. |
| 46 bool get isRunning => _runningTransforms > 0; |
| 47 |
| 48 /// Causes the transformer to pause after running [apply] but before the |
| 49 /// returned Future completes. |
| 50 /// |
| 51 /// This can be resumed by calling [resumeApply]. |
| 52 void pauseApply() { |
| 53 _apply = new Completer(); |
| 54 } |
| 55 |
| 56 /// Resumes the transformer's [apply] call after [pauseApply] was called. |
| 57 void resumeApply() { |
| 58 _apply.complete(); |
| 59 _apply = null; |
| 60 } |
| 61 |
| 62 /// Causes the transformer to pause after running [isPrimary] on the asset |
| 63 /// with the given [name], but before the returned Future completes. |
| 64 /// |
| 65 /// This can be resumed by calling [resumeIsPrimary]. |
| 66 void pauseIsPrimary(String name) { |
| 67 _isPrimary[new AssetId.parse(name)] = new Completer(); |
| 68 } |
| 69 |
| 70 /// Resumes the transformer's [isPrimary] call on the asset with the given |
| 71 /// [name] after [pauseIsPrimary] was called. |
| 72 void resumeIsPrimary(String name) { |
| 73 _isPrimary.remove(new AssetId.parse(name)).complete(); |
| 74 } |
| 75 |
| 76 /// Causes the transformer to pause while loading the input with the given |
| 77 /// [name]. This can be the primary input or a secondary input. |
| 78 /// |
| 79 /// This can be resumed by calling [resumeGetInput]. |
| 80 void pauseGetInput(String name) { |
| 81 _getInput[new AssetId.parse(name)] = new Completer(); |
| 82 } |
| 83 |
| 84 /// Resumes the transformer's loading of the input with the given [name] after |
| 85 /// [pauseGetInput] was called. |
| 86 void resumeGetInput(String name) { |
| 87 _getInput.remove(new AssetId.parse(name)).complete(); |
| 88 } |
| 89 |
| 90 /// Like [Transform.getInput], but respects [pauseGetInput]. |
| 91 /// |
| 92 /// This is intended for use by subclasses of [MockTransformer]. |
| 93 Future<Asset> getInput(Transform transform, AssetId id) { |
| 94 return newFuture(() { |
| 95 if (_getInput.containsKey(id)) return _getInput[id].future; |
| 96 }).then((_) => transform.getInput(id)); |
| 97 } |
| 98 |
| 99 /// Like [Transform.primaryInput], but respects [pauseGetInput]. |
| 100 /// |
| 101 /// This is intended for use by subclasses of [MockTransformer]. |
| 102 Future<Asset> getPrimary(Transform transform) => |
| 103 getInput(transform, transform.primaryId); |
| 104 |
| 105 Future<bool> isPrimary(Asset asset) { |
| 106 return newFuture(() => doIsPrimary(asset)).then((result) { |
| 107 return newFuture(() { |
| 108 if (_isPrimary.containsKey(asset.id)) { |
| 109 return _isPrimary[asset.id].future; |
| 110 } |
| 111 }).then((_) => result); |
| 112 }); |
| 113 } |
| 114 |
| 115 Future apply(Transform transform) { |
| 116 _numRuns++; |
| 117 if (_runningTransforms == 0) _started.complete(); |
| 118 _runningTransforms++; |
| 119 return newFuture(() => doApply(transform)).then((_) { |
| 120 if (_apply != null) return _apply.future; |
| 121 }).whenComplete(() { |
| 122 _runningTransforms--; |
| 123 if (_runningTransforms == 0) _started = new Completer(); |
| 124 }); |
| 125 } |
| 126 |
| 127 /// The wrapped version of [isPrimary] for subclasses to override. |
| 128 Future<bool> doIsPrimary(Asset asset); |
| 129 |
| 130 /// The wrapped version of [doApply] for subclasses to override. |
| 131 Future doApply(Transform transform); |
| 132 } |
OLD | NEW |