Skip to content

Commit ef144e0

Browse files
committed
$watchSet changes. TODO squash
1 parent a3143f3 commit ef144e0

File tree

2 files changed

+44
-71
lines changed

2 files changed

+44
-71
lines changed

src/ng/rootScope.js

+6-13
Original file line numberDiff line numberDiff line change
@@ -383,30 +383,23 @@ function $RootScopeProvider(){
383383
$watchSet: function (watchExpressions, listener) {
384384
if (watchExpressions.length === 0) return noop;
385385

386-
var oldValues = new Array(watchExpressions.length),
386+
var self = this,
387+
oldValues = new Array(watchExpressions.length),
387388
newValues = new Array(watchExpressions.length);
388389

389-
if (watchExpressions.length === 1) {
390-
// Special case size of one
391-
return this.$watch(watchExpressions[0], function (value, oldValue, scope) {
392-
newValues[0] = value;
393-
oldValues[0] = oldValue;
394-
listener.call(this, newValues, oldValues, scope);
395-
});
396-
}
397390
var deregisterFns = [],
398391
changeCount = 0;
399392

400393
forEach(watchExpressions, function (expr, i) {
401-
deregisterFns.push(this.$watch(expr, function (value, oldValue) {
394+
deregisterFns.push(self.$watch(expr, function (value, oldValue) {
402395
newValues[i] = value;
403396
oldValues[i] = oldValue;
404397
changeCount++;
405398
}));
406-
}, this);
399+
});
407400

408-
deregisterFns.push(this.$watch(function () {return changeCount;}, function (c, o, scope) {
409-
listener.call(this, newValues, oldValues, scope);
401+
deregisterFns.push(this.$watch(function () {return changeCount;}, function () {
402+
listener.call(this, newValues, oldValues, self);
410403
}));
411404

412405
return function () {

test/ng/rootScopeSpec.js

+38-58
Original file line numberDiff line numberDiff line change
@@ -734,74 +734,54 @@ describe('Scope', function() {
734734
});
735735

736736
describe('$watchSet', function() {
737-
var scope;
738-
beforeEach(inject(function($rootScope) {
739-
scope = $rootScope.$new();
740-
}));
741-
742-
it('should skip empty sets', function() {
743-
expect(scope.$watchSet([], null)()).toBeUndefined();
744-
});
745737

746-
it('should treat set of 1 as direct watch', function() {
747-
var lastValues = ['foo'];
748-
var log = '';
749-
var clean = scope.$watchSet(['a'], function(values, oldValues, s) {
750-
log += values.join(',') + ';';
751-
expect(s).toBe(scope);
752-
expect(oldValues).toEqual(lastValues);
753-
lastValues = values.slice();
738+
it('should detect a change to any expression in the array', inject(function($rootScope, log) {
739+
$rootScope.$watchSet(['a', 'b'], function(values, oldValues, scope) {
740+
expect(scope).toBe($rootScope);
741+
log(values + '-' + oldValues);
754742
});
755743

756-
scope.a = 'foo';
757-
scope.$digest();
758-
expect(log).toEqual('foo;');
759-
760-
scope.$digest();
761-
expect(log).toEqual('foo;');
762-
763-
scope.a = 'bar';
764-
scope.$digest();
765-
expect(log).toEqual('foo;bar;');
744+
$rootScope.a = 'foo';
745+
$rootScope.b = 'bar';
746+
$rootScope.$digest();
747+
expect(log).toEqual('foo,bar-foo,bar');
766748

767-
clean();
768-
scope.a = 'xxx';
769-
scope.$digest();
770-
expect(log).toEqual('foo;bar;');
771-
});
749+
log.reset();
750+
$rootScope.$digest();
751+
expect(log).toEqual('');
772752

773-
it('should detect a change to any one in a set', function() {
774-
var lastValues = ['foo', 'bar'];
775-
var log = '';
776-
var clean = scope.$watchSet(['a', 'b'], function(values, oldValues, s) {
777-
log += values.join(',') + ';';
778-
expect(s).toBe(scope);
779-
expect(oldValues).toEqual(lastValues);
780-
lastValues = values.slice();
781-
});
753+
log.reset();
754+
$rootScope.a = 'a';
755+
$rootScope.$digest();
756+
expect(log).toEqual('a,bar-foo,bar');
782757

783-
scope.a = 'foo';
784-
scope.b = 'bar';
785-
scope.$digest();
786-
expect(log).toEqual('foo,bar;');
758+
log.reset();
759+
$rootScope.a = 'A';
760+
$rootScope.b = 'B';
761+
$rootScope.$digest();
762+
expect(log).toEqual('A,B-a,bar');
763+
}));
787764

788-
scope.$digest();
789-
expect(log).toEqual('foo,bar;');
765+
it('should return a function that allows listeners to be deregistered', inject(function($rootScope) {
766+
var listener = jasmine.createSpy('watchSet listener'),
767+
listenerRemove;
790768

791-
scope.a = 'a';
792-
scope.$digest();
793-
expect(log).toEqual('foo,bar;a,bar;');
769+
listenerRemove = $rootScope.$watchSet(['a'], listener);
770+
$rootScope.$digest(); //init
771+
expect(listener).toHaveBeenCalled();
772+
expect(listenerRemove).toBeDefined();
794773

795-
scope.a = 'A';
796-
scope.b = 'B';
797-
scope.$digest();
798-
expect(log).toEqual('foo,bar;a,bar;A,B;');
774+
listener.reset();
775+
$rootScope.a = 'bar';
776+
$rootScope.$digest(); //trigger
777+
expect(listener).toHaveBeenCalledOnce();
799778

800-
clean();
801-
scope.a = 'xxx';
802-
scope.$digest();
803-
expect(log).toEqual('foo,bar;a,bar;A,B;');
804-
});
779+
listener.reset();
780+
$rootScope.a = 'baz';
781+
listenerRemove();
782+
$rootScope.$digest(); //trigger
783+
expect(listener).not.toHaveBeenCalled();
784+
}));
805785
});
806786

807787
describe('$destroy', function() {

0 commit comments

Comments
 (0)