Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move action-binding code to a bindAction function #51161

Merged
merged 1 commit into from
Jun 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -176,21 +176,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 ) ) );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it not turn synchronous results into promises? Or does dispatch always return a promise?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, dispatch always returns a promise, for historical reasons. I would love to get rid of that, because it doesn't make much sense -- not all actions are async. But that would break code like dispatch( foo ).bar().then( ... ).

}

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 @@ -379,24 +382,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