Commit 6e941c4a authored by Robert Knight's avatar Robert Knight

Simplify creating store modules with static initial state

Allow the `initialState` argument to `createStoreModule` to be a value
to simplify usage for modules where the initial state is a static value
(ie. has no dependency on data in `settings`).
parent 4fd547b3
......@@ -197,7 +197,7 @@ export function createStore(modules, initArgs = [], middleware = []) {
* @template Actions
* @template {SelectorMap<State>} Selectors
* @template RootSelectors
* @param {(...args: any[]) => State} initialState
* @param {State | ((...args: any[]) => State)} initialState
* @param {object} config
* @param {string} config.namespace -
* The key under which this module's state will live in the store's root state
......@@ -211,6 +211,12 @@ export function createStoreModule(initialState, config) {
// The `initialState` argument is separate to `config` as this allows
// TypeScript to infer the `State` type in the `config` argument at the
// `createStoreModule` call site.
if (!(initialState instanceof Function)) {
const state = initialState;
initialState = () => state;
}
return {
initialState,
...config,
......
......@@ -173,6 +173,18 @@ describe('createStore', () => {
assert.equal(store.getState().b.count, 0);
});
it('supports modules with static initial state', () => {
const initialState = { value: 42 };
const module = createStoreModule(initialState, {
namespace: 'test',
actionCreators: {},
reducers: {},
selectors: {},
});
const store = createStore([module]);
assert.equal(store.getState().test.value, 42);
});
if (process.env.NODE_ENV !== 'production') {
it('freezes store state in development builds', () => {
const store = counterStore();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment