Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(200)

Side by Side Diff: packages/observe/lib/src/dirty_check.dart

Issue 2989763002: Update charted to 0.4.8 and roll (Closed)
Patch Set: Removed Cutch from list of reviewers Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « packages/observe/lib/src/change_record.dart ('k') | packages/observe/lib/src/list_diff.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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());
OLDNEW
« no previous file with comments | « packages/observe/lib/src/change_record.dart ('k') | packages/observe/lib/src/list_diff.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698