| 1 | import { Dispatch } from './types/store'
|
|---|
| 2 | import { ActionCreator, ActionCreatorsMapObject, Action } from './types/actions'
|
|---|
| 3 | import { kindOf } from './utils/kindOf'
|
|---|
| 4 |
|
|---|
| 5 | function bindActionCreator<A extends Action>(
|
|---|
| 6 | actionCreator: ActionCreator<A>,
|
|---|
| 7 | dispatch: Dispatch<A>
|
|---|
| 8 | ) {
|
|---|
| 9 | return function (this: any, ...args: any[]) {
|
|---|
| 10 | return dispatch(actionCreator.apply(this, args))
|
|---|
| 11 | }
|
|---|
| 12 | }
|
|---|
| 13 |
|
|---|
| 14 | /**
|
|---|
| 15 | * Turns an object whose values are action creators, into an object with the
|
|---|
| 16 | * same keys, but with every function wrapped into a `dispatch` call so they
|
|---|
| 17 | * may be invoked directly. This is just a convenience method, as you can call
|
|---|
| 18 | * `store.dispatch(MyActionCreators.doSomething())` yourself just fine.
|
|---|
| 19 | *
|
|---|
| 20 | * For convenience, you can also pass an action creator as the first argument,
|
|---|
| 21 | * and get a dispatch wrapped function in return.
|
|---|
| 22 | *
|
|---|
| 23 | * @param actionCreators An object whose values are action
|
|---|
| 24 | * creator functions. One handy way to obtain it is to use `import * as`
|
|---|
| 25 | * syntax. You may also pass a single function.
|
|---|
| 26 | *
|
|---|
| 27 | * @param dispatch The `dispatch` function available on your Redux
|
|---|
| 28 | * store.
|
|---|
| 29 | *
|
|---|
| 30 | * @returns The object mimicking the original object, but with
|
|---|
| 31 | * every action creator wrapped into the `dispatch` call. If you passed a
|
|---|
| 32 | * function as `actionCreators`, the return value will also be a single
|
|---|
| 33 | * function.
|
|---|
| 34 | */
|
|---|
| 35 | export default function bindActionCreators<A, C extends ActionCreator<A>>(
|
|---|
| 36 | actionCreator: C,
|
|---|
| 37 | dispatch: Dispatch
|
|---|
| 38 | ): C
|
|---|
| 39 |
|
|---|
| 40 | export default function bindActionCreators<
|
|---|
| 41 | A extends ActionCreator<any>,
|
|---|
| 42 | B extends ActionCreator<any>
|
|---|
| 43 | >(actionCreator: A, dispatch: Dispatch): B
|
|---|
| 44 |
|
|---|
| 45 | export default function bindActionCreators<
|
|---|
| 46 | A,
|
|---|
| 47 | M extends ActionCreatorsMapObject<A>
|
|---|
| 48 | >(actionCreators: M, dispatch: Dispatch): M
|
|---|
| 49 | export default function bindActionCreators<
|
|---|
| 50 | M extends ActionCreatorsMapObject,
|
|---|
| 51 | N extends ActionCreatorsMapObject
|
|---|
| 52 | >(actionCreators: M, dispatch: Dispatch): N
|
|---|
| 53 |
|
|---|
| 54 | export default function bindActionCreators(
|
|---|
| 55 | actionCreators: ActionCreator<any> | ActionCreatorsMapObject,
|
|---|
| 56 | dispatch: Dispatch
|
|---|
| 57 | ) {
|
|---|
| 58 | if (typeof actionCreators === 'function') {
|
|---|
| 59 | return bindActionCreator(actionCreators, dispatch)
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | if (typeof actionCreators !== 'object' || actionCreators === null) {
|
|---|
| 63 | throw new Error(
|
|---|
| 64 | `bindActionCreators expected an object or a function, but instead received: '${kindOf(
|
|---|
| 65 | actionCreators
|
|---|
| 66 | )}'. ` +
|
|---|
| 67 | `Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?`
|
|---|
| 68 | )
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | const boundActionCreators: ActionCreatorsMapObject = {}
|
|---|
| 72 | for (const key in actionCreators) {
|
|---|
| 73 | const actionCreator = actionCreators[key]
|
|---|
| 74 | if (typeof actionCreator === 'function') {
|
|---|
| 75 | boundActionCreators[key] = bindActionCreator(actionCreator, dispatch)
|
|---|
| 76 | }
|
|---|
| 77 | }
|
|---|
| 78 | return boundActionCreators
|
|---|
| 79 | }
|
|---|