Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
tony-go committed Jul 1, 2021
1 parent bc1c647 commit 0fad9ff
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
28 changes: 18 additions & 10 deletions src/connect/wrapMapToProps.ts
Expand Up @@ -3,18 +3,23 @@ import { Dispatch } from 'redux'
import { FixTypeLater } from '../types'
import verifyPlainObject from '../utils/verifyPlainObject'

export type MapToProps = {
(stateOrDispatch: FixTypeLater, ownProps?: unknown): FixTypeLater
type AnyState = { [key: string]: any }
type StateOrDispatch<S = AnyState> = S | Dispatch

type AnyProps = { [key: string]: any }

export type MapToProps<P = AnyProps> = {
(stateOrDispatch: StateOrDispatch, ownProps?: P): FixTypeLater
dependsOnOwnProps: boolean
}

export function wrapMapToPropsConstant(
// * Note:
// It seems that the dispatch identifier here
// could be dispatch in some cases (ex: whenMapDispatchToPropsIsMissing)
// and state in some others (ex: whenMapStateToPropsIsMissing)
// It seems that the dispatch argument
// could be a dispatch function in some cases (ex: whenMapDispatchToPropsIsMissing)
// and a state object in some others (ex: whenMapStateToPropsIsMissing)
//
getConstant: (dispatch: Dispatch) => FixTypeLater
getConstant: (dispatch: Dispatch) => { dispatch: Dispatch }
) {
return function initConstantSelector(dispatch: Dispatch) {
const constant = getConstant(dispatch)
Expand Down Expand Up @@ -52,26 +57,29 @@ export function getDependsOnOwnProps(mapToProps: 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: MapToProps, methodName: string) {
export function wrapMapToPropsFunc<P = AnyProps>(
mapToProps: MapToProps,
methodName: string
) {
return function initProxySelector(
dispatch: Dispatch,
{ displayName }: { displayName: string }
) {
const proxy = function mapToPropsProxy(
stateOrDispatch: FixTypeLater,
ownProps: unknown
ownProps?: P
): FixTypeLater {
return proxy.dependsOnOwnProps
? proxy.mapToProps(stateOrDispatch, ownProps)
: proxy.mapToProps(stateOrDispatch, null)
: proxy.mapToProps(stateOrDispatch, undefined)
}

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

proxy.mapToProps = function detectFactoryAndVerify(
stateOrDispatch: FixTypeLater,
ownProps: unknown
ownProps?: P
) {
proxy.mapToProps = mapToProps
proxy.dependsOnOwnProps = getDependsOnOwnProps(mapToProps)
Expand Down
4 changes: 2 additions & 2 deletions test/components/connect.spec.js
Expand Up @@ -2626,7 +2626,7 @@ describe('React', () => {
expect(memoizedReturnCount).toBe(2)
})

it('should allow a mapStateToProps factory consuming just state to return a function that gets ownProps', () => {
it.only('should allow a mapStateToProps factory consuming just state to return a function that gets ownProps', () => {
const store = createStore(() => ({ value: 1 }))

let initialState
Expand Down Expand Up @@ -2660,7 +2660,7 @@ describe('React', () => {
store.dispatch({ type: 'test' })
})

expect(initialOwnProps).toBe(null)
expect(initialOwnProps).toBe(undefined)
expect(initialState).not.toBe(undefined)
expect(secondaryOwnProps).not.toBe(undefined)
expect(secondaryOwnProps.name).toBe('a')
Expand Down

0 comments on commit 0fad9ff

Please sign in to comment.