From 91aee06f91f9f5572231a03ba65c69196964729e Mon Sep 17 00:00:00 2001 From: Durrab Jami Khan Date: Fri, 12 Feb 2021 09:33:40 -0800 Subject: [PATCH] [FIX] xarc-react-redux-saga with factory method (#1822) Co-authored-by: Durrab Khan --- .../src/common/index.tsx | 55 ++++++++----------- 1 file changed, 22 insertions(+), 33 deletions(-) diff --git a/packages/xarc-react-redux-saga/src/common/index.tsx b/packages/xarc-react-redux-saga/src/common/index.tsx index 245e4cedb..f518afb51 100644 --- a/packages/xarc-react-redux-saga/src/common/index.tsx +++ b/packages/xarc-react-redux-saga/src/common/index.tsx @@ -1,48 +1,37 @@ -import { StoreEnhancer } from "redux"; -import { Reducer, Store, StoreCreator } from "redux"; import createSagaMiddleware from "redux-saga"; +import { + applyMiddleware, + Reducer, + createStore, + ReduxFeature, + ReduxDecoratorParams, + ReduxFeatureDecorator +} from "@xarc/react-redux"; + export type ReduxSagaOption = { /** * rootSaga must be provided from the App for initializing Redux Saga Middleware */ rootSaga: any; - /** - * applyMiddlware provided from @xarc-react-redux - */ - applyMiddleware: (e: any) => StoreEnhancer; - - /** - * createStore provided from @xarc-react-redux - */ - createStore: StoreCreator; - - /** - * reducers provided from @xarc-react-redux - */ - reducers: Reducer; - - /** - * initialState provided from @xarc-react-redux - */ - initialState: any; }; /** * @param options */ -export function reduxSagaDecor(options: ReduxSagaOption): Store { - const { rootSaga, applyMiddleware, createStore, reducers, initialState } = options; +export function reduxSagaDecor(options: ReduxSagaOption): ReduxFeatureDecorator { + const { rootSaga } = options; - if (!rootSaga) { - throw new Error("[REDUX-SAGA] must provide a root epic if redux-saga selected!"); + return { + decorate(_reduxFeat: ReduxFeature, params: ReduxDecoratorParams) { + const sagaMiddleware = createSagaMiddleware(); + const store = createStore( + (params.reducers as Reducer) || (x => x), + params.initialState, + applyMiddleware(sagaMiddleware) + ); + sagaMiddleware.run(rootSaga); + return { store }; + } } - const sagaMiddleware = createSagaMiddleware(); - const reduxSagaStore = createStore( - (reducers as Reducer) || (x => x), - initialState, - applyMiddleware(sagaMiddleware) - ); - sagaMiddleware.run(rootSaga); - return reduxSagaStore; }