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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « client/html/src/EventTargetWrappingImplementation.dart ('k') | client/html/src/Node.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: client/html/src/Measurement.dart
diff --git a/client/html/src/Measurement.dart b/client/html/src/Measurement.dart
index fa2a37fe40dc39772a01bf391fa4fa671ae2743f..e92a46c82b7d82ebd44be89fded51e59a5e56b91 100644
--- a/client/html/src/Measurement.dart
+++ b/client/html/src/Measurement.dart
@@ -1,12 +1,20 @@
-// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
+// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-bool _inMeasurementFrame = false;
+typedef Object ComputeValue();
-final _MEASUREMENT_MESSAGE = "DART-MEASURE";
+class _MeasurementRequest<T> {
+ final ComputeValue computeValue;
+ final Completer<T> completer;
+ Object value;
+ bool exception = false;
+ _MeasurementRequest(this.computeValue, this.completer);
+}
-Queue<MeasurementCallback> _pendingMeasurementFrameCallbacks;
+final _MEASUREMENT_MESSAGE = "DART-MEASURE";
+List<_MeasurementRequest> _pendingRequests;
+List<TimeoutHandler> _pendingMeasurementFrameCallbacks;
bool _nextMeasurementFrameScheduled = false;
bool _firstMeasurementRequest = true;
@@ -19,9 +27,9 @@ void _maybeScheduleMeasurementFrame() {
if (_firstMeasurementRequest) {
// Messages from other windows do not cause a security risk as
// all we care about is that _onCompleteMeasurementRequests is called
- // after the current event loop is unwound and calling
- // _runMeasurementFrames is a noop when zero requests are pending.
- window.on.message.add((e) => _runMeasurementFrames());
+ // after the current event loop is unwound and calling the function is
+ // a noop when zero requests are pending.
+ window.on.message.add((e) => _completeMeasurementFutures());
_firstMeasurementRequest = false;
}
@@ -38,52 +46,75 @@ void _maybeScheduleMeasurementFrame() {
* when they would have completed to avoid confusing bugs if it happened that
* no measurements were actually requested.
*/
-void _addMeasurementFrameCallback(MeasurementCallback callback) {
- assert(callback != null);
+void _addMeasurementFrameCallback(TimeoutHandler callback) {
if (_pendingMeasurementFrameCallbacks === null) {
- _pendingMeasurementFrameCallbacks = new Queue<MeasurementCallback>();
+ _pendingMeasurementFrameCallbacks = <TimeoutHandler>[];
+ _maybeScheduleMeasurementFrame();
}
- _maybeScheduleMeasurementFrame();
_pendingMeasurementFrameCallbacks.add(callback);
}
/**
- * Run all pending measurement frames evaluating them in a single batch
+ * Returns a [Future] whose value will be the result of evaluating
+ * [computeValue] during the next safe measurement interval.
+ * The next safe measurement interval is after the current event loop has
+ * unwound but before the browser has rendered the page.
+ * It is important that the [computeValue] function only queries the html
+ * layout and html in any way.
+ */
+Future _createMeasurementFuture(ComputeValue computeValue,
+ Completer completer) {
+ if (_pendingRequests === null) {
+ _pendingRequests = <_MeasurementRequest>[];
+ _maybeScheduleMeasurementFrame();
+ }
+ _pendingRequests.add(new _MeasurementRequest(computeValue, completer));
+ return completer.future;
+}
+
+/**
+ * Complete all pending measurement futures evaluating them in a single batch
* so that the the browser is guaranteed to avoid multiple layouts.
*/
-void _runMeasurementFrames() {
- if (_nextMeasurementFrameScheduled == false || _inMeasurementFrame) {
+void _completeMeasurementFutures() {
+ if (_nextMeasurementFrameScheduled == false) {
// Ignore spurious call to this function.
return;
}
- _inMeasurementFrame = true;
-
- final layoutCallbacks = <LayoutCallback>[];
- while (!_pendingMeasurementFrameCallbacks.isEmpty()) {
- MeasurementCallback measurementCallback =
- _pendingMeasurementFrameCallbacks.removeFirst();
- try {
- final layoutCallback = measurementCallback();
- if (layoutCallback != null) {
- layoutCallbacks.add(layoutCallback);
+ _nextMeasurementFrameScheduled = false;
+ // We must compute all new values before fulfilling the futures as
+ // the onComplete callbacks for the futures could modify the DOM making
+ // subsequent measurement calculations expensive to compute.
+ if (_pendingRequests !== null) {
+ for (_MeasurementRequest request in _pendingRequests) {
+ try {
+ request.value = request.computeValue();
+ } catch(var e) {
+ request.value = e;
+ request.exception = true;
}
- } catch (Object e) {
- window.console.error(
- 'Caught exception in measurement frame callback: ${e}');
- // TODO(jacobr): throw this exception again in the correct way.
}
}
- _inMeasurementFrame = false;
- _nextMeasurementFrameScheduled = false;
+ final completedRequests = _pendingRequests;
+ final readyMeasurementFrameCallbacks = _pendingMeasurementFrameCallbacks;
+ _pendingRequests = null;
+ _pendingMeasurementFrameCallbacks = null;
+ if (completedRequests !== null) {
+ for (_MeasurementRequest request in completedRequests) {
+ if (request.exception) {
+ request.completer.completeException(request.value);
+ } else {
+ request.completer.complete(request.value);
+ }
+ }
+ }
- for (LayoutCallback layoutCallback in layoutCallbacks) {
- try {
- layoutCallback();
- } catch (Object e) {
- window.console.error('Caught exception in layout callback: ${e}');
- // TODO(jacobr): throw this exception again in the correct way.
+ if (readyMeasurementFrameCallbacks !== null) {
+ for (TimeoutHandler handler in readyMeasurementFrameCallbacks) {
+ // TODO(jacobr): wrap each call to a handler in a try-catch block.
+ handler();
}
}
}
« 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