diff options
author | Florian Dold <florian.dold@gmail.com> | 2016-10-10 03:43:44 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2016-10-10 03:43:44 +0200 |
commit | abd94a7f5a50f43c797a11b53549ae48fff667c3 (patch) | |
tree | ab8ed457f65cdd72e13e0571d2975729428f1551 /node_modules/when/keys.js | |
parent | a0247c6a3fd6a09a41a7e35a3441324c4dcb58be (diff) | |
download | wallet-core-abd94a7f5a50f43c797a11b53549ae48fff667c3.tar.xz |
add node_modules to address #4364
Diffstat (limited to 'node_modules/when/keys.js')
-rw-r--r-- | node_modules/when/keys.js | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/node_modules/when/keys.js b/node_modules/when/keys.js new file mode 100644 index 000000000..2a62c5d81 --- /dev/null +++ b/node_modules/when/keys.js @@ -0,0 +1,114 @@ +/** @license MIT License (c) copyright 2011-2013 original author or authors */ + +/** + * Licensed under the MIT License at: + * http://www.opensource.org/licenses/mit-license.php + * + * @author Brian Cavalier + * @author John Hann + */ +(function(define) { 'use strict'; +define(function(require) { + + var when = require('./when'); + var Promise = when.Promise; + var toPromise = when.resolve; + + return { + all: when.lift(all), + map: map, + settle: settle + }; + + /** + * Resolve all the key-value pairs in the supplied object or promise + * for an object. + * @param {Promise|object} object or promise for object whose key-value pairs + * will be resolved + * @returns {Promise} promise for an object with the fully resolved key-value pairs + */ + function all(object) { + var p = Promise._defer(); + var resolver = Promise._handler(p); + + var results = {}; + var keys = Object.keys(object); + var pending = keys.length; + + for(var i=0, k; i<keys.length; ++i) { + k = keys[i]; + Promise._handler(object[k]).fold(settleKey, k, results, resolver); + } + + if(pending === 0) { + resolver.resolve(results); + } + + return p; + + function settleKey(k, x, resolver) { + /*jshint validthis:true*/ + this[k] = x; + if(--pending === 0) { + resolver.resolve(results); + } + } + } + + /** + * Map values in the supplied object's keys + * @param {Promise|object} object or promise for object whose key-value pairs + * will be reduced + * @param {function(value:*, key:String):*} f mapping function which may + * return either a promise or a value + * @returns {Promise} promise for an object with the mapped and fully + * resolved key-value pairs + */ + function map(object, f) { + return toPromise(object).then(function(object) { + return all(Object.keys(object).reduce(function(o, k) { + o[k] = toPromise(object[k]).fold(mapWithKey, k); + return o; + }, {})); + }); + + function mapWithKey(k, x) { + return f(x, k); + } + } + + /** + * Resolve all key-value pairs in the supplied object and return a promise + * that will always fulfill with the outcome states of all input promises. + * @param {object} object whose key-value pairs will be settled + * @returns {Promise} promise for an object with the mapped and fully + * settled key-value pairs + */ + function settle(object) { + var keys = Object.keys(object); + var results = {}; + + if(keys.length === 0) { + return toPromise(results); + } + + var p = Promise._defer(); + var resolver = Promise._handler(p); + var promises = keys.map(function(k) { return object[k]; }); + + when.settle(promises).then(function(states) { + populateResults(keys, states, results, resolver); + }); + + return p; + } + + function populateResults(keys, states, results, resolver) { + for(var i=0; i<keys.length; i++) { + results[keys[i]] = states[i]; + } + resolver.resolve(results); + } + +}); +})(typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }); |