| 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 /// *Warning*: this library is **internal**, and APIs are subject to change. | 5 /// *Warning*: this library is **internal**, and APIs are subject to change. |
| 6 /// | 6 /// |
| 7 /// Tracks observable objects for dirty checking and testing purposes. | 7 /// Tracks observable objects for dirty checking and testing purposes. |
| 8 /// | 8 /// |
| 9 /// It can collect all observed objects, which can be used to trigger | 9 /// It can collect all observed objects, which can be used to trigger |
| 10 /// predictable delivery of all pending changes in a test, including objects | 10 /// predictable delivery of all pending changes in a test, including objects |
| 11 /// allocated internally to another library, such as those in | 11 /// allocated internally to another library, such as those in |
| 12 /// `package:template_binding`. | 12 /// `package:template_binding`. |
| 13 library observe.src.dirty_check; | 13 library observe.src.dirty_check; |
| 14 | 14 |
| 15 import 'dart:async'; | 15 import 'dart:async'; |
| 16 import 'package:func/func.dart'; |
| 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 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 // Only schedule one dirty check per microtask. | 97 // Only schedule one dirty check per microtask. |
| 97 if (pending) return; | 98 if (pending) return; |
| 98 | 99 |
| 99 pending = true; | 100 pending = true; |
| 100 parent.scheduleMicrotask(zone, () { | 101 parent.scheduleMicrotask(zone, () { |
| 101 pending = false; | 102 pending = false; |
| 102 Observable.dirtyCheck(); | 103 Observable.dirtyCheck(); |
| 103 }); | 104 }); |
| 104 } | 105 } |
| 105 | 106 |
| 106 wrapCallback(Zone self, ZoneDelegate parent, Zone zone, f()) { | 107 Func0 wrapCallback(Zone self, ZoneDelegate parent, Zone zone, f()) { |
| 107 // TODO(jmesserly): why does this happen? | 108 // TODO(jmesserly): why does this happen? |
| 108 if (f == null) return f; | 109 if (f == null) return f; |
| 109 return () { | 110 return () { |
| 110 enqueueDirtyCheck(parent, zone); | 111 enqueueDirtyCheck(parent, zone); |
| 111 return f(); | 112 return f(); |
| 112 }; | 113 }; |
| 113 } | 114 } |
| 114 | 115 |
| 115 wrapUnaryCallback(Zone self, ZoneDelegate parent, Zone zone, f(x)) { | 116 Func1 wrapUnaryCallback(Zone self, ZoneDelegate parent, Zone zone, f(x)) { |
| 116 // TODO(jmesserly): why does this happen? | 117 // TODO(jmesserly): why does this happen? |
| 117 if (f == null) return f; | 118 if (f == null) return f; |
| 118 return (x) { | 119 return (x) { |
| 119 enqueueDirtyCheck(parent, zone); | 120 enqueueDirtyCheck(parent, zone); |
| 120 return f(x); | 121 return f(x); |
| 121 }; | 122 }; |
| 122 } | 123 } |
| 123 | 124 |
| 124 return new ZoneSpecification( | 125 return new ZoneSpecification( |
| 125 registerCallback: wrapCallback, | 126 registerCallback: wrapCallback, registerUnaryCallback: wrapUnaryCallback); |
| 126 registerUnaryCallback: wrapUnaryCallback); | |
| 127 } | 127 } |
| 128 | 128 |
| 129 /// Forks a [Zone] off the current one that does dirty-checking automatically | 129 /// Forks a [Zone] off the current one that does dirty-checking automatically |
| 130 /// after each batch of async operations. Equivalent to: | 130 /// after each batch of async operations. Equivalent to: |
| 131 /// | 131 /// |
| 132 /// Zone.current.fork(specification: dirtyCheckZoneSpec()); | 132 /// Zone.current.fork(specification: dirtyCheckZoneSpec()); |
| 133 Zone dirtyCheckZone() => Zone.current.fork(specification: dirtyCheckZoneSpec()); | 133 Zone dirtyCheckZone() => Zone.current.fork(specification: dirtyCheckZoneSpec()); |
| OLD | NEW |