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

Unified Diff: pkg/observe/lib/src/watcher.dart

Issue 19771010: implement dirty checking for @observable objects (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 5 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
Index: pkg/observe/lib/src/watcher.dart
diff --git a/pkg/observe/lib/src/watcher.dart b/pkg/observe/lib/src/watcher.dart
new file mode 100644
index 0000000000000000000000000000000000000000..d10feb1961bbf9c8f120888906e169cd8792d798
--- /dev/null
+++ b/pkg/observe/lib/src/watcher.dart
@@ -0,0 +1,68 @@
+// Copyright (c) 2013, 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.
+
+/**
+ * *Warning*: this library is **internal**, and APIs are subject to change.
+ *
+ * Tracks observable objects for dirty checking and testing purposes.
+ *
+ * It can collect all observed objects, which can be used to trigger predictable
+ * delivery of all pending changes in a test, including objects allocated
+ * internally to another library, such as those in `package:mdv`.
+ */
+library observe.src.watcher;
+
+import 'package:observe/observe.dart' show Observable;
+
+/** The number of active observables in the system. */
+int get allObservablesCount => _allObservablesCount;
+
+int _allObservablesCount = 0;
+
+List<Observable> _allObservables = null;
+
+bool _delivering = false;
+
+void registerObservable(Observable obj) {
+ if (_allObservables == null) _allObservables = <Observable>[];
+ _allObservables.add(obj);
+ _allObservablesCount++;
+}
+
+/**
+ * Synchronously deliver all change records for known observables.
+ *
+ * This will execute [Observable.deliverChanges] on objects that inherit from
+ * [ObservableMixin].
+ */
+// Note: this is called performMicrotaskCheckpoint in change_summary.js.
+void dirtyCheck() {
+ if (_delivering) return;
+ if (_allObservables == null) return;
+
+ _delivering = true;
+
+ int cycles = 0;
+ bool anyChanged = false;
+ do {
+ cycles++;
+ var toCheck = _allObservables;
+ _allObservables = <Observable>[];
+ anyChanged = false;
+
+ for (final observer in toCheck) {
+ if (observer.hasObservers) {
+ anyChanged = anyChanged || observer.deliverChanges();
+ _allObservables.add(observer);
+ }
+ }
+ } while (cycles < _MAX_DIRTY_CHECK_CYCLES && anyChanged);
+
+ // TODO(jmesserly): warn if limit is reached?
+
+ _allObservablesCount = _allObservables.length;
+ _delivering = false;
+}
+
+var _MAX_DIRTY_CHECK_CYCLES = 1000;

Powered by Google App Engine
This is Rietveld 408576698