Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(73)

Side by Side Diff: client/html/src/Measurement.dart

Issue 9145004: Revert "Example showing alternate async measurement solution" (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « client/html/src/EventTargetWrappingImplementation.dart ('k') | client/html/src/Node.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 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 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 bool _inMeasurementFrame = false; 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 }
6 14
7 final _MEASUREMENT_MESSAGE = "DART-MEASURE"; 15 final _MEASUREMENT_MESSAGE = "DART-MEASURE";
8 16 List<_MeasurementRequest> _pendingRequests;
9 Queue<MeasurementCallback> _pendingMeasurementFrameCallbacks; 17 List<TimeoutHandler> _pendingMeasurementFrameCallbacks;
10 bool _nextMeasurementFrameScheduled = false; 18 bool _nextMeasurementFrameScheduled = false;
11 bool _firstMeasurementRequest = true; 19 bool _firstMeasurementRequest = true;
12 20
13 void _maybeScheduleMeasurementFrame() { 21 void _maybeScheduleMeasurementFrame() {
14 if (_nextMeasurementFrameScheduled) return; 22 if (_nextMeasurementFrameScheduled) return;
15 23
16 _nextMeasurementFrameScheduled = true; 24 _nextMeasurementFrameScheduled = true;
17 // postMessage gives us a way to receive a callback after the current 25 // postMessage gives us a way to receive a callback after the current
18 // event listener has unwound but before the browser has repainted. 26 // event listener has unwound but before the browser has repainted.
19 if (_firstMeasurementRequest) { 27 if (_firstMeasurementRequest) {
20 // Messages from other windows do not cause a security risk as 28 // Messages from other windows do not cause a security risk as
21 // all we care about is that _onCompleteMeasurementRequests is called 29 // all we care about is that _onCompleteMeasurementRequests is called
22 // after the current event loop is unwound and calling 30 // after the current event loop is unwound and calling the function is
23 // _runMeasurementFrames is a noop when zero requests are pending. 31 // a noop when zero requests are pending.
24 window.on.message.add((e) => _runMeasurementFrames()); 32 window.on.message.add((e) => _completeMeasurementFutures());
25 _firstMeasurementRequest = false; 33 _firstMeasurementRequest = false;
26 } 34 }
27 35
28 // TODO(jacobr): other mechanisms such as setImmediate and 36 // TODO(jacobr): other mechanisms such as setImmediate and
29 // requestAnimationFrame may work better of platforms that support them. 37 // requestAnimationFrame may work better of platforms that support them.
30 // The key is we need a way to execute code immediately after the current 38 // The key is we need a way to execute code immediately after the current
31 // event listener queue unwinds. 39 // event listener queue unwinds.
32 window.postMessage(_MEASUREMENT_MESSAGE, "*"); 40 window.postMessage(_MEASUREMENT_MESSAGE, "*");
33 } 41 }
34 42
35 /** 43 /**
36 * Registers a [callback] which is called after the next batch of measurements 44 * Registers a [callback] which is called after the next batch of measurements
37 * completes. Even if no measurements completed, the callback is triggered 45 * completes. Even if no measurements completed, the callback is triggered
38 * when they would have completed to avoid confusing bugs if it happened that 46 * when they would have completed to avoid confusing bugs if it happened that
39 * no measurements were actually requested. 47 * no measurements were actually requested.
40 */ 48 */
41 void _addMeasurementFrameCallback(MeasurementCallback callback) { 49 void _addMeasurementFrameCallback(TimeoutHandler callback) {
42 assert(callback != null);
43 if (_pendingMeasurementFrameCallbacks === null) { 50 if (_pendingMeasurementFrameCallbacks === null) {
44 _pendingMeasurementFrameCallbacks = new Queue<MeasurementCallback>(); 51 _pendingMeasurementFrameCallbacks = <TimeoutHandler>[];
52 _maybeScheduleMeasurementFrame();
45 } 53 }
46 _maybeScheduleMeasurementFrame();
47 _pendingMeasurementFrameCallbacks.add(callback); 54 _pendingMeasurementFrameCallbacks.add(callback);
48 } 55 }
49 56
50 /** 57 /**
51 * Run all pending measurement frames evaluating them in a single batch 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
52 * so that the the browser is guaranteed to avoid multiple layouts. 77 * so that the the browser is guaranteed to avoid multiple layouts.
53 */ 78 */
54 void _runMeasurementFrames() { 79 void _completeMeasurementFutures() {
55 if (_nextMeasurementFrameScheduled == false || _inMeasurementFrame) { 80 if (_nextMeasurementFrameScheduled == false) {
56 // Ignore spurious call to this function. 81 // Ignore spurious call to this function.
57 return; 82 return;
58 } 83 }
59 84
60 _inMeasurementFrame = true; 85 _nextMeasurementFrameScheduled = false;
61 86 // We must compute all new values before fulfilling the futures as
62 final layoutCallbacks = <LayoutCallback>[]; 87 // the onComplete callbacks for the futures could modify the DOM making
63 while (!_pendingMeasurementFrameCallbacks.isEmpty()) { 88 // subsequent measurement calculations expensive to compute.
64 MeasurementCallback measurementCallback = 89 if (_pendingRequests !== null) {
65 _pendingMeasurementFrameCallbacks.removeFirst(); 90 for (_MeasurementRequest request in _pendingRequests) {
66 try { 91 try {
67 final layoutCallback = measurementCallback(); 92 request.value = request.computeValue();
68 if (layoutCallback != null) { 93 } catch(var e) {
69 layoutCallbacks.add(layoutCallback); 94 request.value = e;
95 request.exception = true;
70 } 96 }
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.
75 } 97 }
76 } 98 }
77 99
78 _inMeasurementFrame = false; 100 final completedRequests = _pendingRequests;
79 _nextMeasurementFrameScheduled = false; 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 }
80 113
81 for (LayoutCallback layoutCallback in layoutCallbacks) { 114 if (readyMeasurementFrameCallbacks !== null) {
82 try { 115 for (TimeoutHandler handler in readyMeasurementFrameCallbacks) {
83 layoutCallback(); 116 // TODO(jacobr): wrap each call to a handler in a try-catch block.
84 } catch (Object e) { 117 handler();
85 window.console.error('Caught exception in layout callback: ${e}');
86 // TODO(jacobr): throw this exception again in the correct way.
87 } 118 }
88 } 119 }
89 } 120 }
OLDNEW
« no previous file with comments | « client/html/src/EventTargetWrappingImplementation.dart ('k') | client/html/src/Node.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698