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

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

Issue 10951016: Fixing up measurement test. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Removing static modifier from isMutationObserverSupported. Created 8 years, 3 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 | « lib/html/dartium/html_dartium.dart ('k') | lib/html/src/dart2js_MutationObserverSupported.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) 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 typedef Object ComputeValue();
6 6
7 class _MeasurementRequest<T> { 7 class _MeasurementRequest<T> {
8 final ComputeValue computeValue; 8 final ComputeValue computeValue;
9 final Completer<T> completer; 9 final Completer<T> completer;
10 Object value; 10 Object value;
11 bool exception = false; 11 bool exception = false;
12 _MeasurementRequest(this.computeValue, this.completer); 12 _MeasurementRequest(this.computeValue, this.completer);
13 } 13 }
14 14
15 const _MEASUREMENT_MESSAGE = "DART-MEASURE"; 15 typedef void _MeasurementCallback();
16 List<_MeasurementRequest> _pendingRequests;
17 List<TimeoutHandler> _pendingMeasurementFrameCallbacks;
18 bool _nextMeasurementFrameScheduled = false;
19 bool _firstMeasurementRequest = true;
20 16
21 void _maybeScheduleMeasurementFrame() {
22 if (_nextMeasurementFrameScheduled) return;
23 17
24 _nextMeasurementFrameScheduled = true; 18 /**
25 // postMessage gives us a way to receive a callback after the current 19 * This class attempts to invoke a callback as soon as the current event stack
26 // event listener has unwound but before the browser has repainted. 20 * unwinds, but before the browser repaints.
27 if (_firstMeasurementRequest) { 21 */
28 // Messages from other windows do not cause a security risk as 22 abstract class _MeasurementScheduler {
29 // all we care about is that _onCompleteMeasurementRequests is called 23 bool _nextMeasurementFrameScheduled = false;
30 // after the current event loop is unwound and calling the function is 24 _MeasurementCallback _callback;
31 // a noop when zero requests are pending. 25
32 window.on.message.add((e) => _completeMeasurementFutures()); 26 _MeasurementScheduler(this._callback);
33 _firstMeasurementRequest = false; 27
28 /**
29 * Creates the best possible measurement scheduler for the current platform.
30 */
31 factory _MeasurementScheduler.best(_MeasurementCallback callback) {
32 if (_isMutationObserverSupported()) {
33 return new _MutationObserverScheduler(callback);
34 }
35 return new _PostMessageScheduler(callback);
34 } 36 }
35 37
36 // TODO(jacobr): other mechanisms such as setImmediate and 38 /**
37 // requestAnimationFrame may work better of platforms that support them. 39 * Schedules a measurement callback if one has not been scheduled already.
38 // The key is we need a way to execute code immediately after the current 40 */
39 // event listener queue unwinds. 41 void maybeSchedule() {
40 window.postMessage(_MEASUREMENT_MESSAGE, "*"); 42 if (this._nextMeasurementFrameScheduled) {
43 return;
44 }
45 this._nextMeasurementFrameScheduled = true;
46 this._schedule();
47 }
48
49 /**
50 * Does the actual scheduling of the callback.
51 */
52 void _schedule();
53
54 /**
55 * Handles the measurement callback and forwards it if necessary.
56 */
57 void _onCallback() {
58 // Ignore spurious messages.
59 if (!_nextMeasurementFrameScheduled) {
60 return;
61 }
62 _nextMeasurementFrameScheduled = false;
63 this._callback();
64 }
41 } 65 }
42 66
43 /** 67 /**
68 * Scheduler which uses window.postMessage to schedule events.
69 */
70 class _PostMessageScheduler extends _MeasurementScheduler {
71 const _MEASUREMENT_MESSAGE = "DART-MEASURE";
72
73 _PostMessageScheduler(_MeasurementCallback callback): super(callback) {
74 // Messages from other windows do not cause a security risk as
75 // all we care about is that _handleMessage is called
76 // after the current event loop is unwound and calling the function is
77 // a noop when zero requests are pending.
78 window.on.message.add(this._handleMessage);
79 }
80
81 void _schedule() {
82 window.postMessage(_MEASUREMENT_MESSAGE, "*");
83 }
84
85 _handleMessage(e) {
86 this._onCallback();
87 }
88 }
89
90 /**
91 * Scheduler which uses a MutationObserver to schedule events.
92 */
93 class _MutationObserverScheduler extends _MeasurementScheduler {
94 MutationObserver _observer;
95 Element _dummy;
96
97 _MutationObserverScheduler(_MeasurementCallback callback): super(callback) {
98 // Mutation events get fired as soon as the current event stack is unwound
99 // so we just make a dummy event and listen for that.
100 _observer = new MutationObserver(this._handleMutation);
101 _dummy = new DivElement();
102 _observer.observe(_dummy, {}, attributes: true);
103 }
104
105 void _schedule() {
106 // Toggle it to trigger the mutation event.
107 _dummy.hidden = !_dummy.hidden;
108 }
109
110 _handleMutation(List<MutationRecord> mutations, MutationObserver observer) {
111 this._onCallback();
112 }
113 }
114
115
116 List<_MeasurementRequest> _pendingRequests;
117 List<TimeoutHandler> _pendingMeasurementFrameCallbacks;
118 _MeasurementScheduler _measurementScheduler = null;
119
120 void _maybeScheduleMeasurementFrame() {
121 if (_measurementScheduler == null) {
122 _measurementScheduler =
123 new _MeasurementScheduler.best(_completeMeasurementFutures);
124 }
125 _measurementScheduler.maybeSchedule();
126 }
127
128 /**
44 * Registers a [callback] which is called after the next batch of measurements 129 * Registers a [callback] which is called after the next batch of measurements
45 * completes. Even if no measurements completed, the callback is triggered 130 * completes. Even if no measurements completed, the callback is triggered
46 * when they would have completed to avoid confusing bugs if it happened that 131 * when they would have completed to avoid confusing bugs if it happened that
47 * no measurements were actually requested. 132 * no measurements were actually requested.
48 */ 133 */
49 void _addMeasurementFrameCallback(TimeoutHandler callback) { 134 void _addMeasurementFrameCallback(TimeoutHandler callback) {
50 if (_pendingMeasurementFrameCallbacks === null) { 135 if (_pendingMeasurementFrameCallbacks === null) {
51 _pendingMeasurementFrameCallbacks = <TimeoutHandler>[]; 136 _pendingMeasurementFrameCallbacks = <TimeoutHandler>[];
52 _maybeScheduleMeasurementFrame(); 137 _maybeScheduleMeasurementFrame();
53 } 138 }
(...skipping 16 matching lines...) Expand all
70 } 155 }
71 _pendingRequests.add(new _MeasurementRequest(computeValue, completer)); 156 _pendingRequests.add(new _MeasurementRequest(computeValue, completer));
72 return completer.future; 157 return completer.future;
73 } 158 }
74 159
75 /** 160 /**
76 * Complete all pending measurement futures evaluating them in a single batch 161 * Complete all pending measurement futures evaluating them in a single batch
77 * so that the the browser is guaranteed to avoid multiple layouts. 162 * so that the the browser is guaranteed to avoid multiple layouts.
78 */ 163 */
79 void _completeMeasurementFutures() { 164 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 165 // We must compute all new values before fulfilling the futures as
87 // the onComplete callbacks for the futures could modify the DOM making 166 // the onComplete callbacks for the futures could modify the DOM making
88 // subsequent measurement calculations expensive to compute. 167 // subsequent measurement calculations expensive to compute.
89 if (_pendingRequests !== null) { 168 if (_pendingRequests !== null) {
90 for (_MeasurementRequest request in _pendingRequests) { 169 for (_MeasurementRequest request in _pendingRequests) {
91 try { 170 try {
92 request.value = request.computeValue(); 171 request.value = request.computeValue();
93 } catch (e) { 172 } catch (e) {
94 request.value = e; 173 request.value = e;
95 request.exception = true; 174 request.exception = true;
(...skipping 15 matching lines...) Expand all
111 } 190 }
112 } 191 }
113 192
114 if (readyMeasurementFrameCallbacks !== null) { 193 if (readyMeasurementFrameCallbacks !== null) {
115 for (TimeoutHandler handler in readyMeasurementFrameCallbacks) { 194 for (TimeoutHandler handler in readyMeasurementFrameCallbacks) {
116 // TODO(jacobr): wrap each call to a handler in a try-catch block. 195 // TODO(jacobr): wrap each call to a handler in a try-catch block.
117 handler(); 196 handler();
118 } 197 }
119 } 198 }
120 } 199 }
OLDNEW
« no previous file with comments | « lib/html/dartium/html_dartium.dart ('k') | lib/html/src/dart2js_MutationObserverSupported.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698