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; | |
|
Siggi Cherem (dart-lang)
2013/07/22 23:06:14
minor suggestion: consider renaming this to dirty_
Jennifer Messerly
2013/07/23 02:45:15
Done.
| |
| 15 | |
| 16 import 'package:logging/logging.dart'; | |
| 17 import 'package:observe/observe.dart' show Observable; | |
| 18 | |
| 19 /** The number of active observables in the system. */ | |
| 20 int get allObservablesCount => _allObservablesCount; | |
| 21 | |
| 22 int _allObservablesCount = 0; | |
| 23 | |
| 24 List<Observable> _allObservables = null; | |
| 25 | |
| 26 bool _delivering = false; | |
| 27 | |
| 28 void registerObservable(Observable obj) { | |
| 29 if (_allObservables == null) _allObservables = <Observable>[]; | |
| 30 _allObservables.add(obj); | |
| 31 _allObservablesCount++; | |
| 32 } | |
| 33 | |
| 34 /** | |
| 35 * Synchronously deliver all change records for known observables. | |
| 36 * | |
| 37 * This will execute [Observable.deliverChanges] on objects that inherit from | |
| 38 * [ObservableMixin]. | |
| 39 */ | |
| 40 // Note: this is called performMicrotaskCheckpoint in change_summary.js. | |
| 41 void dirtyCheckObservables() { | |
| 42 if (_delivering) return; | |
| 43 if (_allObservables == null) return; | |
| 44 | |
| 45 _delivering = true; | |
| 46 | |
| 47 int cycles = 0; | |
| 48 bool anyChanged = false; | |
| 49 List debugLoop = null; | |
| 50 do { | |
| 51 cycles++; | |
| 52 if (cycles == MAX_DIRTY_CHECK_CYCLES) { | |
| 53 debugLoop = []; | |
| 54 } | |
| 55 | |
| 56 var toCheck = _allObservables; | |
| 57 _allObservables = <Observable>[]; | |
| 58 anyChanged = false; | |
| 59 | |
| 60 for (int i = 0; i < toCheck.length; i++) { | |
| 61 final observer = toCheck[i]; | |
| 62 if (observer.hasObservers) { | |
| 63 if (observer.deliverChanges()) { | |
| 64 anyChanged = true; | |
| 65 if (debugLoop != null) debugLoop.add([i, observer]); | |
| 66 } | |
| 67 _allObservables.add(observer); | |
| 68 } | |
| 69 } | |
| 70 } while (cycles < MAX_DIRTY_CHECK_CYCLES && anyChanged); | |
| 71 | |
| 72 if (debugLoop != null && anyChanged) { | |
| 73 _logger.warning('Possible loop in Observable.dirtyCheck, stopped ' | |
| 74 'checking.'); | |
| 75 for (final info in debugLoop) { | |
| 76 _logger.warning('In last iteration Observable changed at index ' | |
| 77 '${info[0]}, object: ${info[1]}.'); | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 _allObservablesCount = _allObservables.length; | |
| 82 _delivering = false; | |
| 83 } | |
| 84 | |
| 85 const MAX_DIRTY_CHECK_CYCLES = 1000; | |
| 86 | |
| 87 | |
| 88 /** | |
| 89 * Log for messages produced at runtime by this library. Logging can be | |
| 90 * configured by accessing Logger.root from the logging library. | |
| 91 */ | |
| 92 final Logger _logger = new Logger('Observable.dirtyCheck'); | |
| OLD | NEW |