mirror of
https://github.com/mastodon/mastodon.git
synced 2025-11-27 18:10:58 +00:00
add skip dispatch function
This commit is contained in:
parent
54da7ff12b
commit
f2025914e7
|
|
@ -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);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user