Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/pr/105'
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal committed Aug 23, 2012
2 parents d98e618 + a9a3e40 commit cbf3011
Show file tree
Hide file tree
Showing 2 changed files with 376 additions and 14 deletions.
66 changes: 52 additions & 14 deletions q.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,15 +457,18 @@ function defer() {
// forward to the resolved promise. We coerce the resolution value to a
// promise using the ref promise because it handles both fully
// resolved values and other promises gracefully.
var pending = [], value;
var pending = [], progressListeners = [], value;

var deferred = object_create(defer.prototype);
var promise = object_create(makePromise.prototype);

promise.promiseSend = function () {
promise.promiseSend = function (op, _, __, progress) {
var args = array_slice(arguments);
if (pending) {
pending.push(args);
if (op === "when" && progress) {
progressListeners.push(progress);
}
} else {
nextTick(function () {
value.promiseSend.apply(value, args);
Expand Down Expand Up @@ -495,6 +498,7 @@ function defer() {
});
}, void 0);
pending = void 0;
progressListeners = void 0;
return value;
}

Expand All @@ -505,6 +509,17 @@ function defer() {
deferred.reject = function (exception) {
return become(reject(exception));
};
deferred.notify = function () {
if (pending) {
var args = arguments;

array_reduce(progressListeners, function (undefined, progressListener) {
nextTick(function () {
progressListener.apply(void 0, args);
});
}, void 0);
}
};

return deferred;
}
Expand Down Expand Up @@ -579,7 +594,9 @@ function makePromise(descriptor, fallback, valueOf, exception) {
} catch (exception) {
result = reject(exception);
}
resolved(result);
if (resolved) {
resolved(result);
}
};

if (valueOf) {
Expand All @@ -596,8 +613,8 @@ function makePromise(descriptor, fallback, valueOf, exception) {
}

// provide thenables, CommonJS/Promises/A
makePromise.prototype.then = function (fulfilled, rejected) {
return when(this, fulfilled, rejected);
makePromise.prototype.then = function (fulfilled, rejected, progressed) {
return when(this, fulfilled, rejected, progressed);
};

// Chainable methods
Expand All @@ -613,7 +630,7 @@ array_reduce(
"all", "allResolved",
"view", "viewInfo",
"timeout", "delay",
"catch", "finally", "fail", "fin", "end"
"catch", "finally", "fail", "fin", "progress", "end"
],
function (undefined, name) {
makePromise.prototype[name] = function () {
Expand Down Expand Up @@ -759,9 +776,9 @@ function resolve(object) {
}
// assimilate thenables, CommonJS/Promises/A
if (object && typeof object.then === "function") {
var result = defer();
object.then(result.resolve, result.reject);
return result.promise;
var deferred = defer();
object.then(deferred.resolve, deferred.reject, deferred.notify);
return deferred.promise;
}
return makePromise({
"when": function () {
Expand Down Expand Up @@ -885,13 +902,14 @@ function view(object) {
* called, but not both.
* 3. that fulfilled and rejected will not be called in this turn.
*
* @param value promise or immediate reference to observe
* @param fulfilled function to be called with the fulfilled value
* @param rejected function to be called with the rejection exception
* @param value promise or immediate reference to observe
* @param fulfilled function to be called with the fulfilled value
* @param rejected function to be called with the rejection exception
* @param progressed function to be called on any progress notifications
* @return promise for the return value from the invoked callback
*/
exports.when = when;
function when(value, fulfilled, rejected) {
function when(value, fulfilled, rejected, progressed) {
var deferred = defer();
var done = false; // ensure the untrusted promise makes at most a
// single call to one of the callbacks
Expand All @@ -912,8 +930,9 @@ function when(value, fulfilled, rejected) {
}
}

var resolvedValue = resolve(value);
nextTick(function () {
resolve(value).promiseSend("when", function (value) {
resolvedValue.promiseSend("when", function (value) {
if (done) {
return;
}
Expand All @@ -930,6 +949,11 @@ function when(value, fulfilled, rejected) {
});
});

// Progress listeners need to be attached in the current tick.
if (progressed) {
resolvedValue.promiseSend("when", void 0, void 0, progressed);
}

return deferred.promise;
}

Expand Down Expand Up @@ -1325,6 +1349,20 @@ function fail(promise, rejected) {
return when(promise, void 0, rejected);
}

/**
* Attaches a listener that can respond to progress notifications from a
* promise's originating deferred. This listener receives the exact arguments
* passed to ``deferred.notify``.
* @param {Any*} promise for something
* @param {Function} callback to receive any progress notifications
* @returns the given promise, unchanged
*/
exports.progress = progress;
function progress(promise, progressed) {
when(promise, void 0, void 0, progressed);
return promise;
}

/**
* Provides an opportunity to observe the rejection of a promise,
* regardless of whether the promise is fulfilled or rejected. Forwards
Expand Down
Loading

0 comments on commit cbf3011

Please sign in to comment.