Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] xarc-react-redux-saga with factory method #1822

Merged
merged 1 commit into from
Feb 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}