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

perf($q): reduce closures #13293

Closed
wants to merge 2 commits into from
Closed
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
44 changes: 22 additions & 22 deletions src/ng/q.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,18 +243,6 @@ function $$QProvider() {
*/
function qFactory(nextTick, exceptionHandler) {
var $qMinErr = minErr('$q', TypeError);
function callOnce(self, resolveFn, rejectFn) {
var called = false;
function wrap(fn) {
return function(value) {
if (called) return;
called = true;
fn.call(self, value);
};
}

return [wrap(resolveFn), wrap(rejectFn)];
}

/**
* @ngdoc method
Expand All @@ -267,7 +255,12 @@ function qFactory(nextTick, exceptionHandler) {
* @returns {Deferred} Returns a new instance of deferred.
*/
var defer = function() {
return new Deferred();
var d = new Deferred();
//Necessary to support unbound execution :/
d.resolve = simpleBind(d, d.resolve);
d.reject = simpleBind(d, d.reject);
d.notify = simpleBind(d, d.notify);
return d;
};

function Promise() {
Expand Down Expand Up @@ -340,10 +333,6 @@ function qFactory(nextTick, exceptionHandler) {

function Deferred() {
this.promise = new Promise();
//Necessary to support unbound execution :/
this.resolve = simpleBind(this, this.resolve);
this.reject = simpleBind(this, this.reject);
this.notify = simpleBind(this, this.notify);
}

extend(Deferred.prototype, {
Expand All @@ -361,23 +350,34 @@ function qFactory(nextTick, exceptionHandler) {
},

$$resolve: function(val) {
var then, fns;

fns = callOnce(this, this.$$resolve, this.$$reject);
var then;
var that = this;
var done = false;
try {
if ((isObject(val) || isFunction(val))) then = val && val.then;
if (isFunction(then)) {
this.promise.$$state.status = -1;
then.call(val, fns[0], fns[1], this.notify);
then.call(val, resolvePromise, rejectPromise, simpleBind(this, this.notify));
} else {
this.promise.$$state.value = val;
this.promise.$$state.status = 1;
scheduleProcessQueue(this.promise.$$state);
}
} catch (e) {
fns[1](e);
rejectPromise(e);
exceptionHandler(e);
}

function resolvePromise(val) {
if (done) return;
done = true;
that.$$resolve(val);
}
function rejectPromise(val) {
if (done) return;
done = true;
that.$$reject(val);
}
},

reject: function(reason) {
Expand Down