refactor: drop usage of _.flatMap

This commit is contained in:
Justin Beckwith 2018-11-15 13:30:19 -08:00
parent c9d4fbec0a
commit cc1d1f4913
2 changed files with 18 additions and 3 deletions

View File

@ -716,7 +716,7 @@ function _getStreamReadCallback(emitter, call, get_listener, deserialize) {
* @return {boolean}
*/
function _areBatchRequirementsMet(batch_ops, completed_ops) {
var dependencies = _.flatMap(batch_ops, function(op) {
var dependencies = common.flatMap(batch_ops, function(op) {
return OP_DEPENDENCIES[op] || [];
});
for (var i = 0; i < dependencies.length; i++) {
@ -752,7 +752,7 @@ function _startBatchIfReady(call, batch, batch_state, callback) {
// dependencies are met as a result.
call.startBatch(batch, callback);
completed_ops = _.union(completed_ops, batch_ops);
deferred_batches = _.flatMap(deferred_batches, function(deferred_batch) {
deferred_batches = common.flatMap(deferred_batches, function(deferred_batch) {
var deferred_batch_ops = Object.keys(deferred_batch).map(Number);
if (_areBatchRequirementsMet(deferred_batch_ops, completed_ops)) {
call.startBatch(deferred_batch.batch, deferred_batch.callback);

View File

@ -112,11 +112,26 @@ exports.getMethodType = function(method_definition) {
}
};
/**
* Iterate over a collection of items, and run the given handler.
* Return the results as a flattened array of values.
*
* @private
*
* @param {Array} collection Array of items to process
* @param {Function} handler The function to call on each element in the array
* @return {Array} A flattened array of results.
*/
exports.flatMap = function(collection, handler) {
const mapped = collection.map(handler);
return mapped.reduce((acc, curr) => acc.concat(curr), []);
}
/**
* Given an array of property names and an array of values,
* combine the two into an object map.
* Equivalent to _.zipObject.
*
*
* @private
*
* @param props {Array<String>} Array of property names