import { isPromiseLike } from "."; import { Action } from "../action/types"; import { StoreDispatchApi } from "../store/types"; import { Thunk } from "./types"; export const typeThunk = (thunk: T) => thunk; export const dispatchThunk = function < S extends StoreDispatchApi, T extends Thunk>> >( store: S, thunk: T ): ReturnType extends Promise ? Promise : Action[] { const dispatchedActions: Action[] = []; const result = thunk({ getState: store.getState, dispatch: function (action) { const dispatchedAction = store.dispatch(action); if (dispatchedAction) { dispatchedActions.push(dispatchedAction); } return dispatchedAction; }, }); if (isPromiseLike(result)) { return result.then(function () { return dispatchedActions; }) as any; } return dispatchedActions as any; };