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

feat($q): added support to promise notification #2223

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
62 changes: 55 additions & 7 deletions src/ng/q.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,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 @@ -207,8 +207,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 @@ -229,10 +246,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 @@ -321,7 +346,7 @@ function qFactory(nextTick, exceptionHandler) {
* the promises is resolved with a rejection, this resulting promise will be resolved with the
* same rejection.
*/
var when = function(value, callback, errback) {
var when = function(value, callback, errback, progressback) {
var result = defer(),
done;

Expand All @@ -343,15 +368,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 Expand Up @@ -386,7 +422,9 @@ function qFactory(nextTick, exceptionHandler) {
function all(promises) {
var deferred = defer(),
counter = 0,
results = isArray(promises) ? [] : {};
results = isArray(promises) ? [] : {},
notifications = isArray(promises) ? [] : {},
notifying;

forEach(promises, function(promise, key) {
counter++;
Expand All @@ -397,6 +435,16 @@ function qFactory(nextTick, exceptionHandler) {
}, function(reason) {
if (results.hasOwnProperty(key)) return;
deferred.reject(reason);
}, function(progress) {
notifications[key] = progress;
if (!notifying) {
notifying = true;
nextTick(function() {
deferred.notify(notifications);
notifications = isArray(promises) ? [] : {};
notifying = false;
});
}
});
});

Expand Down
Loading