Compare commits

...

1 Commits

Author SHA1 Message Date
ChaosExAnima
f2025914e7 add skip dispatch function 2025-08-20 11:45:04 +02:00

View File

@@ -60,7 +60,7 @@ type AppThunk<Arg = void, Returned = void> = (
type AppThunkCreator<Arg = void, Returned = void, ExtraArg = unknown> = (
arg: Arg,
api: AppThunkApi,
api: AppThunkApi & { skipDispatch: () => void },
extra?: ExtraArg,
) => Returned;
@@ -102,11 +102,20 @@ export function createAppThunk<Arg = void, Returned = void, ExtraArg = unknown>(
const extra = isDispatcher ? maybeExtra : (maybeCreatorOrExtra as ExtraArg);
let action: null | AppThunkActionCreator<Arg, Returned> = null;
let skipDispatch = false;
const toggleSkipDispatch = () => {
skipDispatch = !skipDispatch;
};
// Creates a thunk that dispatches the action with the result of the creator.
const actionCreator: AppThunk<Arg, Returned> = (arg) => {
return (dispatch, getState) => {
const result = creator(arg, { dispatch, getState }, extra);
if (action) {
const result = creator(
arg,
{ dispatch, getState, skipDispatch: toggleSkipDispatch },
extra,
);
if (action && !skipDispatch) {
// Dispatches the action with the result.
const actionObj = action(result, arg);
dispatch(actionObj);