Skip to content

Commit

Permalink
chore(mapStateToProps): move to ts first pass
Browse files Browse the repository at this point in the history
  • Loading branch information
tony-go authored and timdorr committed Jun 29, 2021
1 parent 76ed07f commit 9c7ca0e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
@@ -1,12 +1,16 @@
import { wrapMapToPropsConstant, wrapMapToPropsFunc } from './wrapMapToProps'
import {
MapToProps,
wrapMapToPropsConstant,
wrapMapToPropsFunc,
} from './wrapMapToProps'

export function whenMapStateToPropsIsFunction(mapStateToProps) {
export function whenMapStateToPropsIsFunction(mapStateToProps?: MapToProps) {
return typeof mapStateToProps === 'function'
? wrapMapToPropsFunc(mapStateToProps, 'mapStateToProps')
: undefined
}

export function whenMapStateToPropsIsMissing(mapStateToProps) {
export function whenMapStateToPropsIsMissing(mapStateToProps?: MapToProps) {
return !mapStateToProps ? wrapMapToPropsConstant(() => ({})) : undefined
}

Expand Down
36 changes: 25 additions & 11 deletions src/connect/wrapMapToProps.js → src/connect/wrapMapToProps.ts
@@ -1,7 +1,16 @@
import { Dispatch } from 'redux'

import verifyPlainObject from '../utils/verifyPlainObject'

export function wrapMapToPropsConstant(getConstant) {
return function initConstantSelector(dispatch, options) {
export type MapToProps = {
(stateOrDispatch: any, ownProps?: any): any
dependsOnOwnProps: boolean
}

export function wrapMapToPropsConstant(
getConstant: (dispatch: Dispatch, options: any) => void
) {
return function initConstantSelector(dispatch: Dispatch, options: any) {
const constant = getConstant(dispatch, options)

function constantSelector() {
Expand All @@ -19,9 +28,8 @@ export function wrapMapToPropsConstant(getConstant) {
// A length of one signals that mapToProps does not depend on props from the parent component.
// A length of zero is assumed to mean mapToProps is getting args via arguments or ...args and
// therefore not reporting its length accurately..
export function getDependsOnOwnProps(mapToProps) {
return mapToProps.dependsOnOwnProps !== null &&
mapToProps.dependsOnOwnProps !== undefined
export function getDependsOnOwnProps(mapToProps: MapToProps) {
return mapToProps?.dependsOnOwnProps
? Boolean(mapToProps.dependsOnOwnProps)
: mapToProps.length !== 1
}
Expand All @@ -38,20 +46,26 @@ export function getDependsOnOwnProps(mapToProps) {
// * On first call, verifies the first result is a plain object, in order to warn
// the developer that their mapToProps function is not returning a valid result.
//
export function wrapMapToPropsFunc(mapToProps, methodName) {
return function initProxySelector(dispatch, { displayName }) {
const proxy = function mapToPropsProxy(stateOrDispatch, ownProps) {
export function wrapMapToPropsFunc(mapToProps: MapToProps, methodName: string) {
return function initProxySelector(
dispatch: Dispatch,
{ displayName }: { displayName: string }
) {
const proxy = function mapToPropsProxy(
stateOrDispatch: any,
ownProps: any
): any {
return proxy.dependsOnOwnProps
? proxy.mapToProps(stateOrDispatch, ownProps)
: proxy.mapToProps(stateOrDispatch)
: proxy.mapToProps(stateOrDispatch, null)
}

// allow detectFactoryAndVerify to get ownProps
proxy.dependsOnOwnProps = true

proxy.mapToProps = function detectFactoryAndVerify(
stateOrDispatch,
ownProps
stateOrDispatch: any,
ownProps: any
) {
proxy.mapToProps = mapToProps
proxy.dependsOnOwnProps = getDependsOnOwnProps(mapToProps)
Expand Down

0 comments on commit 9c7ca0e

Please sign in to comment.