Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(165)

Side by Side Diff: pkg/barback/test/transformer/mock_aggregate.dart

Issue 299833003: Expose aggregate transformers in barback. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, 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_aggregate;
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 aggregate 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 [doClassifyPrimary] and
20 /// rather than [isPrimary] and [apply], and they should use [getInput] and 20 /// [doApply] rather than [classifyPrimary] and [apply], and they should use
21 /// [getPrimary] rather than [transform.getInput] and [transform.primaryInput]. 21 /// [getInput] and [getPrimaryInputs] rather than [transform.getInput] and
22 abstract class MockTransformer extends Transformer { 22 /// [transform.primaryInputs].
23 abstract class MockAggregateTransformer extends AggregateTransformer {
23 /// The number of times the transformer has been applied. 24 /// The number of times the transformer has been applied.
24 /// 25 ///
25 /// This is scheduled. The Future will complete at the point in the schedule 26 /// This is scheduled. The Future will complete at the point in the schedule
26 /// that this is called. 27 /// that this is called.
27 Future<int> get numRuns => schedule(() => _numRuns); 28 Future<int> get numRuns => schedule(() => _numRuns);
28 var _numRuns = 0; 29 var _numRuns = 0;
29 30
30 /// The number of currently running transforms. 31 /// The number of currently running transforms.
31 int _runningTransforms = 0; 32 int _runningTransforms = 0;
32 33
33 /// A completer for pausing the transformer before it finishes running [apply] . 34 /// A completer for pausing the transformer before it finishes running [apply] .
Bob Nystrom 2014/05/27 17:20:18 Long line (probably in the mock_transformer too).
nweiz 2014/05/27 21:03:10 Done.
34 Completer _apply; 35 Completer _apply;
35 36
36 /// Completers for pausing the transformer before it finishes running 37 /// Completers for pausing the transformer before it finishes running
37 /// [isPrimary]. 38 /// [classifyPrimary].
38 final _isPrimary = new Map<AssetId, Completer>(); 39 final _classifyPrimary = new Map<AssetId, Completer>();
39 40
40 /// Completers for pausing the transformer before it finishes getting inputs 41 /// Completers for pausing the transformer before it finishes getting inputs
41 /// the [Transform]. 42 /// the [Transform].
42 final _getInput = new Map<AssetId, Completer>(); 43 final _getInput = new Map<AssetId, Completer>();
43 44
44 /// Completer for pausing the transformer before it accesses [primaryInput]. 45 /// Completer for pausing the transformer before it accesses
45 Completer _primaryInput; 46 /// [getPrimaryInputs].
47 Completer _primaryInputs;
46 48
47 /// A completer that completes once this transformer begins running. 49 /// A completer that completes once this transformer begins running.
48 /// 50 ///
49 /// Once this transformer finishes running, this is reset to a new completer, 51 /// Once this transformer finishes running, this is reset to a new completer,
50 /// so it can be used multiple times. 52 /// so it can be used multiple times.
51 var _started = new Completer(); 53 var _started = new Completer();
52 54
53 /// `true` if any transforms are currently running. 55 /// `true` if any transforms are currently running.
54 /// 56 ///
55 /// This is scheduled. The Future will complete at the point in the schedule 57 /// This is scheduled. The Future will complete at the point in the schedule
56 /// that this is called. 58 /// that this is called.
57 Future<bool> get isRunning => schedule(() => _runningTransforms > 0); 59 Future<bool> get isRunning => schedule(() => _runningTransforms > 0);
58 60
59 /// If this is set to `true`, the transformer will consume its primary input 61 /// All elements of this set will be automatically consumed during [apply].
60 /// during [apply]. 62 final consumePrimaries = new Set<String>();
61 bool consumePrimary = false;
62 63
63 /// Pauses the schedule until this transformer begins running. 64 /// Pauses the schedule until this transformer begins running.
64 void waitUntilStarted() { 65 void waitUntilStarted() {
65 schedule(() => _started.future, "wait until $this starts"); 66 schedule(() => _started.future, "wait until $this starts");
66 } 67 }
67 68
68 /// Causes the transformer to pause after running [apply] but before the 69 /// Causes the transformer to pause after running [apply] but before the
69 /// returned Future completes. 70 /// returned Future completes.
70 /// 71 ///
71 /// This can be resumed by calling [resumeApply]. This operation is scheduled. 72 /// This can be resumed by calling [resumeApply]. This operation is scheduled.
72 void pauseApply() { 73 void pauseApply() {
73 schedule(() { 74 schedule(() {
74 _apply = new Completer(); 75 _apply = new Completer();
75 }, "pause apply for $this"); 76 }, "pause apply for $this");
76 } 77 }
77 78
78 /// Resumes the transformer's [apply] call after [pauseApply] was called. 79 /// Resumes the transformer's [apply] call after [pauseApply] was called.
79 /// 80 ///
80 /// This operation is scheduled. 81 /// This operation is scheduled.
81 void resumeApply() { 82 void resumeApply() {
82 schedule(() { 83 schedule(() {
83 _apply.complete(); 84 _apply.complete();
84 _apply = null; 85 _apply = null;
85 }, "resume apply for $this"); 86 }, "resume apply for $this");
86 } 87 }
87 88
88 /// Causes the transformer to pause after running [isPrimary] on the asset 89 /// Causes the transformer to pause after running [classifyPrimary] on the
89 /// with the given [name], but before the returned Future completes. 90 /// asset with the given [name], but before the returned Future completes.
90 /// 91 ///
91 /// This can be resumed by calling [resumeIsPrimary]. This operation is 92 /// This can be resumed by calling [resumeClassifyPrimary]. This operation is
92 /// scheduled. 93 /// scheduled.
93 void pauseIsPrimary(String name) { 94 void pauseClassifyPrimary(String name) {
94 schedule(() { 95 schedule(() {
95 _isPrimary[new AssetId.parse(name)] = new Completer(); 96 _classifyPrimary[new AssetId.parse(name)] = new Completer();
96 }, "pause isPrimary($name) for $this"); 97 }, "pause classifyPrimary($name) for $this");
97 } 98 }
98 99
99 /// Resumes the transformer's [isPrimary] call on the asset with the given 100 /// Resumes the transformer's [classifyPrimary] call on the asset with the giv en
Bob Nystrom 2014/05/27 17:20:18 Long line.
nweiz 2014/05/27 21:03:10 Done.
100 /// [name] after [pauseIsPrimary] was called. 101 /// [name] after [pauseClassifyPrimary] was called.
101 /// 102 ///
102 /// This operation is scheduled. 103 /// This operation is scheduled.
103 void resumeIsPrimary(String name) { 104 void resumeClassifyPrimary(String name) {
104 schedule(() { 105 schedule(() {
105 _isPrimary.remove(new AssetId.parse(name)).complete(); 106 _classifyPrimary.remove(new AssetId.parse(name)).complete();
106 }, "resume isPrimary($name) for $this"); 107 }, "resume classifyPrimary($name) for $this");
107 } 108 }
108 109
109 /// Causes the transformer to pause while loading the secondary input with 110 /// Causes the transformer to pause while loading the secondary input with
110 /// the given [name]. 111 /// the given [name].
111 /// 112 ///
112 /// This can be resumed by calling [resumeGetInput]. This operation is 113 /// This can be resumed by calling [resumeGetInput]. This operation is
113 /// scheduled. 114 /// scheduled.
114 void pauseGetInput(String name) { 115 void pauseGetInput(String name) {
115 schedule(() { 116 schedule(() {
116 _getInput[new AssetId.parse(name)] = new Completer(); 117 _getInput[new AssetId.parse(name)] = new Completer();
117 }, "pause getInput($name) for $this"); 118 }, "pause getInput($name) for $this");
118 } 119 }
119 120
120 /// Resumes the transformer's loading of the input with the given [name] after 121 /// Resumes the transformer's loading of the input with the given [name] after
121 /// [pauseGetInput] was called. 122 /// [pauseGetInput] was called.
122 /// 123 ///
123 /// This operation is scheduled. 124 /// This operation is scheduled.
124 void resumeGetInput(String name) { 125 void resumeGetInput(String name) {
125 schedule(() { 126 schedule(() {
126 _getInput.remove(new AssetId.parse(name)).complete(); 127 _getInput.remove(new AssetId.parse(name)).complete();
127 }, "resume getInput($name) for $this"); 128 }, "resume getInput($name) for $this");
128 } 129 }
129 130
130 /// Causes the transformer to pause before accessing [primaryInput]. 131 /// Causes the transformer to pause before accessing [getPrimaryInputs].
131 /// 132 ///
132 /// This can be resumed by calling [resumeGetPrimary]. This operation is 133 /// This can be resumed by calling [resumePrimaryInputs]. This operation is
133 /// scheduled. 134 /// scheduled.
134 void pausePrimaryInput() { 135 void pausePrimaryInputs() {
135 schedule(() { 136 schedule(() {
136 _primaryInput = new Completer(); 137 _primaryInputs = new Completer();
137 }, "pause primaryInput for $this"); 138 }, "pause primaryInputs for $this");
138 } 139 }
139 140
140 /// Resumes the transformer's invocation of [primaryInput] after 141 /// Resumes the transformer's invocation of [primaryInputs] after
141 /// [pauseGetPrimary] was called. 142 /// [pausePrimaryInputs] was called.
142 /// 143 ///
143 /// This operation is scheduled. 144 /// This operation is scheduled.
144 void resumePrimaryInput() { 145 void resumePrimaryInputs() {
145 schedule(() { 146 schedule(() {
146 _primaryInput.complete(); 147 _primaryInputs.complete();
147 _primaryInput = null; 148 _primaryInputs = null;
148 }, "resume getPrimary() for $this"); 149 }, "resume primaryInputs for $this");
149 } 150 }
150 151
151 /// Like [Transform.getInput], but respects [pauseGetInput]. 152 /// Like [AggregateTransform.getInput], but respects [pauseGetInput].
152 /// 153 ///
153 /// This is intended for use by subclasses of [MockTransformer]. 154 /// This is intended for use by subclasses of [MockAggregateTransformer].
154 Future<Asset> getInput(Transform transform, AssetId id) { 155 Future<Asset> getInput(AggregateTransform transform, AssetId id) {
155 return newFuture(() { 156 return newFuture(() {
156 if (_getInput.containsKey(id)) return _getInput[id].future; 157 if (_getInput.containsKey(id)) return _getInput[id].future;
157 }).then((_) => transform.getInput(id)); 158 }).then((_) => transform.getInput(id));
158 } 159 }
159 160
160 /// Like [Transform.primaryInput], but respects [pauseGetPrimary]. 161 /// Like [AggregateTransform.primaryInputs], but respects
162 /// [pausePrimaryInputs].
161 /// 163 ///
162 /// This is intended for use by subclasses of [MockTransformer]. 164 /// This is intended for use by subclasses of [MockAggregateTransformer].
163 Future<Asset> getPrimary(Transform transform) { 165 Stream<Asset> getPrimaryInputs(AggregateTransform transform) {
164 return newFuture(() { 166 return futureStream(newFuture(() {
165 if (_primaryInput != null) return _primaryInput.future; 167 if (_primaryInputs != null) return _primaryInputs.future;
166 }).then((_) => transform.primaryInput); 168 }).then((_) => transform.primaryInputs));
167 } 169 }
168 170
169 Future<bool> isPrimary(AssetId id) { 171 Future<String> classifyPrimary(AssetId id) {
170 return newFuture(() => doIsPrimary(id)).then((result) { 172 return newFuture(() => doClassifyPrimary(id)).then((result) {
171 return newFuture(() { 173 return newFuture(() {
Bob Nystrom 2014/05/27 17:20:18 Why is newFuture needed here?
nweiz 2014/05/27 21:03:10 We need to return "result" afterwards, and it woul
Bob Nystrom 2014/05/28 21:03:58 Oh, right. Thanks for clarifying.
172 if (_isPrimary.containsKey(id)) { 174 if (_classifyPrimary.containsKey(id)) {
173 return _isPrimary[id].future; 175 return _classifyPrimary[id].future;
174 } 176 }
175 }).then((_) => result); 177 }).then((_) => result);
176 }); 178 });
177 } 179 }
178 180
179 Future apply(Transform transform) { 181 Future apply(AggregateTransform transform) {
180 _numRuns++; 182 _numRuns++;
181 if (_runningTransforms == 0) _started.complete(); 183 if (_runningTransforms == 0) _started.complete();
182 _runningTransforms++; 184 _runningTransforms++;
183 if (consumePrimary) transform.consumePrimary();
184 return newFuture(() => doApply(transform)).then((_) { 185 return newFuture(() => doApply(transform)).then((_) {
185 if (_apply != null) return _apply.future; 186 if (_apply != null) return _apply.future;
186 }).whenComplete(() { 187 }).whenComplete(() {
188 for (var id in consumePrimaries) {
189 transform.consumePrimary(new AssetId.parse(id));
190 }
187 _runningTransforms--; 191 _runningTransforms--;
188 if (_runningTransforms == 0) _started = new Completer(); 192 if (_runningTransforms == 0) _started = new Completer();
189 }); 193 });
190 } 194 }
191 195
192 /// The wrapped version of [isPrimary] for subclasses to override. 196 /// The wrapped version of [classifyPrimary] for subclasses to override.
193 /// 197 ///
194 /// This may return a `Future<bool>` or, if it's entirely synchronous, a 198 /// This may return a `Future<String>` or, if it's entirely synchronous, a
195 /// `bool`. 199 /// `String`.
196 doIsPrimary(AssetId id); 200 doClassifyPrimary(AssetId id);
197 201
198 /// The wrapped version of [doApply] for subclasses to override. 202 /// The wrapped version of [doApply] for subclasses to override.
199 /// 203 ///
200 /// If this does asynchronous work, it should return a [Future] that completes 204 /// If this does asynchronous work, it should return a [Future] that completes
201 /// once it's finished. 205 /// once it's finished.
202 doApply(Transform transform); 206 doApply(AggregateTransform transform);
203 } 207 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698