perf(delegateService): $getByHandle tiny improvement

This commit is contained in:
Andy Joslin
2014-03-28 08:05:14 -06:00
parent dd64ac557a
commit e9933e9920
2 changed files with 42 additions and 29 deletions

View File

@@ -5,8 +5,6 @@ function delegateService(methodNames) {
var instances = this._instances = [];
this._registerInstance = function(instance, handle) {
handle || (handle = ionic.Utils.nextUid());
instance.$$delegateHandle = handle;
instances.push(instance);
@@ -47,19 +45,48 @@ function delegateService(methodNames) {
methodNames.forEach(function(methodName) {
InstanceForHandle.prototype[methodName] = function() {
var handle = this.handle;
var instancesToUse = instances.filter(function(instance) {
return instance.$$delegateHandle === handle;
var args = arguments;
var matchingInstancesFound = 0;
var finalResult;
var result;
//This logic is repeated below; we could factor some of it out to a function
//but don't because it lets this method be more performant (one loop versus 2)
instances.forEach(function(instance) {
if (instance.$$delegateHandle === handle) {
matchingInstancesFound++;
result = instance[methodName].apply(instance, args);
//Only return the value from the first call
if (matchingInstancesFound === 1) {
finalResult = result;
}
}
});
if (!instancesToUse.length) {
if (!matchingInstancesFound) {
return $log.warn(
'Delegate for handle "'+this.handle+'" could not find a',
'corresponding element with delegate-handle="'+this.handle+'"!',
methodName, 'was not called!');
}
return callMethod(instancesToUse, methodName, arguments);
return finalResult;
};
delegate[methodName] = function() {
return callMethod(instances, methodName, arguments);
var args = arguments;
var finalResult;
var result;
//This logic is repeated above
instances.forEach(function(instance, index) {
result = instance[methodName].apply(instance, args);
//Only return the value from the first call
if (index === 0) {
finalResult = result;
}
});
return finalResult;
};
function callMethod(instancesToUse, methodName, args) {

View File

@@ -23,15 +23,11 @@ describe('DelegateFactory', function() {
expect(delegate._instances.length).toBe(0);
});
it('should allow reg & dereg of instance, and make its own handle', function() {
spyOn(ionic.Utils, 'nextUid').andCallFake(function() {
return 'uid';
});
it('should allow reg & dereg of instance, without handle', function() {
var delegate = setup();
var instance = {};
var deregister = delegate._registerInstance(instance);
expect(ionic.Utils.nextUid).toHaveBeenCalled();
expect(instance.$$delegateHandle).toBe('uid');
var deregister = delegate._registerInstance(instance, null);
expect(instance.$$delegateHandle).toBe(null);
expect(delegate._instances[0]).toBe(instance);
deregister();
expect(delegate._instances.length).toBe(0);
@@ -78,14 +74,10 @@ describe('DelegateFactory', function() {
it('should return the first return value from multiple instances', function() {
var delegate = setup(['fn']);
var instance1 = {
fn: jasmine.createSpy('fn').andCallFake(function() {
return 'ret1';
})
fn: jasmine.createSpy('fn').andReturn('ret1')
};
var instance2 = {
fn: jasmine.createSpy('fn').andCallFake(function() {
return 'ret2';
})
fn: jasmine.createSpy('fn').andReturn('ret2')
};
var deregister = delegate._registerInstance(instance1);
delegate._registerInstance(instance2);
@@ -105,19 +97,13 @@ describe('DelegateFactory', function() {
beforeEach(function() {
delegate = setup(['a']);
instance1 = {
a: jasmine.createSpy('a1').andCallFake(function() {
return 'a1';
}),
a: jasmine.createSpy('a1').andReturn('a1')
};
instance2 = {
a: jasmine.createSpy('a2').andCallFake(function() {
return 'a2';
}),
a: jasmine.createSpy('a2').andReturn('a2')
};
instance3 = {
a: jasmine.createSpy('a3').andCallFake(function() {
return 'a3';
})
a: jasmine.createSpy('a3').andReturn('a3')
};
});
it('should return an InstanceWithHandle object with fields', function() {