[inulax] 修复middleware(...) is not a function

This commit is contained in:
huangxuan 2023-11-30 16:11:37 +08:00
parent 3fb6652607
commit 3019fa5581
No known key found for this signature in database
GPG Key ID: E79F50C67022565D
2 changed files with 28 additions and 21 deletions

View File

@ -27,12 +27,12 @@ export {
createDispatchHook,
} from './reduxReact';
export type ReduxStoreHandler = {
reducer: (state: any, action: { type: string }) => any;
dispatch: (action: { type: string }) => void;
getState: () => any;
subscribe: (listener: () => void) => () => void;
replaceReducer: (reducer: (state: any, action: { type: string }) => any) => void;
export type ReduxStoreHandler<T = any> = {
reducer(state: T, action: { type: string }): any;
dispatch(action: { type: string }): void;
getState(): T;
subscribe(listener: () => void): () => void;
replaceReducer(reducer: (state: T, action: { type: string }) => any): void;
};
export type ReduxAction = {
@ -53,6 +53,9 @@ export type ReduxMiddleware = (
type Reducer = (state: any, action: ReduxAction) => any;
type StoreCreator = (reducer: Reducer, preloadedState?: any) => ReduxStoreHandler
type StoreEnhancer = (next: StoreCreator) => StoreCreator
function mergeData(state, data) {
if (!data) {
state.stateWrapper = data;
@ -87,7 +90,7 @@ function mergeData(state, 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({
id: 'defaultStore',
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.reverse();
let dispatch = store.dispatch;
const storeObj = createStore(reducer, preloadedState);
let dispatch = storeObj.dispatch;
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 {
return store => {
return applyMiddlewares(store, middlewares);
export function applyMiddleware(...middlewares: ReduxMiddleware[]): (createStore: StoreCreator) => StoreCreator {
return createStore => {
return applyMiddlewares(createStore, middlewares);
};
}
@ -170,7 +177,7 @@ type ActionCreator = (...params: any[]) => ReduxAction;
type ActionCreators = { [key: string]: ActionCreator };
export type BoundActionCreator = (...params: any[]) => void;
type BoundActionCreators = { [key: string]: BoundActionCreator };
type Dispatch = (action) => any;
type Dispatch = (action: ReduxAction) => any;
export function bindActionCreators(actionCreators: ActionCreators, dispatch: Dispatch): BoundActionCreators {
const boundActionCreators = {};

View File

@ -35,5 +35,5 @@ function createThunkMiddleware(extraArgument?: any): ReduxMiddleware {
}
export const thunk = createThunkMiddleware();
// @ts-ignore
thunk.withExtraArgument = createThunkMiddleware;
export const withExtraArgument = createThunkMiddleware;