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 /// *Warning*: this library is **internal**, and APIs are subject to change. | |
6 /// | |
7 /// Tracks observable objects for dirty checking and testing purposes. | |
8 /// | |
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 | |
11 /// allocated internally to another library, such as those in | |
12 /// `package:template_binding`. | |
13 library observe.src.dirty_check; | |
14 | |
15 import 'dart:async'; | |
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 /// Synchronously deliver all change records for known observables. | |
35 /// | |
36 /// This will execute [Observable.deliverChanges] on objects that inherit from | |
37 /// [Observable]. | |
38 // Note: this is called performMicrotaskCheckpoint in change_summary.js. | |
39 void dirtyCheckObservables() { | |
40 if (_delivering) return; | |
41 if (_allObservables == null) return; | |
42 | |
43 _delivering = true; | |
44 | |
45 int cycles = 0; | |
46 bool anyChanged = false; | |
47 List debugLoop = null; | |
48 do { | |
49 cycles++; | |
50 if (cycles == MAX_DIRTY_CHECK_CYCLES) { | |
51 debugLoop = []; | |
52 } | |
53 | |
54 var toCheck = _allObservables; | |
55 _allObservables = <Observable>[]; | |
56 anyChanged = false; | |
57 | |
58 for (int i = 0; i < toCheck.length; i++) { | |
59 final observer = toCheck[i]; | |
60 if (observer.hasObservers) { | |
61 if (observer.deliverChanges()) { | |
62 anyChanged = true; | |
63 if (debugLoop != null) debugLoop.add([i, observer]); | |
64 } | |
65 _allObservables.add(observer); | |
66 } | |
67 } | |
68 } while (cycles < MAX_DIRTY_CHECK_CYCLES && anyChanged); | |
69 | |
70 if (debugLoop != null && anyChanged) { | |
71 _logger.warning('Possible loop in Observable.dirtyCheck, stopped ' | |
72 'checking.'); | |
73 for (final info in debugLoop) { | |
74 _logger.warning('In last iteration Observable changed at index ' | |
75 '${info[0]}, object: ${info[1]}.'); | |
76 } | |
77 } | |
78 | |
79 _allObservablesCount = _allObservables.length; | |
80 _delivering = false; | |
81 } | |
82 | |
83 const MAX_DIRTY_CHECK_CYCLES = 1000; | |
84 | |
85 /// Log for messages produced at runtime by this library. Logging can be | |
86 /// configured by accessing Logger.root from the logging library. | |
87 final Logger _logger = new Logger('Observable.dirtyCheck'); | |
88 | |
89 /// Creates a [ZoneSpecification] to set up automatic dirty checking after each | |
90 /// batch of async operations. This ensures that change notifications are always | |
91 /// delivered. Typically used via [dirtyCheckZone]. | |
92 ZoneSpecification dirtyCheckZoneSpec() { | |
93 bool pending = false; | |
94 | |
95 enqueueDirtyCheck(ZoneDelegate parent, Zone zone) { | |
96 // Only schedule one dirty check per microtask. | |
97 if (pending) return; | |
98 | |
99 pending = true; | |
100 parent.scheduleMicrotask(zone, () { | |
101 pending = false; | |
102 Observable.dirtyCheck(); | |
103 }); | |
104 } | |
105 | |
106 wrapCallback(Zone self, ZoneDelegate parent, Zone zone, f()) { | |
107 // TODO(jmesserly): why does this happen? | |
108 if (f == null) return f; | |
109 return () { | |
110 enqueueDirtyCheck(parent, zone); | |
111 return f(); | |
112 }; | |
113 } | |
114 | |
115 wrapUnaryCallback(Zone self, ZoneDelegate parent, Zone zone, f(x)) { | |
116 // TODO(jmesserly): why does this happen? | |
117 if (f == null) return f; | |
118 return (x) { | |
119 enqueueDirtyCheck(parent, zone); | |
120 return f(x); | |
121 }; | |
122 } | |
123 | |
124 return new ZoneSpecification( | |
125 registerCallback: wrapCallback, | |
126 registerUnaryCallback: wrapUnaryCallback); | |
127 } | |
128 | |
129 /// Forks a [Zone] off the current one that does dirty-checking automatically | |
130 /// after each batch of async operations. Equivalent to: | |
131 /// | |
132 /// Zone.current.fork(specification: dirtyCheckZoneSpec()); | |
133 Zone dirtyCheckZone() => Zone.current.fork(specification: dirtyCheckZoneSpec()); | |
OLD | NEW |