source: node_modules/react-redux/src/connect/mergeProps.ts

Last change on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 2.0 KB
RevLine 
[a762898]1import type { Action, Dispatch } from 'redux'
2import verifyPlainObject from '../utils/verifyPlainObject'
3import { createInvalidArgFactory } from './invalidArgFactory'
4import type { MergeProps } from './selectorFactory'
5import type { EqualityFn } from '../types'
6
7function 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
21function 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
65export 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}
Note: See TracBrowser for help on using the repository browser.