Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
| 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. | |
| 4 | |
| 5 /** | |
| 6 * *Warning*: this library is **internal**, and APIs are subject to change. | |
| 7 * | |
| 8 * Tracks observable objects for dirty checking and testing purposes. | |
| 9 * | |
| 10 * It can collect all observed objects, which can be used to trigger predictable | |
| 11 * delivery of all pending changes in a test, including objects allocated | |
| 12 * internally to another library, such as those in `package:mdv`. | |
| 13 */ | |
| 14 library observe.src.watcher; | |
| 15 | |
| 16 import 'package:observe/observe.dart' show Observable; | |
| 17 | |
| 18 /** The number of active observables in the system. */ | |
| 19 int get allObservablesCount => _allObservablesCount; | |
|
Jennifer Messerly
2013/07/19 01:32:58
this is public so tests can get at it. it's not us
| |
| 20 | |
| 21 int _allObservablesCount = 0; | |
| 22 | |
| 23 List<Observable> _allObservables = null; | |
| 24 | |
| 25 bool _delivering = false; | |
| 26 | |
| 27 void registerObservable(Observable obj) { | |
| 28 if (_allObservables == null) _allObservables = <Observable>[]; | |
| 29 _allObservables.add(obj); | |
| 30 _allObservablesCount++; | |
| 31 } | |
| 32 | |
| 33 /** | |
| 34 * Synchronously deliver all change records for known observables. | |
| 35 * | |
| 36 * This will execute [Observable.deliverChanges] on objects that inherit from | |
| 37 * [ObservableMixin]. | |
| 38 */ | |
| 39 // Note: this is called performMicrotaskCheckpoint in change_summary.js. | |
| 40 void dirtyCheck() { | |
| 41 if (_delivering) return; | |
| 42 if (_allObservables == null) return; | |
| 43 | |
| 44 _delivering = true; | |
| 45 | |
| 46 int cycles = 0; | |
| 47 bool anyChanged = false; | |
| 48 do { | |
| 49 cycles++; | |
| 50 var toCheck = _allObservables; | |
| 51 _allObservables = <Observable>[]; | |
| 52 anyChanged = false; | |
| 53 | |
| 54 for (final observer in toCheck) { | |
| 55 if (observer.hasObservers) { | |
| 56 anyChanged = anyChanged || observer.deliverChanges(); | |
| 57 _allObservables.add(observer); | |
| 58 } | |
| 59 } | |
| 60 } while (cycles < _MAX_DIRTY_CHECK_CYCLES && anyChanged); | |
| 61 | |
| 62 // TODO(jmesserly): warn if limit is reached? | |
| 63 | |
| 64 _allObservablesCount = _allObservables.length; | |
| 65 _delivering = false; | |
| 66 } | |
| 67 | |
| 68 var _MAX_DIRTY_CHECK_CYCLES = 1000; | |
| OLD | NEW |