Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

arePropsEqualFuncWrapper #254

Merged
merged 6 commits into from
Jul 16, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/connectWithShell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,32 @@ 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 arePropsEqualFuncWrapper<Props extends unknown, F extends (next: Props, prev: Props) => boolean>(
componentShouldUpdateFunc: Maybe<(shell: Shell, ownProps?: Props) => boolean>,
arePropsEqualFunc: F,
getOwnProps: () => Props,
shell: Shell
): F {
if (!componentShouldUpdateFunc) {
return arePropsEqualFunc
}
let changeInPropsDetected = false
noacoWix marked this conversation as resolved.
Show resolved Hide resolved
return ((...args: Parameters<F>) => {
const componentShouldUpdate = componentShouldUpdateFunc(shell, getOwnProps())
if (componentShouldUpdate) {
if (changeInPropsDetected) {
changeInPropsDetected = false
return false
}
return arePropsEqualFunc(args[0], args[1])
}
if (!changeInPropsDetected) {
changeInPropsDetected = !arePropsEqualFunc(args[0], args[1])
}
return true
}) 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 +124,7 @@ function wrapWithShellContext<State, OwnProps, StateProps, DispatchProps>(
this.getOwnProps,
boundShell
),
areOwnPropsEqual: wrapWithShouldUpdate(
areOwnPropsEqual: arePropsEqualFuncWrapper(
shouldComponentUpdate,
reduxConnectOptions.areOwnPropsEqual,
this.getOwnProps,
Expand Down
Loading