-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FIX] xarc-react-redux-saga with factory method (#1822)
Co-authored-by: Durrab Khan <[email protected]>
- Loading branch information
Showing
1 changed file
with
22 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<unknown, any>; | ||
|
||
/** | ||
* 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<unknown, any>) || (x => x), | ||
params.initialState, | ||
applyMiddleware(sagaMiddleware) | ||
); | ||
sagaMiddleware.run(rootSaga); | ||
return { store }; | ||
} | ||
} | ||
const sagaMiddleware = createSagaMiddleware(); | ||
const reduxSagaStore = createStore( | ||
(reducers as Reducer<unknown, any>) || (x => x), | ||
initialState, | ||
applyMiddleware(sagaMiddleware) | ||
); | ||
sagaMiddleware.run(rootSaga); | ||
return reduxSagaStore; | ||
} |