mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-22 21:25:47 +00:00
fix(scope): fix edge case for $digest & $broadcast scope traversal
- fixed traversal originating on a scope with with a right sibling - unified code for both $broadcast and $digest
This commit is contained in:
parent
c763b009ac
commit
93f96a16f6
2 changed files with 81 additions and 59 deletions
87
src/Scope.js
87
src/Scope.js
|
|
@ -321,31 +321,31 @@ Scope.prototype = {
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
$digest: function() {
|
$digest: function() {
|
||||||
var watch, value, last, next,
|
var watch, value, last,
|
||||||
watchers,
|
watchers,
|
||||||
asyncQueue,
|
asyncQueue,
|
||||||
length,
|
length,
|
||||||
dirty, ttl = 100,
|
dirty, ttl = 100,
|
||||||
scope;
|
next, current, target = this;
|
||||||
|
|
||||||
if (this.$$phase) {
|
if (target.$$phase) {
|
||||||
throw Error(this.$$phase + ' already in progress');
|
throw Error(target.$$phase + ' already in progress');
|
||||||
}
|
}
|
||||||
do {
|
do {
|
||||||
|
|
||||||
dirty = false;
|
dirty = false;
|
||||||
scope = this;
|
current = target;
|
||||||
do {
|
do {
|
||||||
scope.$$phase = '$digest';
|
current.$$phase = '$digest';
|
||||||
asyncQueue = scope.$$asyncQueue;
|
asyncQueue = current.$$asyncQueue;
|
||||||
while(asyncQueue.length) {
|
while(asyncQueue.length) {
|
||||||
try {
|
try {
|
||||||
scope.$eval(asyncQueue.shift());
|
current.$eval(asyncQueue.shift());
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
scope.$service('$exceptionHandler')(e);
|
current.$service('$exceptionHandler')(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ((watchers = scope.$$watchers)) {
|
if ((watchers = current.$$watchers)) {
|
||||||
// process our watches
|
// process our watches
|
||||||
length = watchers.length;
|
length = watchers.length;
|
||||||
while (length--) {
|
while (length--) {
|
||||||
|
|
@ -353,28 +353,27 @@ Scope.prototype = {
|
||||||
watch = watchers[length];
|
watch = watchers[length];
|
||||||
// Most common watches are on primitives, in which case we can short
|
// Most common watches are on primitives, in which case we can short
|
||||||
// circuit it with === operator, only when === fails do we use .equals
|
// circuit it with === operator, only when === fails do we use .equals
|
||||||
if ((value = watch.get(scope)) !== (last = watch.last) && !equals(value, last)) {
|
if ((value = watch.get(current)) !== (last = watch.last) && !equals(value, last)) {
|
||||||
dirty = true;
|
dirty = true;
|
||||||
watch.fn(scope, watch.last = copy(value), last);
|
watch.fn(current, watch.last = copy(value), last);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
scope.$service('$exceptionHandler')(e);
|
current.$service('$exceptionHandler')(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
current.$$phase = null;
|
||||||
|
|
||||||
scope.$$phase = null;
|
// Insanity Warning: scope depth-first traversal
|
||||||
// find the next scope in traversal.
|
// yes, this code is a bit crazy, but it works and we have tests to prove it!
|
||||||
if (!(next = scope.$$childHead || scope.$$nextSibling) && scope !== this) {
|
// this piece should be kept in sync with the traversal in $broadcast
|
||||||
do {
|
if (!(next = (current.$$childHead || (current !== target && current.$$nextSibling)))) {
|
||||||
scope = scope.$parent;
|
while(current !== target && !(next = current.$$nextSibling)) {
|
||||||
if (scope == this || (next = scope.$$nextSibling)) {
|
current = current.$parent;
|
||||||
break;
|
}
|
||||||
}
|
|
||||||
} while (scope !== this);
|
|
||||||
}
|
}
|
||||||
} while ((scope = next));
|
} while ((current = next));
|
||||||
|
|
||||||
if(!(ttl--)) {
|
if(!(ttl--)) {
|
||||||
throw Error('100 $digest() iterations reached. Aborting!');
|
throw Error('100 $digest() iterations reached. Aborting!');
|
||||||
|
|
@ -651,44 +650,34 @@ Scope.prototype = {
|
||||||
* @param {...*} args Optional set of arguments which will be passed onto the event listeners.
|
* @param {...*} args Optional set of arguments which will be passed onto the event listeners.
|
||||||
*/
|
*/
|
||||||
$broadcast: function(name, args) {
|
$broadcast: function(name, args) {
|
||||||
var targetScope = this,
|
var target = this,
|
||||||
currentScope = targetScope,
|
current = target,
|
||||||
nextScope = targetScope,
|
next = target,
|
||||||
event = { name: name,
|
event = { name: name,
|
||||||
targetScope: targetScope },
|
targetScope: target },
|
||||||
listenerArgs = concat([event], arguments, 1);
|
listenerArgs = concat([event], arguments, 1);
|
||||||
|
|
||||||
//down while you can, then up and next sibling or up and next sibling until back at root
|
//down while you can, then up and next sibling or up and next sibling until back at root
|
||||||
do {
|
do {
|
||||||
currentScope = nextScope;
|
current = next;
|
||||||
event.currentScope = currentScope;
|
event.currentScope = current;
|
||||||
forEach(currentScope.$$listeners[name], function(listener) {
|
forEach(current.$$listeners[name], function(listener) {
|
||||||
try {
|
try {
|
||||||
listener.apply(null, listenerArgs);
|
listener.apply(null, listenerArgs);
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
currentScope.$service('$exceptionHandler')(e);
|
current.$service('$exceptionHandler')(e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// down or to the right!
|
// Insanity Warning: scope depth-first traversal
|
||||||
nextScope = currentScope.$$childHead || currentScope.$$nextSibling;
|
// yes, this code is a bit crazy, but it works and we have tests to prove it!
|
||||||
|
// this piece should be kept in sync with the traversal in $digest
|
||||||
if (nextScope) {
|
if (!(next = (current.$$childHead || (current !== target && current.$$nextSibling)))) {
|
||||||
// found child or sibling
|
while(current !== target && !(next = current.$$nextSibling)) {
|
||||||
continue;
|
current = current.$parent;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} while ((current = next));
|
||||||
// we have to restore nextScope and go up!
|
|
||||||
nextScope = currentScope;
|
|
||||||
|
|
||||||
while (!nextScope.$$nextSibling && (nextScope != targetScope)) {
|
|
||||||
nextScope = nextScope.$parent;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (nextScope != targetScope) {
|
|
||||||
nextScope = nextScope.$$nextSibling;
|
|
||||||
}
|
|
||||||
} while (nextScope != targetScope);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -153,7 +153,7 @@ describe('Scope', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
it('should delegate $digest to children in addition order', function() {
|
it('should call child $watchers in addition order', function() {
|
||||||
// this is not an external guarantee, just our own sanity
|
// this is not an external guarantee, just our own sanity
|
||||||
var log = '';
|
var log = '';
|
||||||
var childA = root.$new();
|
var childA = root.$new();
|
||||||
|
|
@ -168,6 +168,30 @@ describe('Scope', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('should allow $digest on a child scope with and without a right sibling', function() {
|
||||||
|
// tests a traversal edge case which we originally missed
|
||||||
|
var log = '',
|
||||||
|
childA = root.$new(),
|
||||||
|
childB = root.$new();
|
||||||
|
|
||||||
|
root.$watch(function() { log += 'r'; });
|
||||||
|
childA.$watch(function() { log += 'a'; });
|
||||||
|
childB.$watch(function() { log += 'b'; });
|
||||||
|
|
||||||
|
// init
|
||||||
|
root.$digest();
|
||||||
|
expect(log).toBe('rabrab');
|
||||||
|
|
||||||
|
log = '';
|
||||||
|
childA.$digest();
|
||||||
|
expect(log).toBe('a');
|
||||||
|
|
||||||
|
log = '';
|
||||||
|
childB.$digest();
|
||||||
|
expect(log).toBe('b');
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
it('should repeat watch cycle while model changes are identified', function() {
|
it('should repeat watch cycle while model changes are identified', function() {
|
||||||
var log = '';
|
var log = '';
|
||||||
root.$watch('c', function(self, v){self.d = v; log+='c'; });
|
root.$watch('c', function(self, v){self.d = v; log+='c'; });
|
||||||
|
|
@ -498,7 +522,7 @@ describe('Scope', function() {
|
||||||
|
|
||||||
describe('$broadcast', function() {
|
describe('$broadcast', function() {
|
||||||
describe('event propagation', function() {
|
describe('event propagation', function() {
|
||||||
var log, child1, child2, child3, grandChild11, grandChild21, grandChild22,
|
var log, child1, child2, child3, grandChild11, grandChild21, grandChild22, grandChild23,
|
||||||
greatGrandChild211;
|
greatGrandChild211;
|
||||||
|
|
||||||
function logger(event) {
|
function logger(event) {
|
||||||
|
|
@ -513,6 +537,7 @@ describe('Scope', function() {
|
||||||
grandChild11 = child1.$new();
|
grandChild11 = child1.$new();
|
||||||
grandChild21 = child2.$new();
|
grandChild21 = child2.$new();
|
||||||
grandChild22 = child2.$new();
|
grandChild22 = child2.$new();
|
||||||
|
grandChild23 = child2.$new();
|
||||||
greatGrandChild211 = grandChild21.$new();
|
greatGrandChild211 = grandChild21.$new();
|
||||||
|
|
||||||
root.id = 0;
|
root.id = 0;
|
||||||
|
|
@ -522,6 +547,7 @@ describe('Scope', function() {
|
||||||
grandChild11.id = 11;
|
grandChild11.id = 11;
|
||||||
grandChild21.id = 21;
|
grandChild21.id = 21;
|
||||||
grandChild22.id = 22;
|
grandChild22.id = 22;
|
||||||
|
grandChild23.id = 23;
|
||||||
greatGrandChild211.id = 211;
|
greatGrandChild211.id = 211;
|
||||||
|
|
||||||
root.$on('myEvent', logger);
|
root.$on('myEvent', logger);
|
||||||
|
|
@ -531,13 +557,14 @@ describe('Scope', function() {
|
||||||
grandChild11.$on('myEvent', logger);
|
grandChild11.$on('myEvent', logger);
|
||||||
grandChild21.$on('myEvent', logger);
|
grandChild21.$on('myEvent', logger);
|
||||||
grandChild22.$on('myEvent', logger);
|
grandChild22.$on('myEvent', logger);
|
||||||
|
grandChild23.$on('myEvent', logger);
|
||||||
greatGrandChild211.$on('myEvent', logger);
|
greatGrandChild211.$on('myEvent', logger);
|
||||||
|
|
||||||
// R
|
// R
|
||||||
// / | \
|
// / | \
|
||||||
// 1 2 3
|
// 1 2 3
|
||||||
// / / \
|
// / / | \
|
||||||
// 11 21 22
|
// 11 21 22 23
|
||||||
// |
|
// |
|
||||||
// 211
|
// 211
|
||||||
});
|
});
|
||||||
|
|
@ -545,22 +572,28 @@ describe('Scope', function() {
|
||||||
|
|
||||||
it('should broadcast an event from the root scope', function() {
|
it('should broadcast an event from the root scope', function() {
|
||||||
root.$broadcast('myEvent');
|
root.$broadcast('myEvent');
|
||||||
expect(log).toBe('0>1>11>2>21>211>22>3>');
|
expect(log).toBe('0>1>11>2>21>211>22>23>3>');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
it('should broadcast an event from a child scope', function() {
|
it('should broadcast an event from a child scope', function() {
|
||||||
child2.$broadcast('myEvent');
|
child2.$broadcast('myEvent');
|
||||||
expect(log).toBe('2>21>211>22>');
|
expect(log).toBe('2>21>211>22>23>');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
it('should broadcast an event from a leaf scope', function() {
|
it('should broadcast an event from a leaf scope with a sibling', function() {
|
||||||
grandChild22.$broadcast('myEvent');
|
grandChild22.$broadcast('myEvent');
|
||||||
expect(log).toBe('22>');
|
expect(log).toBe('22>');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('should broadcast an event from a leaf scope without a sibling', function() {
|
||||||
|
grandChild23.$broadcast('myEvent');
|
||||||
|
expect(log).toBe('23>');
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
it('should not not fire any listeners for other events', function() {
|
it('should not not fire any listeners for other events', function() {
|
||||||
root.$broadcast('fooEvent');
|
root.$broadcast('fooEvent');
|
||||||
expect(log).toBe('');
|
expect(log).toBe('');
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue