OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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 /** | 5 /** |
6 * *Warning*: this library is **internal**, and APIs are subject to change. | 6 * *Warning*: this library is **internal**, and APIs are subject to change. |
7 * | 7 * |
8 * Tracks observable objects for dirty checking and testing purposes. | 8 * Tracks observable objects for dirty checking and testing purposes. |
9 * | 9 * |
10 * It can collect all observed objects, which can be used to trigger predictable | 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 | 11 * delivery of all pending changes in a test, including objects allocated |
12 * internally to another library, such as those in `package:mdv`. | 12 * internally to another library, such as those in `package:mdv`. |
13 */ | 13 */ |
14 library observe.src.dirty_check; | 14 library observe.src.dirty_check; |
15 | 15 |
| 16 import 'dart:async'; |
16 import 'package:logging/logging.dart'; | 17 import 'package:logging/logging.dart'; |
17 import 'package:observe/observe.dart' show Observable; | 18 import 'package:observe/observe.dart' show Observable; |
18 | 19 |
19 /** The number of active observables in the system. */ | 20 /** The number of active observables in the system. */ |
20 int get allObservablesCount => _allObservablesCount; | 21 int get allObservablesCount => _allObservablesCount; |
21 | 22 |
22 int _allObservablesCount = 0; | 23 int _allObservablesCount = 0; |
23 | 24 |
24 List<Observable> _allObservables = null; | 25 List<Observable> _allObservables = null; |
25 | 26 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 '${info[0]}, object: ${info[1]}.'); | 78 '${info[0]}, object: ${info[1]}.'); |
78 } | 79 } |
79 } | 80 } |
80 | 81 |
81 _allObservablesCount = _allObservables.length; | 82 _allObservablesCount = _allObservables.length; |
82 _delivering = false; | 83 _delivering = false; |
83 } | 84 } |
84 | 85 |
85 const MAX_DIRTY_CHECK_CYCLES = 1000; | 86 const MAX_DIRTY_CHECK_CYCLES = 1000; |
86 | 87 |
87 | |
88 /** | 88 /** |
89 * Log for messages produced at runtime by this library. Logging can be | 89 * Log for messages produced at runtime by this library. Logging can be |
90 * configured by accessing Logger.root from the logging library. | 90 * configured by accessing Logger.root from the logging library. |
91 */ | 91 */ |
92 final Logger _logger = new Logger('Observable.dirtyCheck'); | 92 final Logger _logger = new Logger('Observable.dirtyCheck'); |
| 93 |
| 94 /** |
| 95 * Creates a [ZoneSpecification] to set up automatic dirty checking after each |
| 96 * batch of async operations. This ensures that change notifications are always |
| 97 * delivered. Typically used via [dirtyCheckZone]. |
| 98 */ |
| 99 ZoneSpecification dirtyCheckZoneSpec() { |
| 100 bool pending = false; |
| 101 |
| 102 enqueueDirtyCheck(ZoneDelegate parent, Zone zone) { |
| 103 // Only schedule one dirty check per microtask. |
| 104 if (pending) return; |
| 105 |
| 106 pending = true; |
| 107 parent.scheduleMicrotask(zone, () { |
| 108 pending = false; |
| 109 Observable.dirtyCheck(); |
| 110 }); |
| 111 } |
| 112 |
| 113 wrapCallback(Zone self, ZoneDelegate parent, Zone zone, f()) { |
| 114 // TODO(jmesserly): why does this happen? |
| 115 if (f == null) return f; |
| 116 return () { |
| 117 enqueueDirtyCheck(parent, zone); |
| 118 return f(); |
| 119 }; |
| 120 } |
| 121 |
| 122 wrapUnaryCallback(Zone self, ZoneDelegate parent, Zone zone, f(x)) { |
| 123 // TODO(jmesserly): why does this happen? |
| 124 if (f == null) return f; |
| 125 return (x) { |
| 126 enqueueDirtyCheck(parent, zone); |
| 127 return f(x); |
| 128 }; |
| 129 } |
| 130 |
| 131 return new ZoneSpecification( |
| 132 registerCallback: wrapCallback, |
| 133 registerUnaryCallback: wrapUnaryCallback); |
| 134 } |
| 135 |
| 136 /** |
| 137 * Forks a [Zone] off the current one that does dirty-checking automatically |
| 138 * after each batch of async operations. Equivalent to: |
| 139 * |
| 140 * Zone.current.fork(specification: dirtyCheckZoneSpec()); |
| 141 */ |
| 142 Zone dirtyCheckZone() => Zone.current.fork(specification: dirtyCheckZoneSpec()); |
OLD | NEW |