OLD | NEW |
1 // Copyright (c) 2011, 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 bool _inMeasurementFrame = false; |
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 | 6 |
15 final _MEASUREMENT_MESSAGE = "DART-MEASURE"; | 7 final _MEASUREMENT_MESSAGE = "DART-MEASURE"; |
16 List<_MeasurementRequest> _pendingRequests; | 8 |
17 List<TimeoutHandler> _pendingMeasurementFrameCallbacks; | 9 Queue<MeasurementCallback> _pendingMeasurementFrameCallbacks; |
18 bool _nextMeasurementFrameScheduled = false; | 10 bool _nextMeasurementFrameScheduled = false; |
19 bool _firstMeasurementRequest = true; | 11 bool _firstMeasurementRequest = true; |
20 | 12 |
21 void _maybeScheduleMeasurementFrame() { | 13 void _maybeScheduleMeasurementFrame() { |
22 if (_nextMeasurementFrameScheduled) return; | 14 if (_nextMeasurementFrameScheduled) return; |
23 | 15 |
24 _nextMeasurementFrameScheduled = true; | 16 _nextMeasurementFrameScheduled = true; |
25 // postMessage gives us a way to receive a callback after the current | 17 // postMessage gives us a way to receive a callback after the current |
26 // event listener has unwound but before the browser has repainted. | 18 // event listener has unwound but before the browser has repainted. |
27 if (_firstMeasurementRequest) { | 19 if (_firstMeasurementRequest) { |
28 // Messages from other windows do not cause a security risk as | 20 // Messages from other windows do not cause a security risk as |
29 // all we care about is that _onCompleteMeasurementRequests is called | 21 // all we care about is that _onCompleteMeasurementRequests is called |
30 // after the current event loop is unwound and calling the function is | 22 // after the current event loop is unwound and calling |
31 // a noop when zero requests are pending. | 23 // _runMeasurementFrames is a noop when zero requests are pending. |
32 window.on.message.add((e) => _completeMeasurementFutures()); | 24 window.on.message.add((e) => _runMeasurementFrames()); |
33 _firstMeasurementRequest = false; | 25 _firstMeasurementRequest = false; |
34 } | 26 } |
35 | 27 |
36 // TODO(jacobr): other mechanisms such as setImmediate and | 28 // TODO(jacobr): other mechanisms such as setImmediate and |
37 // requestAnimationFrame may work better of platforms that support them. | 29 // requestAnimationFrame may work better of platforms that support them. |
38 // The key is we need a way to execute code immediately after the current | 30 // The key is we need a way to execute code immediately after the current |
39 // event listener queue unwinds. | 31 // event listener queue unwinds. |
40 window.postMessage(_MEASUREMENT_MESSAGE, "*"); | 32 window.postMessage(_MEASUREMENT_MESSAGE, "*"); |
41 } | 33 } |
42 | 34 |
43 /** | 35 /** |
44 * Registers a [callback] which is called after the next batch of measurements | 36 * Registers a [callback] which is called after the next batch of measurements |
45 * completes. Even if no measurements completed, the callback is triggered | 37 * completes. Even if no measurements completed, the callback is triggered |
46 * when they would have completed to avoid confusing bugs if it happened that | 38 * when they would have completed to avoid confusing bugs if it happened that |
47 * no measurements were actually requested. | 39 * no measurements were actually requested. |
48 */ | 40 */ |
49 void _addMeasurementFrameCallback(TimeoutHandler callback) { | 41 void _addMeasurementFrameCallback(MeasurementCallback callback) { |
| 42 assert(callback != null); |
50 if (_pendingMeasurementFrameCallbacks === null) { | 43 if (_pendingMeasurementFrameCallbacks === null) { |
51 _pendingMeasurementFrameCallbacks = <TimeoutHandler>[]; | 44 _pendingMeasurementFrameCallbacks = new Queue<MeasurementCallback>(); |
52 _maybeScheduleMeasurementFrame(); | |
53 } | 45 } |
| 46 _maybeScheduleMeasurementFrame(); |
54 _pendingMeasurementFrameCallbacks.add(callback); | 47 _pendingMeasurementFrameCallbacks.add(callback); |
55 } | 48 } |
56 | 49 |
57 /** | 50 /** |
58 * Returns a [Future] whose value will be the result of evaluating | 51 * Run all pending measurement frames evaluating them in a single batch |
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. | 52 * so that the the browser is guaranteed to avoid multiple layouts. |
78 */ | 53 */ |
79 void _completeMeasurementFutures() { | 54 void _runMeasurementFrames() { |
80 if (_nextMeasurementFrameScheduled == false) { | 55 if (_nextMeasurementFrameScheduled == false || _inMeasurementFrame) { |
81 // Ignore spurious call to this function. | 56 // Ignore spurious call to this function. |
82 return; | 57 return; |
83 } | 58 } |
84 | 59 |
85 _nextMeasurementFrameScheduled = false; | 60 _inMeasurementFrame = true; |
86 // We must compute all new values before fulfilling the futures as | 61 |
87 // the onComplete callbacks for the futures could modify the DOM making | 62 final layoutCallbacks = <LayoutCallback>[]; |
88 // subsequent measurement calculations expensive to compute. | 63 while (!_pendingMeasurementFrameCallbacks.isEmpty()) { |
89 if (_pendingRequests !== null) { | 64 MeasurementCallback measurementCallback = |
90 for (_MeasurementRequest request in _pendingRequests) { | 65 _pendingMeasurementFrameCallbacks.removeFirst(); |
91 try { | 66 try { |
92 request.value = request.computeValue(); | 67 final layoutCallback = measurementCallback(); |
93 } catch(var e) { | 68 if (layoutCallback != null) { |
94 request.value = e; | 69 layoutCallbacks.add(layoutCallback); |
95 request.exception = true; | |
96 } | 70 } |
| 71 } catch (Object e) { |
| 72 window.console.error( |
| 73 'Caught exception in measurement frame callback: ${e}'); |
| 74 // TODO(jacobr): throw this exception again in the correct way. |
97 } | 75 } |
98 } | 76 } |
99 | 77 |
100 final completedRequests = _pendingRequests; | 78 _inMeasurementFrame = false; |
101 final readyMeasurementFrameCallbacks = _pendingMeasurementFrameCallbacks; | 79 _nextMeasurementFrameScheduled = false; |
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 | 80 |
114 if (readyMeasurementFrameCallbacks !== null) { | 81 for (LayoutCallback layoutCallback in layoutCallbacks) { |
115 for (TimeoutHandler handler in readyMeasurementFrameCallbacks) { | 82 try { |
116 // TODO(jacobr): wrap each call to a handler in a try-catch block. | 83 layoutCallback(); |
117 handler(); | 84 } catch (Object e) { |
| 85 window.console.error('Caught exception in layout callback: ${e}'); |
| 86 // TODO(jacobr): throw this exception again in the correct way. |
118 } | 87 } |
119 } | 88 } |
120 } | 89 } |
OLD | NEW |