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

Conversation

tomzemp
Copy link
Member

@tomzemp tomzemp commented Jun 23, 2023

Implements LIBS-514


Key features

  1. feature

Fixes app/plugin communication to pass props via post-robot only when the props provided to wrapper component have changed.

Description

Noted on ticket, but the intent of the wrappers was only to send a communication update when props passed to the component changed. However, when the parent with component updated, the plugins were deemed to have changed because of unstable object references (even when the passed props were themselves stable)

e.g. we had an app that passed stabled references to the whenever it rerendered:

const MyApp = () => {
const myFunction = useCallback(()=>{console.log("I would do something in the plugin"),[]}
const myString = "plugins are cool?"
return <Plugin myFunction={myFunction} myString={myString} />
}

however, when in the Plugin wrapper component...

const Plugin = (props) => {
  useEffect(()=>{
    // since the props updated, send them again
    post-robot_communication(props)
  },[props])
}

the overall props object was not itself a stable reference, hence while the individual values were not changing, the communication would reoccur.


Checklist

  • Have written Documentation
    • This is a bug for a non-released feature, so no documentation updates needed at the moment
  • Has tests coverage
    • tests will be written as part of general work to write tests for this service


const [inErrorState, setInErrorState] = useState<boolean>(false)

// 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 🙏

@tomzemp tomzemp marked this pull request as ready for review August 25, 2023 11:47
@tomzemp tomzemp requested a review from kabaros October 3, 2023 07:45
@tomzemp tomzemp merged commit 86c6f75 into alpha Dec 20, 2023
7 checks passed
@tomzemp tomzemp deleted the LIBS-514/memoize-passed-props branch December 20, 2023 11:46
@dhis2-bot
Copy link
Contributor

@dhis2-bot
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants