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

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: code review Created 6 years, 6 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
« no previous file with comments | « pkg/barback/test/transformer/mock.dart ('k') | pkg/barback/test/utils.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
35 /// [apply].
34 Completer _apply; 36 Completer _apply;
35 37
36 /// Completers for pausing the transformer before it finishes running 38 /// Completers for pausing the transformer before it finishes running
37 /// [isPrimary]. 39 /// [classifyPrimary].
38 final _isPrimary = new Map<AssetId, Completer>(); 40 final _classifyPrimary = new Map<AssetId, Completer>();
39 41
40 /// Completers for pausing the transformer before it finishes getting inputs 42 /// Completers for pausing the transformer before it finishes getting inputs
41 /// the [Transform]. 43 /// the [Transform].
42 final _getInput = new Map<AssetId, Completer>(); 44 final _getInput = new Map<AssetId, Completer>();
43 45
44 /// Completer for pausing the transformer before it accesses [primaryInput]. 46 /// Completer for pausing the transformer before it accesses
45 Completer _primaryInput; 47 /// [getPrimaryInputs].
48 Completer _primaryInputs;
46 49
47 /// A completer that completes once this transformer begins running. 50 /// A completer that completes once this transformer begins running.
48 /// 51 ///
49 /// Once this transformer finishes running, this is reset to a new completer, 52 /// Once this transformer finishes running, this is reset to a new completer,
50 /// so it can be used multiple times. 53 /// so it can be used multiple times.
51 var _started = new Completer(); 54 var _started = new Completer();
52 55
53 /// `true` if any transforms are currently running. 56 /// `true` if any transforms are currently running.
54 /// 57 ///
55 /// This is scheduled. The Future will complete at the point in the schedule 58 /// This is scheduled. The Future will complete at the point in the schedule
56 /// that this is called. 59 /// that this is called.
57 Future<bool> get isRunning => schedule(() => _runningTransforms > 0); 60 Future<bool> get isRunning => schedule(() => _runningTransforms > 0);
58 61
59 /// If this is set to `true`, the transformer will consume its primary input 62 /// All elements of this set will be automatically consumed during [apply].
60 /// during [apply]. 63 final consumePrimaries = new Set<String>();
61 bool consumePrimary = false;
62 64
63 /// Pauses the schedule until this transformer begins running. 65 /// Pauses the schedule until this transformer begins running.
64 void waitUntilStarted() { 66 void waitUntilStarted() {
65 schedule(() => _started.future, "wait until $this starts"); 67 schedule(() => _started.future, "wait until $this starts");
66 } 68 }
67 69
68 /// Causes the transformer to pause after running [apply] but before the 70 /// Causes the transformer to pause after running [apply] but before the
69 /// returned Future completes. 71 /// returned Future completes.
70 /// 72 ///
71 /// This can be resumed by calling [resumeApply]. This operation is scheduled. 73 /// This can be resumed by calling [resumeApply]. This operation is scheduled.
72 void pauseApply() { 74 void pauseApply() {
73 schedule(() { 75 schedule(() {
74 _apply = new Completer(); 76 _apply = new Completer();
75 }, "pause apply for $this"); 77 }, "pause apply for $this");
76 } 78 }
77 79
78 /// Resumes the transformer's [apply] call after [pauseApply] was called. 80 /// Resumes the transformer's [apply] call after [pauseApply] was called.
79 /// 81 ///
80 /// This operation is scheduled. 82 /// This operation is scheduled.
81 void resumeApply() { 83 void resumeApply() {
82 schedule(() { 84 schedule(() {
83 _apply.complete(); 85 _apply.complete();
84 _apply = null; 86 _apply = null;
85 }, "resume apply for $this"); 87 }, "resume apply for $this");
86 } 88 }
87 89
88 /// Causes the transformer to pause after running [isPrimary] on the asset 90 /// Causes the transformer to pause after running [classifyPrimary] on the
89 /// with the given [name], but before the returned Future completes. 91 /// asset with the given [name], but before the returned Future completes.
90 /// 92 ///
91 /// This can be resumed by calling [resumeIsPrimary]. This operation is 93 /// This can be resumed by calling [resumeClassifyPrimary]. This operation is
92 /// scheduled. 94 /// scheduled.
93 void pauseIsPrimary(String name) { 95 void pauseClassifyPrimary(String name) {
94 schedule(() { 96 schedule(() {
95 _isPrimary[new AssetId.parse(name)] = new Completer(); 97 _classifyPrimary[new AssetId.parse(name)] = new Completer();
96 }, "pause isPrimary($name) for $this"); 98 }, "pause classifyPrimary($name) for $this");
97 } 99 }
98 100
99 /// Resumes the transformer's [isPrimary] call on the asset with the given 101 /// Resumes the transformer's [classifyPrimary] call on the asset with the
100 /// [name] after [pauseIsPrimary] was called. 102 /// given [name] after [pauseClassifyPrimary] was called.
101 /// 103 ///
102 /// This operation is scheduled. 104 /// This operation is scheduled.
103 void resumeIsPrimary(String name) { 105 void resumeClassifyPrimary(String name) {
104 schedule(() { 106 schedule(() {
105 _isPrimary.remove(new AssetId.parse(name)).complete(); 107 _classifyPrimary.remove(new AssetId.parse(name)).complete();
106 }, "resume isPrimary($name) for $this"); 108 }, "resume classifyPrimary($name) for $this");
107 } 109 }
108 110
109 /// Causes the transformer to pause while loading the secondary input with 111 /// Causes the transformer to pause while loading the secondary input with
110 /// the given [name]. 112 /// the given [name].
111 /// 113 ///
112 /// This can be resumed by calling [resumeGetInput]. This operation is 114 /// This can be resumed by calling [resumeGetInput]. This operation is
113 /// scheduled. 115 /// scheduled.
114 void pauseGetInput(String name) { 116 void pauseGetInput(String name) {
115 schedule(() { 117 schedule(() {
116 _getInput[new AssetId.parse(name)] = new Completer(); 118 _getInput[new AssetId.parse(name)] = new Completer();
117 }, "pause getInput($name) for $this"); 119 }, "pause getInput($name) for $this");
118 } 120 }
119 121
120 /// Resumes the transformer's loading of the input with the given [name] after 122 /// Resumes the transformer's loading of the input with the given [name] after
121 /// [pauseGetInput] was called. 123 /// [pauseGetInput] was called.
122 /// 124 ///
123 /// This operation is scheduled. 125 /// This operation is scheduled.
124 void resumeGetInput(String name) { 126 void resumeGetInput(String name) {
125 schedule(() { 127 schedule(() {
126 _getInput.remove(new AssetId.parse(name)).complete(); 128 _getInput.remove(new AssetId.parse(name)).complete();
127 }, "resume getInput($name) for $this"); 129 }, "resume getInput($name) for $this");
128 } 130 }
129 131
130 /// Causes the transformer to pause before accessing [primaryInput]. 132 /// Causes the transformer to pause before accessing [getPrimaryInputs].
131 /// 133 ///
132 /// This can be resumed by calling [resumeGetPrimary]. This operation is 134 /// This can be resumed by calling [resumePrimaryInputs]. This operation is
133 /// scheduled. 135 /// scheduled.
134 void pausePrimaryInput() { 136 void pausePrimaryInputs() {
135 schedule(() { 137 schedule(() {
136 _primaryInput = new Completer(); 138 _primaryInputs = new Completer();
137 }, "pause primaryInput for $this"); 139 }, "pause primaryInputs for $this");
138 } 140 }
139 141
140 /// Resumes the transformer's invocation of [primaryInput] after 142 /// Resumes the transformer's invocation of [primaryInputs] after
141 /// [pauseGetPrimary] was called. 143 /// [pausePrimaryInputs] was called.
142 /// 144 ///
143 /// This operation is scheduled. 145 /// This operation is scheduled.
144 void resumePrimaryInput() { 146 void resumePrimaryInputs() {
145 schedule(() { 147 schedule(() {
146 _primaryInput.complete(); 148 _primaryInputs.complete();
147 _primaryInput = null; 149 _primaryInputs = null;
148 }, "resume getPrimary() for $this"); 150 }, "resume primaryInputs for $this");
149 } 151 }
150 152
151 /// Like [Transform.getInput], but respects [pauseGetInput]. 153 /// Like [AggregateTransform.getInput], but respects [pauseGetInput].
152 /// 154 ///
153 /// This is intended for use by subclasses of [MockTransformer]. 155 /// This is intended for use by subclasses of [MockAggregateTransformer].
154 Future<Asset> getInput(Transform transform, AssetId id) { 156 Future<Asset> getInput(AggregateTransform transform, AssetId id) {
155 return newFuture(() { 157 return newFuture(() {
156 if (_getInput.containsKey(id)) return _getInput[id].future; 158 if (_getInput.containsKey(id)) return _getInput[id].future;
157 }).then((_) => transform.getInput(id)); 159 }).then((_) => transform.getInput(id));
158 } 160 }
159 161
160 /// Like [Transform.primaryInput], but respects [pauseGetPrimary]. 162 /// Like [AggregateTransform.primaryInputs], but respects
163 /// [pausePrimaryInputs].
161 /// 164 ///
162 /// This is intended for use by subclasses of [MockTransformer]. 165 /// This is intended for use by subclasses of [MockAggregateTransformer].
163 Future<Asset> getPrimary(Transform transform) { 166 Stream<Asset> getPrimaryInputs(AggregateTransform transform) {
164 return newFuture(() { 167 return futureStream(newFuture(() {
165 if (_primaryInput != null) return _primaryInput.future; 168 if (_primaryInputs != null) return _primaryInputs.future;
166 }).then((_) => transform.primaryInput); 169 }).then((_) => transform.primaryInputs));
167 } 170 }
168 171
169 Future<bool> isPrimary(AssetId id) { 172 Future<String> classifyPrimary(AssetId id) {
170 return newFuture(() => doIsPrimary(id)).then((result) { 173 return newFuture(() => doClassifyPrimary(id)).then((result) {
171 return newFuture(() { 174 return newFuture(() {
172 if (_isPrimary.containsKey(id)) { 175 if (_classifyPrimary.containsKey(id)) {
173 return _isPrimary[id].future; 176 return _classifyPrimary[id].future;
174 } 177 }
175 }).then((_) => result); 178 }).then((_) => result);
176 }); 179 });
177 } 180 }
178 181
179 Future apply(Transform transform) { 182 Future apply(AggregateTransform transform) {
180 _numRuns++; 183 _numRuns++;
181 if (_runningTransforms == 0) _started.complete(); 184 if (_runningTransforms == 0) _started.complete();
182 _runningTransforms++; 185 _runningTransforms++;
183 if (consumePrimary) transform.consumePrimary();
184 return newFuture(() => doApply(transform)).then((_) { 186 return newFuture(() => doApply(transform)).then((_) {
185 if (_apply != null) return _apply.future; 187 if (_apply != null) return _apply.future;
186 }).whenComplete(() { 188 }).whenComplete(() {
189 for (var id in consumePrimaries) {
190 transform.consumePrimary(new AssetId.parse(id));
191 }
187 _runningTransforms--; 192 _runningTransforms--;
188 if (_runningTransforms == 0) _started = new Completer(); 193 if (_runningTransforms == 0) _started = new Completer();
189 }); 194 });
190 } 195 }
191 196
192 /// The wrapped version of [isPrimary] for subclasses to override. 197 /// The wrapped version of [classifyPrimary] for subclasses to override.
193 /// 198 ///
194 /// This may return a `Future<bool>` or, if it's entirely synchronous, a 199 /// This may return a `Future<String>` or, if it's entirely synchronous, a
195 /// `bool`. 200 /// `String`.
196 doIsPrimary(AssetId id); 201 doClassifyPrimary(AssetId id);
197 202
198 /// The wrapped version of [doApply] for subclasses to override. 203 /// The wrapped version of [doApply] for subclasses to override.
199 /// 204 ///
200 /// If this does asynchronous work, it should return a [Future] that completes 205 /// If this does asynchronous work, it should return a [Future] that completes
201 /// once it's finished. 206 /// once it's finished.
202 doApply(Transform transform); 207 doApply(AggregateTransform transform);
203 } 208 }
OLDNEW
« no previous file with comments | « pkg/barback/test/transformer/mock.dart ('k') | pkg/barback/test/utils.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698