Skip to content
This repository has been archived by the owner on Feb 22, 2018. It is now read-only.

fix(scope): log firing expressions in the watchLog #350

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/core/scope.dart
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,11 @@ class Scope implements Map {

watcherCount = 0;
scopeCount = 0;
var firedExpressions;
if (_ttl - _ttlLeft > 2) {
firedExpressions = <String>[];
watchLog.add(firedExpressions);
}
assert((timerId = _perf.startTimer('ng.dirty_check', _ttl-_ttlLeft)) != false);
digestLoop:
do { // "traverse the scopes" loop
Expand All @@ -456,6 +461,9 @@ class Scope implements Map {
var value = watch.get(current);
var last = watch.last;
if (!_identical(value, last)) {
if (_ttl - _ttlLeft > 2) {
firedExpressions.add(watch.exp == null ? '[unknown]' : watch.exp);
}
lastDirtyWatch = watch;
lastLoopLastDirtyWatch = null;
watch.last = value;
Expand Down Expand Up @@ -497,7 +505,7 @@ class Scope implements Map {
assert(_perf.stopTimer(timerId) != false);
if(lastDirtyWatch != null && (_ttlLeft--) == 0) {
throw '$_ttl \$digest() iterations reached. Aborting!\n' +
'Watchers fired in the last 5 iterations: ${_toJson(watchLog)}';
'Watchers fired in the last ${_ttl - 2} iterations: ${_toJson(watchLog)}';
}
} while (lastDirtyWatch != null || innerAsyncQueue.length > 0);
_perf.counters['ng.scope.watchers'] = watcherCount;
Expand Down
14 changes: 14 additions & 0 deletions test/core/scope_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,20 @@ main() {
}));


it(r'should prevent infinite digest and should log firing expressions', inject((Scope $rootScope) {
$rootScope['a'] = 0;
$rootScope['b'] = 0;
$rootScope.$watch('a = a + 1');
$rootScope.$watch('b = b + 1');

expect(() {
$rootScope.$digest();
}).toThrow('Watchers fired in the last 3 iterations: ['
'["a = a + 1","b = b + 1"],["a = a + 1","b = b + 1"],'
'["a = a + 1","b = b + 1"]]');
}));


it(r'should always call the watchr with newVal and oldVal equal on the first run',
inject((Scope $rootScope) {
var log = [];
Expand Down