UNPKG

sub-redux

Version:

[![npm version](https://img.shields.io/npm/v/sub-redux.svg)](https://www.npmjs.com/package/sub-redux) [![npm](https://img.shields.io/npm/dm/sub-redux.svg)](https://www.npmjs.com/package/sub-redux)

35 lines (31 loc) 904 B
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, }; }