Commit 8a24804f authored by Yehuda Katz's avatar Yehuda Katz

Add RSVP.hash

parent 257996c1
define( define(
["rsvp/events","rsvp/promise","rsvp/node","rsvp/all","rsvp/defer","rsvp/config","exports"], ["rsvp/events","rsvp/promise","rsvp/node","rsvp/all","rsvp/hash","rsvp/defer","rsvp/config","exports"],
function(__dependency1__, __dependency2__, __dependency3__, __dependency4__, __dependency5__, __dependency6__, __exports__) { function(__dependency1__, __dependency2__, __dependency3__, __dependency4__, __dependency5__, __dependency6__, __dependency7__, __exports__) {
"use strict"; "use strict";
var EventTarget = __dependency1__.EventTarget; var EventTarget = __dependency1__.EventTarget;
var Promise = __dependency2__.Promise; var Promise = __dependency2__.Promise;
var denodeify = __dependency3__.denodeify; var denodeify = __dependency3__.denodeify;
var all = __dependency4__.all; var all = __dependency4__.all;
var defer = __dependency5__.defer; var hash = __dependency5__.hash;
var config = __dependency6__.config; var defer = __dependency6__.defer;
var config = __dependency7__.config;
function configure(name, value) { function configure(name, value) {
config[name] = value; config[name] = value;
...@@ -16,6 +17,7 @@ define( ...@@ -16,6 +17,7 @@ define(
__exports__.Promise = Promise; __exports__.Promise = Promise;
__exports__.EventTarget = EventTarget; __exports__.EventTarget = EventTarget;
__exports__.all = all; __exports__.all = all;
__exports__.hash = hash;
__exports__.defer = defer; __exports__.defer = defer;
__exports__.denodeify = denodeify; __exports__.denodeify = denodeify;
__exports__.configure = configure; __exports__.configure = configure;
......
...@@ -32,23 +32,16 @@ var define, requireModule; ...@@ -32,23 +32,16 @@ var define, requireModule;
})(); })();
define("rsvp/all", define("rsvp/all",
["rsvp/promise","exports"], ["rsvp/defer","exports"],
function(__dependency1__, __exports__) { function(__dependency1__, __exports__) {
"use strict"; "use strict";
var Promise = __dependency1__.Promise; var defer = __dependency1__.defer;
function all(promises) { function all(promises) {
var i, results = [], resolve, reject; var results = [], deferred = defer(), remaining = promises.length;
var allPromise = new Promise(function(allResolver, allRejecter) {
resolve = allResolver;
reject = allRejecter;
});
var remaining = promises.length;
if (remaining === 0) { if (remaining === 0) {
resolve([]); deferred.resolve([]);
} }
var resolver = function(index) { var resolver = function(index) {
...@@ -60,22 +53,22 @@ define("rsvp/all", ...@@ -60,22 +53,22 @@ define("rsvp/all",
var resolveAll = function(index, value) { var resolveAll = function(index, value) {
results[index] = value; results[index] = value;
if (--remaining === 0) { if (--remaining === 0) {
resolve(results); deferred.resolve(results);
} }
}; };
var rejectAll = function(error) { var rejectAll = function(error) {
reject(error); deferred.reject(error);
}; };
for (i = 0; i < promises.length; i++) { for (var i = 0; i < promises.length; i++) {
if (promises[i] && typeof promises[i].then === 'function') { if (promises[i] && typeof promises[i].then === 'function') {
promises[i].then(resolver(i), rejectAll); promises[i].then(resolver(i), rejectAll);
} else { } else {
resolveAll(i, promises[i]); resolveAll(i, promises[i]);
} }
} }
return allPromise; return deferred.promise;
} }
__exports__.all = all; __exports__.all = all;
...@@ -270,6 +263,60 @@ define("rsvp/events", ...@@ -270,6 +263,60 @@ define("rsvp/events",
__exports__.EventTarget = EventTarget; __exports__.EventTarget = EventTarget;
}); });
define("rsvp/hash",
["rsvp/defer","exports"],
function(__dependency1__, __exports__) {
"use strict";
var defer = __dependency1__.defer;
function size(object) {
var size = 0;
for (var prop in object) {
size++;
}
return size;
}
function hash(promises) {
var results = {}, deferred = defer(), remaining = size(promises);
if (remaining === 0) {
deferred.resolve({});
}
var resolver = function(prop) {
return function(value) {
resolveAll(prop, value);
};
};
var resolveAll = function(prop, value) {
results[prop] = value;
if (--remaining === 0) {
deferred.resolve(results);
}
};
var rejectAll = function(error) {
deferred.reject(error);
};
for (var prop in promises) {
if (promises[prop] && typeof promises[prop].then === 'function') {
promises[prop].then(resolver(prop), rejectAll);
} else {
resolveAll(prop, promises[prop]);
}
}
return deferred.promise;
}
__exports__.hash = hash;
});
define("rsvp/node", define("rsvp/node",
["rsvp/promise","rsvp/all","exports"], ["rsvp/promise","rsvp/all","exports"],
function(__dependency1__, __dependency2__, __exports__) { function(__dependency1__, __dependency2__, __exports__) {
...@@ -456,15 +503,16 @@ define("rsvp/promise", ...@@ -456,15 +503,16 @@ define("rsvp/promise",
}); });
define("rsvp", define("rsvp",
["rsvp/events","rsvp/promise","rsvp/node","rsvp/all","rsvp/defer","rsvp/config","exports"], ["rsvp/events","rsvp/promise","rsvp/node","rsvp/all","rsvp/hash","rsvp/defer","rsvp/config","exports"],
function(__dependency1__, __dependency2__, __dependency3__, __dependency4__, __dependency5__, __dependency6__, __exports__) { function(__dependency1__, __dependency2__, __dependency3__, __dependency4__, __dependency5__, __dependency6__, __dependency7__, __exports__) {
"use strict"; "use strict";
var EventTarget = __dependency1__.EventTarget; var EventTarget = __dependency1__.EventTarget;
var Promise = __dependency2__.Promise; var Promise = __dependency2__.Promise;
var denodeify = __dependency3__.denodeify; var denodeify = __dependency3__.denodeify;
var all = __dependency4__.all; var all = __dependency4__.all;
var defer = __dependency5__.defer; var hash = __dependency5__.hash;
var config = __dependency6__.config; var defer = __dependency6__.defer;
var config = __dependency7__.config;
function configure(name, value) { function configure(name, value) {
config[name] = value; config[name] = value;
...@@ -473,6 +521,7 @@ define("rsvp", ...@@ -473,6 +521,7 @@ define("rsvp",
__exports__.Promise = Promise; __exports__.Promise = Promise;
__exports__.EventTarget = EventTarget; __exports__.EventTarget = EventTarget;
__exports__.all = all; __exports__.all = all;
__exports__.hash = hash;
__exports__.defer = defer; __exports__.defer = defer;
__exports__.denodeify = denodeify; __exports__.denodeify = denodeify;
__exports__.configure = configure; __exports__.configure = configure;
......
This diff is collapsed.
define( define(
["rsvp/promise","exports"], ["rsvp/defer","exports"],
function(__dependency1__, __exports__) { function(__dependency1__, __exports__) {
"use strict"; "use strict";
var Promise = __dependency1__.Promise; var defer = __dependency1__.defer;
function all(promises) { function all(promises) {
var i, results = [], resolve, reject; var results = [], deferred = defer(), remaining = promises.length;
var allPromise = new Promise(function(allResolver, allRejecter) {
resolve = allResolver;
reject = allRejecter;
});
var remaining = promises.length;
if (remaining === 0) { if (remaining === 0) {
resolve([]); deferred.resolve([]);
} }
var resolver = function(index) { var resolver = function(index) {
...@@ -27,22 +20,22 @@ define( ...@@ -27,22 +20,22 @@ define(
var resolveAll = function(index, value) { var resolveAll = function(index, value) {
results[index] = value; results[index] = value;
if (--remaining === 0) { if (--remaining === 0) {
resolve(results); deferred.resolve(results);
} }
}; };
var rejectAll = function(error) { var rejectAll = function(error) {
reject(error); deferred.reject(error);
}; };
for (i = 0; i < promises.length; i++) { for (var i = 0; i < promises.length; i++) {
if (promises[i] && typeof promises[i].then === 'function') { if (promises[i] && typeof promises[i].then === 'function') {
promises[i].then(resolver(i), rejectAll); promises[i].then(resolver(i), rejectAll);
} else { } else {
resolveAll(i, promises[i]); resolveAll(i, promises[i]);
} }
} }
return allPromise; return deferred.promise;
} }
__exports__.all = all; __exports__.all = all;
......
define(
["rsvp/defer","exports"],
function(__dependency1__, __exports__) {
"use strict";
var defer = __dependency1__.defer;
function size(object) {
var size = 0;
for (var prop in object) {
size++;
}
return size;
}
function hash(promises) {
var results = {}, deferred = defer(), remaining = size(promises);
if (remaining === 0) {
deferred.resolve({});
}
var resolver = function(prop) {
return function(value) {
resolveAll(prop, value);
};
};
var resolveAll = function(prop, value) {
results[prop] = value;
if (--remaining === 0) {
deferred.resolve(results);
}
};
var rejectAll = function(error) {
deferred.reject(error);
};
for (var prop in promises) {
if (promises[prop] && typeof promises[prop].then === 'function') {
promises[prop].then(resolver(prop), rejectAll);
} else {
resolveAll(prop, promises[prop]);
}
}
return deferred.promise;
}
__exports__.hash = hash;
});
...@@ -2,6 +2,7 @@ import { EventTarget } from "rsvp/events"; ...@@ -2,6 +2,7 @@ import { EventTarget } from "rsvp/events";
import { Promise } from "rsvp/promise"; import { Promise } from "rsvp/promise";
import { denodeify } from "rsvp/node"; import { denodeify } from "rsvp/node";
import { all } from "rsvp/all"; import { all } from "rsvp/all";
import { hash } from "rsvp/hash";
import { defer } from "rsvp/defer"; import { defer } from "rsvp/defer";
import { config } from "rsvp/config"; import { config } from "rsvp/config";
...@@ -9,4 +10,4 @@ function configure(name, value) { ...@@ -9,4 +10,4 @@ function configure(name, value) {
config[name] = value; config[name] = value;
} }
export { Promise, EventTarget, all, defer, denodeify, configure }; export { Promise, EventTarget, all, hash, defer, denodeify, configure };
\ No newline at end of file \ No newline at end of file
import { Promise } from "rsvp/promise"; import { defer } from "rsvp/defer";
function all(promises) { function all(promises) {
var i, results = [], resolve, reject; var results = [], deferred = defer(), remaining = promises.length;
var allPromise = new Promise(function(allResolver, allRejecter) {
resolve = allResolver;
reject = allRejecter;
});
var remaining = promises.length;
if (remaining === 0) { if (remaining === 0) {
resolve([]); deferred.resolve([]);
} }
var resolver = function(index) { var resolver = function(index) {
...@@ -23,22 +16,22 @@ function all(promises) { ...@@ -23,22 +16,22 @@ function all(promises) {
var resolveAll = function(index, value) { var resolveAll = function(index, value) {
results[index] = value; results[index] = value;
if (--remaining === 0) { if (--remaining === 0) {
resolve(results); deferred.resolve(results);
} }
}; };
var rejectAll = function(error) { var rejectAll = function(error) {
reject(error); deferred.reject(error);
}; };
for (i = 0; i < promises.length; i++) { for (var i = 0; i < promises.length; i++) {
if (promises[i] && typeof promises[i].then === 'function') { if (promises[i] && typeof promises[i].then === 'function') {
promises[i].then(resolver(i), rejectAll); promises[i].then(resolver(i), rejectAll);
} else { } else {
resolveAll(i, promises[i]); resolveAll(i, promises[i]);
} }
} }
return allPromise; return deferred.promise;
} }
export { all }; export { all };
\ No newline at end of file
import { defer } from "rsvp/defer";
function size(object) {
var size = 0;
for (var prop in object) {
size++;
}
return size;
}
function hash(promises) {
var results = {}, deferred = defer(), remaining = size(promises);
if (remaining === 0) {
deferred.resolve({});
}
var resolver = function(prop) {
return function(value) {
resolveAll(prop, value);
};
};
var resolveAll = function(prop, value) {
results[prop] = value;
if (--remaining === 0) {
deferred.resolve(results);
}
};
var rejectAll = function(error) {
deferred.reject(error);
};
for (var prop in promises) {
if (promises[prop] && typeof promises[prop].then === 'function') {
promises[prop].then(resolver(prop), rejectAll);
} else {
resolveAll(prop, promises[prop]);
}
}
return deferred.promise;
}
export { hash };
\ No newline at end of file
...@@ -322,6 +322,92 @@ describe("RSVP extensions", function() { ...@@ -322,6 +322,92 @@ describe("RSVP extensions", function() {
}); });
}); });
describe("RSVP.hash", function() {
it('should exist', function() {
assert(RSVP.hash);
});
specify('fulfilled only after all of the promise values are fulfilled', function(done) {
var firstResolved, secondResolved, firstResolver, secondResolver;
var first = new RSVP.Promise(function(resolve) {
firstResolver = resolve;
});
first.then(function() {
firstResolved = true;
});
var second = new RSVP.Promise(function(resolve) {
secondResolver = resolve;
});
second.then(function() {
secondResolved = true;
});
setTimeout(function() {
firstResolver(true);
}, 0);
setTimeout(function() {
secondResolver(true);
}, 0);
RSVP.hash({ first: first, second: second }).then(function(values) {
assert(values.first);
assert(values.second);
done();
});
});
specify('rejected as soon as a promise is rejected', function(done) {
var firstResolver, secondResolver;
var first = new RSVP.Promise(function(resolve, reject) {
firstResolver = { resolve: resolve, reject: reject };
});
var second = new RSVP.Promise(function(resolve, reject) {
secondResolver = { resolve: resolve, reject: reject };
});
setTimeout(function() {
firstResolver.reject({});
}, 0);
setTimeout(function() {
secondResolver.resolve(true);
}, 5000);
RSVP.hash({ first: first, second: second }).then(function() {
assert(false);
}, function() {
assert(first.isRejected);
assert(!second.isResolved);
done();
});
});
specify('resolves an empty hash passed to RSVP.all()', function(done) {
RSVP.hash({}).then(function(results) {
assert.deepEqual(results, {});
done();
});
});
specify('works with a mix of promises and thenables and non-promises', function(done) {
var promise = new RSVP.Promise(function(resolve) { resolve(1); });
var syncThenable = { then: function (onFulfilled) { onFulfilled(2); } };
var asyncThenable = { then: function (onFulfilled) { setTimeout(function() { onFulfilled(3); }, 0); } };
var nonPromise = 4;
RSVP.hash({ promise: promise, syncThenable: syncThenable, asyncThenable: asyncThenable, nonPromise: nonPromise }).then(function(results) {
assert.deepEqual(results, { promise: 1, syncThenable: 2, asyncThenable: 3, nonPromise: 4 });
done();
});
});
});
describe("RSVP.all", function() { describe("RSVP.all", function() {
it('should exist', function() { it('should exist', function() {
assert(RSVP.all); assert(RSVP.all);
......
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