Skip to content

Commit

Permalink
[FIX] xarc-react-redux-saga with factory method (#1822)
Browse files Browse the repository at this point in the history
Co-authored-by: Durrab Khan <[email protected]>
  • Loading branch information
durrab and Durrab Khan authored Feb 12, 2021
1 parent 0d94c51 commit 91aee06
Showing 1 changed file with 22 additions and 33 deletions.
55 changes: 22 additions & 33 deletions packages/xarc-react-redux-saga/src/common/index.tsx
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;
}

0 comments on commit 91aee06

Please sign in to comment.