| 1 | import type { Action, Dispatch } from 'redux'
|
|---|
| 2 | import verifyPlainObject from '../utils/verifyPlainObject'
|
|---|
| 3 | import { createInvalidArgFactory } from './invalidArgFactory'
|
|---|
| 4 | import type { MergeProps } from './selectorFactory'
|
|---|
| 5 | import type { EqualityFn } from '../types'
|
|---|
| 6 |
|
|---|
| 7 | function defaultMergeProps<
|
|---|
| 8 | TStateProps,
|
|---|
| 9 | TDispatchProps,
|
|---|
| 10 | TOwnProps,
|
|---|
| 11 | TMergedProps,
|
|---|
| 12 | >(
|
|---|
| 13 | stateProps: TStateProps,
|
|---|
| 14 | dispatchProps: TDispatchProps,
|
|---|
| 15 | ownProps: TOwnProps,
|
|---|
| 16 | ): TMergedProps {
|
|---|
| 17 | // @ts-ignore
|
|---|
| 18 | return { ...ownProps, ...stateProps, ...dispatchProps }
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| 21 | function wrapMergePropsFunc<
|
|---|
| 22 | TStateProps,
|
|---|
| 23 | TDispatchProps,
|
|---|
| 24 | TOwnProps,
|
|---|
| 25 | TMergedProps,
|
|---|
| 26 | >(
|
|---|
| 27 | mergeProps: MergeProps<TStateProps, TDispatchProps, TOwnProps, TMergedProps>,
|
|---|
| 28 | ): (
|
|---|
| 29 | dispatch: Dispatch<Action<string>>,
|
|---|
| 30 | options: {
|
|---|
| 31 | readonly displayName: string
|
|---|
| 32 | readonly areMergedPropsEqual: EqualityFn<TMergedProps>
|
|---|
| 33 | },
|
|---|
| 34 | ) => MergeProps<TStateProps, TDispatchProps, TOwnProps, TMergedProps> {
|
|---|
| 35 | return function initMergePropsProxy(
|
|---|
| 36 | dispatch,
|
|---|
| 37 | { displayName, areMergedPropsEqual },
|
|---|
| 38 | ) {
|
|---|
| 39 | let hasRunOnce = false
|
|---|
| 40 | let mergedProps: TMergedProps
|
|---|
| 41 |
|
|---|
| 42 | return function mergePropsProxy(
|
|---|
| 43 | stateProps: TStateProps,
|
|---|
| 44 | dispatchProps: TDispatchProps,
|
|---|
| 45 | ownProps: TOwnProps,
|
|---|
| 46 | ) {
|
|---|
| 47 | const nextMergedProps = mergeProps(stateProps, dispatchProps, ownProps)
|
|---|
| 48 |
|
|---|
| 49 | if (hasRunOnce) {
|
|---|
| 50 | if (!areMergedPropsEqual(nextMergedProps, mergedProps))
|
|---|
| 51 | mergedProps = nextMergedProps
|
|---|
| 52 | } else {
|
|---|
| 53 | hasRunOnce = true
|
|---|
| 54 | mergedProps = nextMergedProps
|
|---|
| 55 |
|
|---|
| 56 | if (process.env.NODE_ENV !== 'production')
|
|---|
| 57 | verifyPlainObject(mergedProps, displayName, 'mergeProps')
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | return mergedProps
|
|---|
| 61 | }
|
|---|
| 62 | }
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | export function mergePropsFactory<
|
|---|
| 66 | TStateProps,
|
|---|
| 67 | TDispatchProps,
|
|---|
| 68 | TOwnProps,
|
|---|
| 69 | TMergedProps,
|
|---|
| 70 | >(
|
|---|
| 71 | mergeProps?: MergeProps<TStateProps, TDispatchProps, TOwnProps, TMergedProps>,
|
|---|
| 72 | ) {
|
|---|
| 73 | return !mergeProps
|
|---|
| 74 | ? () => defaultMergeProps
|
|---|
| 75 | : typeof mergeProps === 'function'
|
|---|
| 76 | ? wrapMergePropsFunc(mergeProps)
|
|---|
| 77 | : createInvalidArgFactory(mergeProps, 'mergeProps')
|
|---|
| 78 | }
|
|---|