Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011, 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 typedef Object ComputeValue(); | |
| 6 | |
| 7 class MeasurementRequest<T> { | |
| 8 final ComputeValue computeValue; | |
| 9 final Completer<T> completer; | |
| 10 Object value; | |
| 11 bool exception = false; | |
| 12 MeasurementRequest(this.computeValue, this.completer); | |
| 13 } | |
| 14 | |
| 15 final _MEASUREMENT_MESSAGE = "DART-MEASURE"; | |
| 16 List<MeasurementRequest> _pendingRequests; | |
| 17 List<TimeoutHandler> _pendingMeasurementFrameCallbacks; | |
| 18 bool _nextMeasurementFrameScheduled = false; | |
| 19 bool _firstMeasurementRequest = true; | |
| 20 | |
| 21 void _maybeScheduleMeasurementFrame() { | |
| 22 if (!_nextMeasurementFrameScheduled) { | |
|
nweiz
2011/10/28 03:58:38
Style nit: if (_nextMeasurementFrameScheduled) ret
Jacob
2011/10/31 22:09:50
Done.
| |
| 23 _nextMeasurementFrameScheduled = true; | |
| 24 // postMessage gives us a way to receive a callback after the current | |
| 25 // event listener has unwound but before the browser has repainted. | |
| 26 if (_firstMeasurementRequest) { | |
| 27 // Messages from other windows do not cause a security risk as | |
| 28 // all we care about is that _onCompleteMeasurementRequests is called | |
| 29 // after the current event loop is unwound and calling the function is | |
| 30 // a noop if no requests are pending. | |
|
nweiz
2011/10/28 03:58:38
How sure are we that it isn't possible to at least
Jacob
2011/10/31 22:09:50
100% sure due to the early terminate at the start
| |
| 31 window.on.message.add(Isolate.bind( | |
|
nweiz
2011/10/28 03:58:38
I believe Isolate.bind is no longer necessary (see
Jacob
2011/10/31 22:09:50
Done.
| |
| 32 (e) => _onCompleteMeasurementFutures() | |
|
nweiz
2011/10/28 03:58:38
Style nit: 4 spaces
Jacob
2011/10/31 22:09:50
Done.
| |
| 33 )); | |
| 34 _firstMeasurementRequest = false; | |
| 35 } | |
| 36 | |
| 37 // TODO(jacobr): other mechanisms such as setImmediate and | |
| 38 // requestAnimationFrame may work better of platforms that support them. | |
| 39 // The key is we need a way to execute code immediately after the current | |
| 40 // event listener queue unwinds. | |
| 41 window.postMessage(_MEASUREMENT_MESSAGE, "*"); | |
| 42 } | |
| 43 } | |
| 44 | |
| 45 void _addMeasurementFrameCallback(TimeoutHandler callback) { | |
|
nweiz
2011/10/28 03:58:38
It would be nice to have some documentation for th
Jacob
2011/10/31 22:09:50
Agreed. Added some flower box comments
| |
| 46 if (null == _pendingMeasurementFrameCallbacks) { | |
| 47 _pendingMeasurementFrameCallbacks = <TimeoutHandler>[]; | |
| 48 _maybeScheduleMeasurementFrame(); | |
| 49 } | |
| 50 _pendingMeasurementFrameCallbacks.add(callback); | |
| 51 } | |
| 52 | |
| 53 Future _createMeasurementFuture(ComputeValue computeValue, | |
| 54 Completer completer) { | |
| 55 if (null == _pendingRequests) { | |
| 56 _pendingRequests = <MeasurementRequest>[]; | |
| 57 _maybeScheduleMeasurementFrame(); | |
| 58 } | |
| 59 _pendingRequests.add(new MeasurementRequest(computeValue, completer)); | |
| 60 return completer.future; | |
| 61 } | |
| 62 | |
| 63 void _onCompleteMeasurementFutures() { | |
|
nweiz
2011/10/28 03:58:38
I feel like this name suggests that the function i
Jacob
2011/10/31 22:09:50
Done.
| |
| 64 if (_nextMeasurementFrameScheduled == false) { | |
| 65 // Ignore spurious call to this function. | |
| 66 return; | |
| 67 } | |
| 68 | |
| 69 _nextMeasurementFrameScheduled = false; | |
| 70 // We must compute all new values before fulfilling the futures as | |
| 71 // the onComplete callbacks for the futures could modify the DOM making | |
| 72 // subsequent measurement calculations expensive to compute. | |
| 73 for (MeasurementRequest request in _pendingRequests) { | |
| 74 try { | |
| 75 request.value = request.computeValue(); | |
| 76 } catch(var e) { | |
| 77 request.value = e; | |
| 78 request.exception = true; | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 final completedRequests = _pendingRequests; | |
| 83 final readyMeasurementFrameCallbacks = _pendingMeasurementFrameCallbacks; | |
| 84 _pendingRequests = null; | |
| 85 _pendingMeasurementFrameCallbacks = null; | |
| 86 for (MeasurementRequest request in completedRequests) { | |
| 87 if (request.exception) { | |
| 88 request.completer.completeException(request.value); | |
| 89 } else { | |
| 90 request.completer.complete(request.value); | |
| 91 } | |
| 92 } | |
| 93 | |
| 94 if (null != readyMeasurementFrameCallbacks) { | |
| 95 for (TimeoutHandler handler in readyMeasurementFrameCallbacks) { | |
| 96 // TODO(jacobr): wrap each call to a handler in a try-catch block. | |
| 97 handler(); | |
| 98 } | |
| 99 } | |
| 100 } | |
| OLD | NEW |