| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 typedef Object ComputeValue(); | 5 typedef Object ComputeValue(); |
| 6 | 6 |
| 7 class _MeasurementRequest<T> { | 7 class _MeasurementRequest<T> { |
| 8 final ComputeValue computeValue; | 8 final ComputeValue computeValue; |
| 9 final Completer<T> completer; | 9 final Completer<T> completer; |
| 10 Object value; | 10 Object value; |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 _measurementScheduler.maybeSchedule(); | 125 _measurementScheduler.maybeSchedule(); |
| 126 } | 126 } |
| 127 | 127 |
| 128 /** | 128 /** |
| 129 * Registers a [callback] which is called after the next batch of measurements | 129 * Registers a [callback] which is called after the next batch of measurements |
| 130 * completes. Even if no measurements completed, the callback is triggered | 130 * completes. Even if no measurements completed, the callback is triggered |
| 131 * when they would have completed to avoid confusing bugs if it happened that | 131 * when they would have completed to avoid confusing bugs if it happened that |
| 132 * no measurements were actually requested. | 132 * no measurements were actually requested. |
| 133 */ | 133 */ |
| 134 void _addMeasurementFrameCallback(TimeoutHandler callback) { | 134 void _addMeasurementFrameCallback(TimeoutHandler callback) { |
| 135 if (_pendingMeasurementFrameCallbacks === null) { | 135 if (_pendingMeasurementFrameCallbacks == null) { |
| 136 _pendingMeasurementFrameCallbacks = <TimeoutHandler>[]; | 136 _pendingMeasurementFrameCallbacks = <TimeoutHandler>[]; |
| 137 _maybeScheduleMeasurementFrame(); | 137 _maybeScheduleMeasurementFrame(); |
| 138 } | 138 } |
| 139 _pendingMeasurementFrameCallbacks.add(callback); | 139 _pendingMeasurementFrameCallbacks.add(callback); |
| 140 } | 140 } |
| 141 | 141 |
| 142 /** | 142 /** |
| 143 * Returns a [Future] whose value will be the result of evaluating | 143 * Returns a [Future] whose value will be the result of evaluating |
| 144 * [computeValue] during the next safe measurement interval. | 144 * [computeValue] during the next safe measurement interval. |
| 145 * The next safe measurement interval is after the current event loop has | 145 * The next safe measurement interval is after the current event loop has |
| 146 * unwound but before the browser has rendered the page. | 146 * unwound but before the browser has rendered the page. |
| 147 * It is important that the [computeValue] function only queries the html | 147 * It is important that the [computeValue] function only queries the html |
| 148 * layout and html in any way. | 148 * layout and html in any way. |
| 149 */ | 149 */ |
| 150 Future _createMeasurementFuture(ComputeValue computeValue, | 150 Future _createMeasurementFuture(ComputeValue computeValue, |
| 151 Completer completer) { | 151 Completer completer) { |
| 152 if (_pendingRequests === null) { | 152 if (_pendingRequests == null) { |
| 153 _pendingRequests = <_MeasurementRequest>[]; | 153 _pendingRequests = <_MeasurementRequest>[]; |
| 154 _maybeScheduleMeasurementFrame(); | 154 _maybeScheduleMeasurementFrame(); |
| 155 } | 155 } |
| 156 _pendingRequests.add(new _MeasurementRequest(computeValue, completer)); | 156 _pendingRequests.add(new _MeasurementRequest(computeValue, completer)); |
| 157 return completer.future; | 157 return completer.future; |
| 158 } | 158 } |
| 159 | 159 |
| 160 /** | 160 /** |
| 161 * Complete all pending measurement futures evaluating them in a single batch | 161 * Complete all pending measurement futures evaluating them in a single batch |
| 162 * so that the the browser is guaranteed to avoid multiple layouts. | 162 * so that the the browser is guaranteed to avoid multiple layouts. |
| 163 */ | 163 */ |
| 164 void _completeMeasurementFutures() { | 164 void _completeMeasurementFutures() { |
| 165 // We must compute all new values before fulfilling the futures as | 165 // We must compute all new values before fulfilling the futures as |
| 166 // the onComplete callbacks for the futures could modify the DOM making | 166 // the onComplete callbacks for the futures could modify the DOM making |
| 167 // subsequent measurement calculations expensive to compute. | 167 // subsequent measurement calculations expensive to compute. |
| 168 if (_pendingRequests !== null) { | 168 if (_pendingRequests != null) { |
| 169 for (_MeasurementRequest request in _pendingRequests) { | 169 for (_MeasurementRequest request in _pendingRequests) { |
| 170 try { | 170 try { |
| 171 request.value = request.computeValue(); | 171 request.value = request.computeValue(); |
| 172 } catch (e) { | 172 } catch (e) { |
| 173 request.value = e; | 173 request.value = e; |
| 174 request.exception = true; | 174 request.exception = true; |
| 175 } | 175 } |
| 176 } | 176 } |
| 177 } | 177 } |
| 178 | 178 |
| 179 final completedRequests = _pendingRequests; | 179 final completedRequests = _pendingRequests; |
| 180 final readyMeasurementFrameCallbacks = _pendingMeasurementFrameCallbacks; | 180 final readyMeasurementFrameCallbacks = _pendingMeasurementFrameCallbacks; |
| 181 _pendingRequests = null; | 181 _pendingRequests = null; |
| 182 _pendingMeasurementFrameCallbacks = null; | 182 _pendingMeasurementFrameCallbacks = null; |
| 183 if (completedRequests !== null) { | 183 if (completedRequests != null) { |
| 184 for (_MeasurementRequest request in completedRequests) { | 184 for (_MeasurementRequest request in completedRequests) { |
| 185 if (request.exception) { | 185 if (request.exception) { |
| 186 request.completer.completeException(request.value); | 186 request.completer.completeException(request.value); |
| 187 } else { | 187 } else { |
| 188 request.completer.complete(request.value); | 188 request.completer.complete(request.value); |
| 189 } | 189 } |
| 190 } | 190 } |
| 191 } | 191 } |
| 192 | 192 |
| 193 if (readyMeasurementFrameCallbacks !== null) { | 193 if (readyMeasurementFrameCallbacks != null) { |
| 194 for (TimeoutHandler handler in readyMeasurementFrameCallbacks) { | 194 for (TimeoutHandler handler in readyMeasurementFrameCallbacks) { |
| 195 // TODO(jacobr): wrap each call to a handler in a try-catch block. | 195 // TODO(jacobr): wrap each call to a handler in a try-catch block. |
| 196 handler(); | 196 handler(); |
| 197 } | 197 } |
| 198 } | 198 } |
| 199 } | 199 } |
| OLD | NEW |