sub-redux
Version:
[](https://www.npmjs.com/package/sub-redux) [](https://www.npmjs.com/package/sub-redux)
35 lines (31 loc) • 904 B
text/typescript
import { wrapSubAction } from './actionHelpers';
import { MiddlewareAPI, Store } from 'redux';
export function getSubMiddlewareApi(
instance: string,
store: MiddlewareAPI,
): MiddlewareAPI {
return {
dispatch: subAction => {
store.dispatch(wrapSubAction({ instance, subAction }));
return subAction;
},
getState: () => store.getState().subRedux[instance].state,
};
}
export type SubReduxStore = Store & {
isSubReduxStore: true;
subReduxInstance: string;
parentStore: Store;
};
export function getSubStore(instance: string, store: Store): SubReduxStore {
return {
...getSubMiddlewareApi(instance, store),
subscribe: subscriber => store.subscribe(subscriber),
replaceReducer() {
throw new Error('subredux subStore does not support replaceReducer');
},
isSubReduxStore: true,
subReduxInstance: instance,
parentStore: store,
};
}