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.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'; |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 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 /// The number of currently running transforms. | 30 /// The number of currently running transforms. |
| 31 int _runningTransforms = 0; | 31 int _runningTransforms = 0; |
| 32 | 32 |
| 33 // A completer for pausing the transformer before it finishes running [apply]. | 33 /// A completer for pausing the transformer before it finishes running [apply] . |
| 34 Completer _apply; | 34 Completer _apply; |
| 35 | 35 |
| 36 // Completers for pausing the transformer before it finishes running | 36 /// Completers for pausing the transformer before it finishes running |
| 37 // [isPrimary]. | 37 /// [isPrimary]. |
| 38 final _isPrimary = new Map<AssetId, Completer>(); | 38 final _isPrimary = new Map<AssetId, Completer>(); |
| 39 | 39 |
| 40 // Completers for pausing the transformer before it finishes getting inputs | 40 /// Completers for pausing the transformer before it finishes getting inputs |
| 41 // the [Transform]. | 41 /// the [Transform]. |
| 42 final _getInput = new Map<AssetId, Completer>(); | 42 final _getInput = new Map<AssetId, Completer>(); |
| 43 | 43 |
| 44 /// Completer for pausing the transformer before it accesses [primaryInput]. | |
| 45 Completer _getPrimary; | |
| 46 | |
| 44 /// A completer that completes once this transformer begins running. | 47 /// A completer that completes once this transformer begins running. |
| 45 /// | 48 /// |
| 46 /// Once this transformer finishes running, this is reset to a new completer, | 49 /// Once this transformer finishes running, this is reset to a new completer, |
| 47 /// so it can be used multiple times. | 50 /// so it can be used multiple times. |
| 48 var _started = new Completer(); | 51 var _started = new Completer(); |
| 49 | 52 |
| 50 /// `true` if any transforms are currently running. | 53 /// `true` if any transforms are currently running. |
| 51 /// | 54 /// |
| 52 /// This is scheduled. The Future will complete at the point in the schedule | 55 /// This is scheduled. The Future will complete at the point in the schedule |
| 53 /// that this is called. | 56 /// that this is called. |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 92 /// Resumes the transformer's [isPrimary] call on the asset with the given | 95 /// Resumes the transformer's [isPrimary] call on the asset with the given |
| 93 /// [name] after [pauseIsPrimary] was called. | 96 /// [name] after [pauseIsPrimary] was called. |
| 94 /// | 97 /// |
| 95 /// This operation is scheduled. | 98 /// This operation is scheduled. |
| 96 void resumeIsPrimary(String name) { | 99 void resumeIsPrimary(String name) { |
| 97 schedule(() { | 100 schedule(() { |
| 98 _isPrimary.remove(new AssetId.parse(name)).complete(); | 101 _isPrimary.remove(new AssetId.parse(name)).complete(); |
| 99 }, "resume isPrimary($name) for $this"); | 102 }, "resume isPrimary($name) for $this"); |
| 100 } | 103 } |
| 101 | 104 |
| 102 /// Causes the transformer to pause while loading the input with the given | 105 /// Causes the transformer to pause while loading the secondary input with |
| 103 /// [name]. This can be the primary input or a secondary input. | 106 /// the given [name]. |
| 104 /// | 107 /// |
| 105 /// This can be resumed by calling [resumeGetInput]. This operation is | 108 /// This can be resumed by calling [resumeGetInput]. This operation is |
| 106 /// scheduled. | 109 /// scheduled. |
| 107 void pauseGetInput(String name) { | 110 void pauseGetInput(String name) { |
| 108 schedule(() { | 111 schedule(() { |
| 109 _getInput[new AssetId.parse(name)] = new Completer(); | 112 _getInput[new AssetId.parse(name)] = new Completer(); |
| 110 }, "pause getInput($name) for $this"); | 113 }, "pause getInput($name) for $this"); |
| 111 } | 114 } |
| 112 | 115 |
| 113 /// Resumes the transformer's loading of the input with the given [name] after | 116 /// Resumes the transformer's loading of the input with the given [name] after |
| 114 /// [pauseGetInput] was called. | 117 /// [pauseGetInput] was called. |
| 115 /// | 118 /// |
| 116 /// This operation is scheduled. | 119 /// This operation is scheduled. |
| 117 void resumeGetInput(String name) { | 120 void resumeGetInput(String name) { |
| 118 schedule(() { | 121 schedule(() { |
| 119 _getInput.remove(new AssetId.parse(name)).complete(); | 122 _getInput.remove(new AssetId.parse(name)).complete(); |
| 120 }, "resume getInput($name) for $this"); | 123 }, "resume getInput($name) for $this"); |
| 121 } | 124 } |
| 122 | 125 |
| 126 /// Causes the transformer to pause before accessing [primaryInput]. | |
| 127 /// | |
| 128 /// This can be resumed by calling [resumeGetPrimary]. This operation is | |
| 129 /// scheduled. | |
| 130 void pauseGetPrimary() { | |
|
nweiz
2013/08/28 21:15:00
I don't like making these separate from [pauseGetI
Bob Nystrom
2013/08/28 22:38:24
I think it's important to split this out. This met
| |
| 131 schedule(() { | |
| 132 _getPrimary = new Completer(); | |
| 133 }, "pause primaryInput for $this"); | |
| 134 } | |
| 135 | |
| 136 /// Resumes the transformer's invocation of [primaryInput] after | |
| 137 /// [pauseGetPrimary] was called. | |
| 138 /// | |
| 139 /// This operation is scheduled. | |
| 140 void resumeGetPrimary() { | |
| 141 schedule(() { | |
| 142 _getPrimary.complete(); | |
| 143 _getPrimary = null; | |
| 144 }, "resume getPrimary() for $this"); | |
| 145 } | |
| 146 | |
| 123 /// Like [Transform.getInput], but respects [pauseGetInput]. | 147 /// Like [Transform.getInput], but respects [pauseGetInput]. |
| 124 /// | 148 /// |
| 125 /// This is intended for use by subclasses of [MockTransformer]. | 149 /// This is intended for use by subclasses of [MockTransformer]. |
| 126 Future<Asset> getInput(Transform transform, AssetId id) { | 150 Future<Asset> getInput(Transform transform, AssetId id) { |
| 127 return newFuture(() { | 151 return newFuture(() { |
| 128 if (_getInput.containsKey(id)) return _getInput[id].future; | 152 if (_getInput.containsKey(id)) return _getInput[id].future; |
| 129 }).then((_) => transform.getInput(id)); | 153 }).then((_) => transform.getInput(id)); |
| 130 } | 154 } |
| 131 | 155 |
| 132 /// Like [Transform.primaryInput], but respects [pauseGetInput]. | 156 /// Like [Transform.primaryInput], but respects [pauseGetPrimary]. |
| 133 /// | 157 /// |
| 134 /// This is intended for use by subclasses of [MockTransformer]. | 158 /// This is intended for use by subclasses of [MockTransformer]. |
| 135 Future<Asset> getPrimary(Transform transform) => | 159 Future<Asset> getPrimary(Transform transform) { |
| 136 getInput(transform, transform.primaryId); | 160 return newFuture(() { |
| 161 if (_getPrimary != null) return _getPrimary.future; | |
| 162 }).then((_) => transform.primaryInput); | |
| 163 } | |
| 137 | 164 |
| 138 Future<bool> isPrimary(Asset asset) { | 165 Future<bool> isPrimary(Asset asset) { |
| 139 return newFuture(() => doIsPrimary(asset)).then((result) { | 166 return newFuture(() => doIsPrimary(asset)).then((result) { |
| 140 return newFuture(() { | 167 return newFuture(() { |
| 141 if (_isPrimary.containsKey(asset.id)) { | 168 if (_isPrimary.containsKey(asset.id)) { |
| 142 return _isPrimary[asset.id].future; | 169 return _isPrimary[asset.id].future; |
| 143 } | 170 } |
| 144 }).then((_) => result); | 171 }).then((_) => result); |
| 145 }); | 172 }); |
| 146 } | 173 } |
| 147 | 174 |
| 148 Future apply(Transform transform) { | 175 Future apply(Transform transform) { |
| 149 _numRuns++; | 176 _numRuns++; |
| 150 if (_runningTransforms == 0) _started.complete(); | 177 if (_runningTransforms == 0) _started.complete(); |
| 151 _runningTransforms++; | 178 _runningTransforms++; |
| 152 return newFuture(() => doApply(transform)).then((_) { | 179 return newFuture(() => doApply(transform)).then((_) { |
| 153 if (_apply != null) return _apply.future; | 180 if (_apply != null) return _apply.future; |
| 154 }).whenComplete(() { | 181 }).whenComplete(() { |
| 155 _runningTransforms--; | 182 _runningTransforms--; |
| 156 if (_runningTransforms == 0) _started = new Completer(); | 183 if (_runningTransforms == 0) _started = new Completer(); |
| 157 }); | 184 }); |
| 158 } | 185 } |
| 159 | 186 |
| 160 /// The wrapped version of [isPrimary] for subclasses to override. | 187 /// The wrapped version of [isPrimary] for subclasses to override. |
| 161 Future<bool> doIsPrimary(Asset asset); | 188 Future<bool> doIsPrimary(Asset asset); |
| 162 | 189 |
| 163 /// The wrapped version of [doApply] for subclasses to override. | 190 /// The wrapped version of [doApply] for subclasses to override. |
| 164 Future doApply(Transform transform); | 191 Future doApply(Transform transform); |
| 165 } | 192 } |
| OLD | NEW |