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

fix: prevent sending updated props to plugin when props do not change [LIBS-514] #1351

Merged
merged 6 commits into from
Dec 20, 2023
Merged
Changes from all 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
16 changes: 14 additions & 2 deletions services/plugin/src/Plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ export const Plugin = ({
}
}, [height, width])

// since we do not know what props are passed, the dependency array has to be keys of whatever is standard prop
const memoizedPropsToPass = useMemo(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the fix. I'm not sure I like this solution but since it's unknown what props are actually going to be passed, this was the best way I could come up with handling the dependency array.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't the memoized value change if the values of the propsToPass change, not just the keys? will that be the case now?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦‍♂️ thanks @kabaros. Yes; this should have been [...Object.values(propsToPass)]...but now I think I remember that I did that originally, but then thought that the order of the props wouldn't theoretically guaranteed to be in the same order on the object, hence it might be better to do something like [...Object.keys(propsToPass).sort().map(k=>propsToPass[k])], but then I get a type error:

  No index signature with a parameter of type 'string' was found on type '{ propsToPass: PropsObject; }'.

And then I tried to explicitly declare propsToPass as an object with string keys (I made an interface of

interface PropsObject {
  [propName: string]: any;
}

but that did not resolve the type issues 🫤

I've pushed a change to [...Object.keys(propsToPass).sort().map(k=>propsToPass[k])], but let me know if you have tips about what I could to fix the type warnings 🙏

() => propsToPass,
/* eslint-disable react-hooks/exhaustive-deps */
[
...Object.keys(propsToPass)
.sort()
.map((k) => (propsToPass as any)[k]),
]
/* eslint-enable react-hooks/exhaustive-deps */
)

useEffect(() => {
setCommunicationReceived(false)
}, [pluginEntryPoint])
Expand All @@ -86,7 +98,7 @@ export const Plugin = ({

if (iframeRef?.current) {
const iframeProps = {
...propsToPass,
...memoizedPropsToPass,
alertsAdd,
setPluginHeight,
setPluginWidth,
Expand Down Expand Up @@ -128,7 +140,7 @@ export const Plugin = ({
}
// prevCommunicationReceived update should not retrigger this hook
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [propsToPass, communicationReceived, inErrorState, alertsAdd])
}, [memoizedPropsToPass, communicationReceived, inErrorState, alertsAdd])

if (data && !pluginEntryPoint) {
return (
Expand Down
Loading