Skip to content

Commit

Permalink
wrapWithShouldUpdateExperimental
Browse files Browse the repository at this point in the history
  • Loading branch information
noacoWix committed Jul 14, 2024
1 parent c4c18a1 commit acc96c1
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/connectWithShell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,31 @@ function wrapWithShouldUpdate<Props extends unknown, F extends (next: Props, pre
return ((...args: Parameters<F>) => (shouldUpdate && !shouldUpdate(shell, getOwnProps()) ? true : func(args[0], args[1]))) as F
}

function wrapWithShouldUpdateExperimental<Props extends unknown, F extends (next: Props, prev: Props) => boolean>(
shouldUpdate: Maybe<(shell: Shell, ownProps?: Props) => boolean>,
arePropsEqual: F,
getOwnProps: () => Props,
shell: Shell
): F {
if (!shouldUpdate) {
return arePropsEqual
}
let changeInPropsDetected = false
return ((...args: Parameters<F>) => {
if (!shouldUpdate(shell, getOwnProps())) {
if (!changeInPropsDetected) {
changeInPropsDetected = !arePropsEqual(args[0], args[1])
}
return true
}
if (changeInPropsDetected) {
changeInPropsDetected = false
return false
}
return arePropsEqual(args[0], args[1])
}) as F
}

function wrapWithShellContext<State, OwnProps, StateProps, DispatchProps>(
component: React.ComponentType<OwnProps & StateProps & DispatchProps>,
mapStateToProps: MapStateToProps<State, OwnProps, StateProps>,
Expand Down Expand Up @@ -98,7 +123,7 @@ function wrapWithShellContext<State, OwnProps, StateProps, DispatchProps>(
this.getOwnProps,
boundShell
),
areOwnPropsEqual: wrapWithShouldUpdate(
areOwnPropsEqual: wrapWithShouldUpdateExperimental(
shouldComponentUpdate,
reduxConnectOptions.areOwnPropsEqual,
this.getOwnProps,
Expand Down

0 comments on commit acc96c1

Please sign in to comment.