From 5b629a89c6ca43e27d6a15bbade247aea1efa029 Mon Sep 17 00:00:00 2001 From: Jarda Snajdr Date: Mon, 7 Sep 2020 09:22:43 +0200 Subject: [PATCH] Data: add a comment about why we normalize resolvers to objects with fulfill method Also refactors the code to remove too smart destructuring, and removes a stale JSDoc comment. --- packages/data/src/namespace-store/index.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/data/src/namespace-store/index.js b/packages/data/src/namespace-store/index.js index 952c620acfa58..3cf0919db018b 100644 --- a/packages/data/src/namespace-store/index.js +++ b/packages/data/src/namespace-store/index.js @@ -252,13 +252,21 @@ function mapActions( actions, store ) { * @param {Object} resolvers Resolvers to register. * @param {Object} selectors The current selectors to be modified. * @param {Object} store The redux store to which the resolvers should be mapped. - * @param {Object} queue Resolvers async queue. * @param {Object} resolversCache Resolvers Cache. */ function mapResolvers( resolvers, selectors, store, resolversCache ) { + // The `resolver` can be either a function that does the resolution, or, in more advanced + // cases, an object with a `fullfill` method and other optional methods like `isFulfilled`. + // Here we normalize the `resolver` function to an object with `fulfill` method. const mappedResolvers = mapValues( resolvers, ( resolver ) => { - const { fulfill: resolverFulfill = resolver } = resolver; - return { ...resolver, fulfill: resolverFulfill }; + if ( resolver.fulfill ) { + return resolver; + } + + return { + ...resolver, // copy the enumerable properties of the resolver function + fulfill: resolver, // add the fulfill method + }; } ); const mapSelector = ( selector, selectorName ) => {