Create a "combineStores" utility function to combine stores
See original GitHub issueFeature Description
Currently, we create “combined” stores like this: https://github.com/google/site-kit-wp/blob/507e81f4af2b52e77db6a71710777d5ea086302b/assets/js/googlesitekit/modules/create-module-store.js#L59
But that’s a bit verbose and makes combining multiple stores together a bit annoying. We should create a utility that combines stores—even ones that lack certain keys.
Do not alter or remove anything below. The following sections will be managed by moderators only.
Acceptance criteria
- Create a
combineStores( …args )function that accepts a list of stores and combines their actions, reducers, etc. for multiple stores and returns a single store. It should not require that stores have all store properties (eg.initialState,actions, etc.) and should use sane defaults for any missing values. - It should still fail if duplicate keys are found, similar to the
collectActions, etc. functions.
Implementation Brief
- Add a
combineStores( …args )function that accepts full stores as arguments, each with the object shape:
{
INITIAL_STATE,
actions,
controls,
reducer,
resolvers,
selectors,
}
Note that every value should be optional, because it’s possible to have a store that doesn’t have controls, or even selectors, etc. We can provide sane defaults for other things like reducer or initialState if none are provided. (Though equally, I’d be fine if initialState was also required.)
- This should act like an amalgamation of this type of code: https://github.com/google/site-kit-wp/blob/c302d8cb77374e394ba4711a93b3d7ca33e43802/assets/js/googlesitekit/datastore/site/index.js#L38-L85, but allow for developers to simply submit entire stores. So the above code would change to:
const store = Data.combineStores(
{
actions: Data.commonActions,
controls: Data.commonControls,
},
connection,
info,
reset,
notifications,
);
Data.registerStore( STORE_NAME, store );
QA Brief
Changelog entry
- Add
googlesitekit.data.combineStoresutility function to combine multiple datastore objects.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
combineReducers(reducers) - Redux
This function helps you organize your reducers to manage their own slices of state, similar to how you would have different Flux Stores...
Read more >combineStore, React native create store, createStore ...
It turns out that Redux lets us combine multiple reducers into one that can be passed into createStore by using a helper function...
Read more >tapayne88/combine-selectors-redux - GitHub
Utility function for combining selectors when working with redux. Much like combineReducers it helps for defining selector functions within the reducers.
Read more >anew - npm
The createStore utility allows you to create a Redux store without the ... The combineStores utility is very similar to the combineReducer ......
Read more >Utility function that uses the store - Stack Overflow
I am working with react and I need to create a utility function that uses a parameter from the redux store.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

IB ✅
Let’s have all parts of the object optional including
INITIAL_STATEand use defaults (INITIAL_STATEcould just be an empty object).@jqlee85 Yeah exactly, sorry for being a bit unprecise in my comment above.
combineStoresshould not use any initialization logic indirectly either 👍 The actual problem as mentioned lies inaddInitializeActionbeing called with an empty object forcommonStore.