Skip to content

Commit

Permalink
Move action-binding code to a bindAction function (WordPress#51161)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsnajdr authored and sethrubenstein committed Jul 13, 2023
1 parent 4ee1f6c commit 3fd025c
Showing 1 changed file with 14 additions and 29 deletions.
43 changes: 14 additions & 29 deletions packages/data/src/redux-store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,21 +175,24 @@ export default function createReduxStore( key, options ) {
lock( store, privateRegistrationFunctions );
const resolversCache = createResolversCache();

const actions = mapActions(
{
...metadataActions,
...options.actions,
},
store
);
function bindAction( action ) {
return ( ...args ) =>
Promise.resolve( store.dispatch( action( ...args ) ) );
}

const actions = {
...mapValues( metadataActions, bindAction ),
...mapValues( options.actions, bindAction ),
};

lock(
actions,
new Proxy( privateActions, {
get: ( target, prop ) => {
return (
mapActions( privateActions, store )[ prop ] ||
actions[ prop ]
);
const privateAction = privateActions[ prop ];
return privateAction
? bindAction( privateAction )
: actions[ prop ];
},
} )
);
Expand Down Expand Up @@ -378,24 +381,6 @@ function mapSelectors( selectors, store ) {
return mapValues( selectors, createStateSelector );
}

/**
* Maps actions to dispatch from a given store.
*
* @param {Object} actions Actions to register.
* @param {Object} store The redux store to which the actions should be mapped.
*
* @return {Object} Actions mapped to the redux store provided.
*/
function mapActions( actions, store ) {
const createBoundAction =
( action ) =>
( ...args ) => {
return Promise.resolve( store.dispatch( action( ...args ) ) );
};

return mapValues( actions, createBoundAction );
}

/**
* Maps selectors to functions that return a resolution promise for them
*
Expand Down

0 comments on commit 3fd025c

Please sign in to comment.