| 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(); | |
| 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) return; | |
| 23 | |
| 24 _nextMeasurementFrameScheduled = true; | |
| 25 // postMessage gives us a way to receive a callback after the current | |
| 26 // event listener has unwound but before the browser has repainted. | |
| 27 if (_firstMeasurementRequest) { | |
| 28 // Messages from other windows do not cause a security risk as | |
| 29 // all we care about is that _onCompleteMeasurementRequests is called | |
| 30 // after the current event loop is unwound and calling the function is | |
| 31 // a noop when zero requests are pending. | |
| 32 window.on.message.add((e) => _completeMeasurementFutures()); | |
| 33 _firstMeasurementRequest = false; | |
| 34 } | |
| 35 | |
| 36 // TODO(jacobr): other mechanisms such as setImmediate and | |
| 37 // requestAnimationFrame may work better of platforms that support them. | |
| 38 // The key is we need a way to execute code immediately after the current | |
| 39 // event listener queue unwinds. | |
| 40 window.postMessage(_MEASUREMENT_MESSAGE, "*"); | |
| 41 } | |
| 42 | |
| 43 /** | |
| 44 * Registers a [callback] which is called after the next batch of measurements | |
| 45 * completes. Even if no measurements completed, the callback is triggered | |
| 46 * when they would have completed to avoid confusing bugs if it happened that | |
| 47 * no measurements were actually requested. | |
| 48 */ | |
| 49 void _addMeasurementFrameCallback(TimeoutHandler callback) { | |
| 50 if (_pendingMeasurementFrameCallbacks === null) { | |
| 51 _pendingMeasurementFrameCallbacks = <TimeoutHandler>[]; | |
| 52 _maybeScheduleMeasurementFrame(); | |
| 53 } | |
| 54 _pendingMeasurementFrameCallbacks.add(callback); | |
| 55 } | |
| 56 | |
| 57 /** | |
| 58 * Returns a [Future] whose value will be the result of evaluating | |
| 59 * [computeValue] during the next safe measurement interval. | |
| 60 * The next safe measurement interval is after the current event loop has | |
| 61 * unwound but before the browser has rendered the page. | |
| 62 * It is important that the [computeValue] function only queries the html | |
| 63 * layout and html in any way. | |
| 64 */ | |
| 65 Future _createMeasurementFuture(ComputeValue computeValue, | |
| 66 Completer completer) { | |
| 67 if (_pendingRequests === null) { | |
| 68 _pendingRequests = <_MeasurementRequest>[]; | |
| 69 _maybeScheduleMeasurementFrame(); | |
| 70 } | |
| 71 _pendingRequests.add(new _MeasurementRequest(computeValue, completer)); | |
| 72 return completer.future; | |
| 73 } | |
| 74 | |
| 75 /** | |
| 76 * Complete all pending measurement futures evaluating them in a single batch | |
| 77 * so that the the browser is guaranteed to avoid multiple layouts. | |
| 78 */ | |
| 79 void _completeMeasurementFutures() { | |
| 80 if (_nextMeasurementFrameScheduled == false) { | |
| 81 // Ignore spurious call to this function. | |
| 82 return; | |
| 83 } | |
| 84 | |
| 85 _nextMeasurementFrameScheduled = false; | |
| 86 // We must compute all new values before fulfilling the futures as | |
| 87 // the onComplete callbacks for the futures could modify the DOM making | |
| 88 // subsequent measurement calculations expensive to compute. | |
| 89 if (_pendingRequests !== null) { | |
| 90 for (_MeasurementRequest request in _pendingRequests) { | |
| 91 try { | |
| 92 request.value = request.computeValue(); | |
| 93 } catch(var e) { | |
| 94 request.value = e; | |
| 95 request.exception = true; | |
| 96 } | |
| 97 } | |
| 98 } | |
| 99 | |
| 100 final completedRequests = _pendingRequests; | |
| 101 final readyMeasurementFrameCallbacks = _pendingMeasurementFrameCallbacks; | |
| 102 _pendingRequests = null; | |
| 103 _pendingMeasurementFrameCallbacks = null; | |
| 104 if (completedRequests !== null) { | |
| 105 for (_MeasurementRequest request in completedRequests) { | |
| 106 if (request.exception) { | |
| 107 request.completer.completeException(request.value); | |
| 108 } else { | |
| 109 request.completer.complete(request.value); | |
| 110 } | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 if (readyMeasurementFrameCallbacks !== null) { | |
| 115 for (TimeoutHandler handler in readyMeasurementFrameCallbacks) { | |
| 116 // TODO(jacobr): wrap each call to a handler in a try-catch block. | |
| 117 handler(); | |
| 118 } | |
| 119 } | |
| 120 } | |
| 121 | |
| 122 class $CLASSNAME$EXTENDS$IMPLEMENTS native "@*DOMWindow" { | 5 class $CLASSNAME$EXTENDS$IMPLEMENTS native "@*DOMWindow" { |
| 123 | 6 |
| 124 _DocumentJs get document() native "return this.document.documentElement;"; | 7 _DocumentImpl get document() native "return this.document.documentElement;"; |
| 125 | 8 |
| 126 void requestLayoutFrame(TimeoutHandler callback) { | 9 void requestLayoutFrame(TimeoutHandler callback) { |
| 127 _addMeasurementFrameCallback(callback); | 10 _addMeasurementFrameCallback(callback); |
| 128 } | 11 } |
| 129 | 12 |
| 130 $!MEMBERS | 13 $!MEMBERS |
| 131 } | 14 } |
| OLD | NEW |