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 == false) { | |
|
arv (Not doing code reviews)
2011/10/27 05:50:24
if (!_nextMeasurementFrameScheduled) {
Jacob
2011/10/27 20:59:25
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 | |
|
arv (Not doing code reviews)
2011/10/27 05:50:24
as as
Jacob
2011/10/27 20:59:25
as as ==> as all
done done
| |
| 28 // as 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. | |
| 31 window.on.message.add(Isolate.bind( | |
|
arv (Not doing code reviews)
2011/10/27 05:50:24
If anyone else is listening to on message they wil
Jacob
2011/10/27 20:59:25
Do you have any ideas? Creating an iframe just for
| |
| 32 (e) => _onCompleteMeasurementFutures() | |
| 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) { | |
| 46 if (null == _pendingMeasurementFrameCallbacks) { | |
| 47 _pendingMeasurementFrameCallbacks = <TimeoutHandler>[]; | |
| 48 _maybeScheduleMeasurementFrame(); | |
| 49 } | |
| 50 _pendingMeasurementFrameCallbacks.add(callback); | |
| 51 } | |
| 52 | |
| 53 Future _createMeasurementFuture(ComputeValue computeValue, Completer completer) { | |
|
arv (Not doing code reviews)
2011/10/27 05:50:24
long line
Jacob
2011/10/27 20:59:25
Done.
| |
| 54 if (null == _pendingRequests) { | |
| 55 _pendingRequests = <MeasurementRequest>[]; | |
| 56 _maybeScheduleMeasurementFrame(); | |
| 57 } | |
| 58 _pendingRequests.add(new MeasurementRequest(computeValue, completer)); | |
| 59 return completer.future; | |
| 60 } | |
| 61 | |
| 62 void _onCompleteMeasurementFutures() { | |
| 63 if (_nextMeasurementFrameScheduled == false) { | |
| 64 // Ignore spurious call to this function. | |
| 65 return; | |
| 66 } | |
| 67 | |
| 68 _nextMeasurementFrameScheduled = false; | |
| 69 // We must compute all new values before fulfilling the futures as | |
| 70 // the onComplete callbacks for the futures could modify the DOM making | |
| 71 // subsequent measurement calculations expensive to compute. | |
| 72 for (MeasurementRequest request in _pendingRequests) { | |
| 73 try { | |
| 74 request.value = request.computeValue(); | |
| 75 } catch(var e) { | |
| 76 request.value = e; | |
| 77 request.exception = true; | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 final completedRequests = _pendingRequests; | |
| 82 final readyMeasurementFrameCallbacks = _pendingMeasurementFrameCallbacks; | |
| 83 _pendingRequests = null; | |
| 84 _pendingMeasurementFrameCallbacks = null; | |
| 85 for (MeasurementRequest request in completedRequests) { | |
| 86 if (request.exception) { | |
| 87 request.completer.completeException(request.value); | |
| 88 } else { | |
| 89 request.completer.complete(request.value); | |
| 90 } | |
| 91 } | |
| 92 | |
| 93 if (null != readyMeasurementFrameCallbacks) { | |
| 94 for (TimeoutHandler handler in readyMeasurementFrameCallbacks) { | |
| 95 // TODO(jacobr): wrap each call to a handler in a try-catch block. | |
| 96 handler(); | |
| 97 } | |
| 98 } | |
| 99 } | |
| OLD | NEW |