add skip dispatch function

This commit is contained in:
ChaosExAnima 2025-08-20 11:45:04 +02:00
parent 54da7ff12b
commit f2025914e7
No known key found for this signature in database
GPG Key ID: 8F2B333100FB6117

View File

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