[inulax] 修复middleware(...) is not a function
This commit is contained in:
parent
3fb6652607
commit
3019fa5581
|
@ -27,12 +27,12 @@ export {
|
||||||
createDispatchHook,
|
createDispatchHook,
|
||||||
} from './reduxReact';
|
} from './reduxReact';
|
||||||
|
|
||||||
export type ReduxStoreHandler = {
|
export type ReduxStoreHandler<T = any> = {
|
||||||
reducer: (state: any, action: { type: string }) => any;
|
reducer(state: T, action: { type: string }): any;
|
||||||
dispatch: (action: { type: string }) => void;
|
dispatch(action: { type: string }): void;
|
||||||
getState: () => any;
|
getState(): T;
|
||||||
subscribe: (listener: () => void) => () => void;
|
subscribe(listener: () => void): () => void;
|
||||||
replaceReducer: (reducer: (state: any, action: { type: string }) => any) => void;
|
replaceReducer(reducer: (state: T, action: { type: string }) => any): void;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type ReduxAction = {
|
export type ReduxAction = {
|
||||||
|
@ -53,6 +53,9 @@ export type ReduxMiddleware = (
|
||||||
|
|
||||||
type Reducer = (state: any, action: ReduxAction) => any;
|
type Reducer = (state: any, action: ReduxAction) => any;
|
||||||
|
|
||||||
|
type StoreCreator = (reducer: Reducer, preloadedState?: any) => ReduxStoreHandler
|
||||||
|
type StoreEnhancer = (next: StoreCreator) => StoreCreator
|
||||||
|
|
||||||
function mergeData(state, data) {
|
function mergeData(state, data) {
|
||||||
if (!data) {
|
if (!data) {
|
||||||
state.stateWrapper = data;
|
state.stateWrapper = data;
|
||||||
|
@ -87,7 +90,7 @@ function mergeData(state, data) {
|
||||||
state.stateWrapper = data;
|
state.stateWrapper = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createStore(reducer: Reducer, preloadedState?: any, enhancers?): ReduxStoreHandler {
|
export function createStore(reducer: Reducer, preloadedState?: any, enhancers?: StoreEnhancer): ReduxStoreHandler {
|
||||||
const store = createStoreX({
|
const store = createStoreX({
|
||||||
id: 'defaultStore',
|
id: 'defaultStore',
|
||||||
state: { stateWrapper: preloadedState },
|
state: { stateWrapper: preloadedState },
|
||||||
|
@ -150,19 +153,23 @@ export function combineReducers(reducers: { [key: string]: Reducer }): Reducer {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function applyMiddlewares(store: ReduxStoreHandler, middlewares: ReduxMiddleware[]): void {
|
function applyMiddlewares(createStore: StoreCreator, middlewares: ReduxMiddleware[]): StoreCreator {
|
||||||
|
return (reducer, preloadedState) => {
|
||||||
middlewares = middlewares.slice();
|
middlewares = middlewares.slice();
|
||||||
middlewares.reverse();
|
middlewares.reverse();
|
||||||
let dispatch = store.dispatch;
|
const storeObj = createStore(reducer, preloadedState);
|
||||||
|
let dispatch = storeObj.dispatch;
|
||||||
middlewares.forEach(middleware => {
|
middlewares.forEach(middleware => {
|
||||||
dispatch = middleware(store)(dispatch);
|
dispatch = middleware(storeObj)(dispatch);
|
||||||
});
|
});
|
||||||
store.dispatch = dispatch;
|
storeObj.dispatch = dispatch;
|
||||||
|
return storeObj;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function applyMiddleware(...middlewares: ReduxMiddleware[]): (store: ReduxStoreHandler) => void {
|
export function applyMiddleware(...middlewares: ReduxMiddleware[]): (createStore: StoreCreator) => StoreCreator {
|
||||||
return store => {
|
return createStore => {
|
||||||
return applyMiddlewares(store, middlewares);
|
return applyMiddlewares(createStore, middlewares);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -170,7 +177,7 @@ type ActionCreator = (...params: any[]) => ReduxAction;
|
||||||
type ActionCreators = { [key: string]: ActionCreator };
|
type ActionCreators = { [key: string]: ActionCreator };
|
||||||
export type BoundActionCreator = (...params: any[]) => void;
|
export type BoundActionCreator = (...params: any[]) => void;
|
||||||
type BoundActionCreators = { [key: string]: BoundActionCreator };
|
type BoundActionCreators = { [key: string]: BoundActionCreator };
|
||||||
type Dispatch = (action) => any;
|
type Dispatch = (action: ReduxAction) => any;
|
||||||
|
|
||||||
export function bindActionCreators(actionCreators: ActionCreators, dispatch: Dispatch): BoundActionCreators {
|
export function bindActionCreators(actionCreators: ActionCreators, dispatch: Dispatch): BoundActionCreators {
|
||||||
const boundActionCreators = {};
|
const boundActionCreators = {};
|
||||||
|
|
|
@ -35,5 +35,5 @@ function createThunkMiddleware(extraArgument?: any): ReduxMiddleware {
|
||||||
}
|
}
|
||||||
|
|
||||||
export const thunk = createThunkMiddleware();
|
export const thunk = createThunkMiddleware();
|
||||||
// @ts-ignore
|
|
||||||
thunk.withExtraArgument = createThunkMiddleware;
|
export const withExtraArgument = createThunkMiddleware;
|
||||||
|
|
Loading…
Reference in New Issue