Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit beb8ab6

Browse files
committed
perf($q): reduce closures when resolving promises
- changes Deferred.$$resolve to only wrap the internal resolve/reject when wrapping another promise
1 parent aff74ec commit beb8ab6

File tree

1 file changed

+19
-24
lines changed

1 file changed

+19
-24
lines changed

src/ng/q.js

+19-24
Original file line numberDiff line numberDiff line change
@@ -243,18 +243,6 @@ function $$QProvider() {
243243
*/
244244
function qFactory(nextTick, exceptionHandler) {
245245
var $qMinErr = minErr('$q', TypeError);
246-
function callOnce(self, resolveFn, rejectFn) {
247-
var called = false;
248-
function wrap(fn) {
249-
return function(value) {
250-
if (called) return;
251-
called = true;
252-
fn.call(self, value);
253-
};
254-
}
255-
256-
return [wrap(resolveFn), wrap(rejectFn)];
257-
}
258246

259247
/**
260248
* @ngdoc method
@@ -361,21 +349,27 @@ function qFactory(nextTick, exceptionHandler) {
361349
},
362350

363351
$$resolve: function(val) {
364-
var then, fns;
352+
if (this.promise.$$state.status > 0) return;
365353

366-
fns = callOnce(this, this.$$resolve, this.$$reject);
354+
var then;
355+
if ((isObject(val) || isFunction(val)) && isFunction(then = val.then)) {
356+
this.promise.$$state.status = -1;
357+
this.$$delayResolve(val, then);
358+
} else {
359+
this.promise.$$state.value = val;
360+
this.promise.$$state.status = 1;
361+
scheduleProcessQueue(this.promise.$$state);
362+
}
363+
},
364+
365+
$$delayResolve: function(promiseLike, then) {
366+
var that = this;
367+
function resolve(val) { that.$$resolve(val); }
368+
function reject(val) { that.$$reject(val); }
367369
try {
368-
if ((isObject(val) || isFunction(val))) then = val && val.then;
369-
if (isFunction(then)) {
370-
this.promise.$$state.status = -1;
371-
then.call(val, fns[0], fns[1], this.notify);
372-
} else {
373-
this.promise.$$state.value = val;
374-
this.promise.$$state.status = 1;
375-
scheduleProcessQueue(this.promise.$$state);
376-
}
370+
then.call(promiseLike, resolve, reject, this.notify);
377371
} catch (e) {
378-
fns[1](e);
372+
reject(e);
379373
exceptionHandler(e);
380374
}
381375
},
@@ -386,6 +380,7 @@ function qFactory(nextTick, exceptionHandler) {
386380
},
387381

388382
$$reject: function(reason) {
383+
if (this.promise.$$state.status > 0) return;
389384
this.promise.$$state.value = reason;
390385
this.promise.$$state.status = 2;
391386
scheduleProcessQueue(this.promise.$$state);

0 commit comments

Comments
 (0)