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

Commit

Permalink
feat($q): added support to promise notification
Browse files Browse the repository at this point in the history
It is now possible to notify a promise through deferred.notify() method.
Notifications are useful to provide a way to send progress information
to promise holders.
  • Loading branch information
caiotoon authored and chirayuk committed Jul 15, 2013
1 parent d884eb8 commit 2a5c355
Show file tree
Hide file tree
Showing 2 changed files with 345 additions and 20 deletions.
48 changes: 42 additions & 6 deletions src/ng/q.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ function qFactory(nextTick, exceptionHandler) {
var callback;
for (var i = 0, ii = callbacks.length; i < ii; i++) {
callback = callbacks[i];
value.then(callback[0], callback[1]);
value.then(callback[0], callback[1], callback[2]);
}
});
}
Expand All @@ -212,8 +212,25 @@ function qFactory(nextTick, exceptionHandler) {
},


notify: function(progress) {
if (pending) {
var callbacks = pending;

if (pending.length) {
nextTick(function() {
var callback;
for (var i = 0, ii = callbacks.length; i < ii; i++) {
callback = callbacks[i];
callback[2](progress);
}
});
}
}
},


promise: {
then: function(callback, errback) {
then: function(callback, errback, progressback) {
var result = defer();

var wrappedCallback = function(value) {
Expand All @@ -234,10 +251,18 @@ function qFactory(nextTick, exceptionHandler) {
}
};

var wrappedProgressback = function(progress) {
try {
result.notify((progressback || defaultCallback)(progress));
} catch(e) {
exceptionHandler(e);
}
};

if (pending) {
pending.push([wrappedCallback, wrappedErrback]);
pending.push([wrappedCallback, wrappedErrback, wrappedProgressback]);
} else {
value.then(wrappedCallback, wrappedErrback);
value.then(wrappedCallback, wrappedErrback, wrappedProgressback);
}

return result.promise;
Expand Down Expand Up @@ -359,7 +384,7 @@ function qFactory(nextTick, exceptionHandler) {
* @param {*} value Value or a promise
* @returns {Promise} Returns a promise of the passed value or promise
*/
var when = function(value, callback, errback) {
var when = function(value, callback, errback, progressback) {
var result = defer(),
done;

Expand All @@ -381,15 +406,26 @@ function qFactory(nextTick, exceptionHandler) {
}
};

var wrappedProgressback = function(progress) {
try {
return (progressback || defaultCallback)(progress);
} catch (e) {
exceptionHandler(e);
}
};

nextTick(function() {
ref(value).then(function(value) {
if (done) return;
done = true;
result.resolve(ref(value).then(wrappedCallback, wrappedErrback));
result.resolve(ref(value).then(wrappedCallback, wrappedErrback, wrappedProgressback));
}, function(reason) {
if (done) return;
done = true;
result.resolve(wrappedErrback(reason));
}, function(progress) {
if (done) return;
result.notify(wrappedProgressback(progress));
});
});

Expand Down
Loading

0 comments on commit 2a5c355

Please sign in to comment.