MCPcopy
hub / github.com/reduxjs/redux / dispatchExtension

Function dispatchExtension

test/typescript/enhancers.ts:11–44  ·  view source on GitHub ↗

* Store enhancer that extends the type of dispatch.

()

Source from the content-addressed store, hash-verified

9 * Store enhancer that extends the type of dispatch.
10 */
11function dispatchExtension() {
12 type PromiseDispatch = <T extends Action>(promise: Promise<T>) => Promise<T>
13
14 const enhancer: StoreEnhancer<{
15 dispatch: PromiseDispatch
16 }> =
17 createStore =>
18 <S, A extends Action, PreloadedState>(
19 reducer: Reducer<S, A, PreloadedState>,
20 preloadedState?: PreloadedState | undefined
21 ) => {
22 const store = createStore(reducer, preloadedState)
23 return {
24 ...store,
25 dispatch: (action: any) => {
26 if (action.type) {
27 store.dispatch(action)
28 } else if (action.then) {
29 action.then(store.dispatch)
30 }
31 return action
32 }
33 }
34 }
35
36 const store = createStore(reducer, enhancer)
37
38 store.dispatch({ type: 'INCREMENT' })
39 store.dispatch(Promise.resolve({ type: 'INCREMENT' }))
40 // @ts-expect-error
41 store.dispatch('not-an-action')
42 // @ts-expect-error
43 store.dispatch(Promise.resolve('not-an-action'))
44}
45
46/**
47 * Store enhancer that extends the type of the state.

Callers

nothing calls this directly

Calls 1

createStoreFunction · 0.85

Tested by

no test coverage detected