From cc1d1f49134cdb5c95a04e8e395a6b3ae0c70eaa Mon Sep 17 00:00:00 2001 From: Justin Beckwith Date: Thu, 15 Nov 2018 13:30:19 -0800 Subject: [PATCH] refactor: drop usage of _.flatMap --- .../grpc-native-core/src/client_interceptors.js | 4 ++-- packages/grpc-native-core/src/common.js | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/grpc-native-core/src/client_interceptors.js b/packages/grpc-native-core/src/client_interceptors.js index 5ad6f254..992cb480 100644 --- a/packages/grpc-native-core/src/client_interceptors.js +++ b/packages/grpc-native-core/src/client_interceptors.js @@ -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); diff --git a/packages/grpc-native-core/src/common.js b/packages/grpc-native-core/src/common.js index 187dd3eb..834c5f95 100644 --- a/packages/grpc-native-core/src/common.js +++ b/packages/grpc-native-core/src/common.js @@ -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} Array of property names