Commit 34a4233b authored by Tristan Cavelier's avatar Tristan Cavelier

add notifier to `all` and `any`

parent 361f17d8
...@@ -18,7 +18,7 @@ function promiseAtLeast(expected_count, promises) { ...@@ -18,7 +18,7 @@ function promiseAtLeast(expected_count, promises) {
} }
} }
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject, notify) {
var results = [], remaining = promises.length, var results = [], remaining = promises.length,
promise, remaining_count = promises.length - expected_count; promise, remaining_count = promises.length - expected_count;
...@@ -48,11 +48,17 @@ function promiseAtLeast(expected_count, promises) { ...@@ -48,11 +48,17 @@ function promiseAtLeast(expected_count, promises) {
} }
} }
function notifier(index) {
return function(value) {
notify({"index": index, "value": value});
};
}
for (var i = 0; i < promises.length; i++) { for (var i = 0; i < promises.length; i++) {
promise = promises[i]; promise = promises[i];
if (promise && typeof promise.then === 'function') { if (promise && typeof promise.then === 'function') {
promise.then(resolver(i), reject); promise.then(resolver(i), reject, notifier(i));
} else { } else {
resolveAll(i, promise); resolveAll(i, promise);
} }
......
...@@ -781,6 +781,44 @@ describe("RSVP extensions", function() { ...@@ -781,6 +781,44 @@ describe("RSVP extensions", function() {
done(); done();
}, 20); }, 20);
}); });
specify('notifies promises progress', function(done) {
var firstResolver, secondResolver, count = 0, expected = {};
var first = new RSVP.Promise(function(resolve, reject, notify) {
firstResolver = { resolve: resolve, reject: reject, notify: notify };
});
var second = new RSVP.Promise(function(resolve, reject, notify) {
secondResolver = { resolve: resolve, reject: reject, notify: notify };
});
expected.index = [0, 1, 1];
expected.value = [1, 2, 3];
setTimeout(function() {
firstResolver.notify(1);
secondResolver.notify(2);
}, 0);
setTimeout(function() {
secondResolver.notify(3);
}, 30);
var all_promise = RSVP.all([first, second]).
then(null, null, function (value) {
count += 1;
assert.equal(value.index, expected.index.shift());
assert.equal(value.value, expected.value.shift());
});
all_promise.cancel();
setTimeout(function() {
assert.equal(count, 3);
done();
}, 50);
});
}); });
describe("RSVP.any", function() { describe("RSVP.any", function() {
...@@ -953,6 +991,44 @@ describe("RSVP extensions", function() { ...@@ -953,6 +991,44 @@ describe("RSVP extensions", function() {
done(); done();
}, 20); }, 20);
}); });
specify('notifies promises progress', function(done) {
var firstResolver, secondResolver, count = 0, expected = {};
var first = new RSVP.Promise(function(resolve, reject, notify) {
firstResolver = { resolve: resolve, reject: reject, notify: notify };
});
var second = new RSVP.Promise(function(resolve, reject, notify) {
secondResolver = { resolve: resolve, reject: reject, notify: notify };
});
expected.index = [0, 1, 1];
expected.value = [1, 2, 3];
setTimeout(function() {
firstResolver.notify(1);
secondResolver.notify(2);
}, 0);
setTimeout(function() {
secondResolver.notify(3);
}, 30);
var all_promise = RSVP.any([first, second]).
then(null, null, function (value) {
count += 1;
assert.equal(value.index, expected.index.shift());
assert.equal(value.value, expected.value.shift());
});
all_promise.cancel();
setTimeout(function() {
assert.equal(count, 3);
done();
}, 50);
});
}); });
describe("RSVP.reject", function(){ describe("RSVP.reject", function(){
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment