-
Notifications
You must be signed in to change notification settings - Fork 75
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
Clear interventions after they've been displayed #5341
Conversation
Add a CLEAR_INTERVENTION action to clear any stored interventions. Don't clear interventions on next subject. Update tests.
Add an onUnmount callback to the Intervention component, and call it on component cleanup. Memo-ise Intervention to prevent unnecessary renders. Update tests.
Call the clearIntervention action when the Intervention component unmounts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM minor questions only. Side note is that effects look very nice compared to the existing lifecycle hooks.
const { message } = intervention; | ||
const checkbox = React.createRef(); | ||
|
||
useEffect(() => { | ||
// the return value of an effect will be called to clean up after the component | ||
return onUnmount; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My reading suggests this will only run on unmount but it looks like effects will cleanup up if props change as well - https://reactjs.org/docs/hooks-effect.html#explanation-why-effects-run-on-each-update
I don't think this is an issue as we don't pass props to this function, thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. The props here are onUnmount, intervention, user
and we probably do want cleanup to run if any of those change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might be useful.
If you want to run an effect and clean it up only once (on mount and unmount), you can pass an empty array ([]) as a second argument. This tells React that your effect doesn’t depend on any values from props or state, so it never needs to re-run. This isn’t handled as a special case — it follows directly from how the dependencies array always works.
https://reactjs.org/docs/hooks-effect.html#tip-optimizing-performance-by-skipping-effects
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've been playing around with this a bit and I think the only time it might happen is if the intervention
prop changes while the component is being displayed. Is there a case where I might get a new intervention message while I'm already reading one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes - though in practice it would be slim. They don't send many messages but I could receive one while i'm viewing one, especially if the network / gateway has issues, i.e. received message at gateway, send to sugar but timeout to client who retries.
Would the current code unmount and then update with the newly received message? Should we drop incoming messages until there are no interventions in the store?
I'd say no to switching the store to a list of messages, unless the incoming message has a validity period attached to it, zooniverse/interventions-gateway#22
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My reading of the code is the classifier would receive a new intervention
prop, which it would pass down to the Intervention
component, which would then run useEffect()
because of the props change? I'm not sure what would happen then: maybe the new intervention would render briefly, then be cleared by the cleanup code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm happy to add the validity period to the incoming messages via the gateway API if it helps simplify this issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could use the second argument to useEffect
to ignore changes in the intervention
prop. I'm not sure what the implications of that are, but it sounds like a case that shouldn't occur anyway.
At the moment, if a second message were received while you're currently reading one, it would overwrite it and the screen would update to show the new message.
Staging branch URL: https://pr-5341.pfe-preview.zooniverse.org
Fixes #5337.
Adds a
clearIntervention
action to the classifier and calls it from theIntervention
component on component cleanup. Changes thenextSubject
action so that it doesn't clear any stored interventions by default.Required Manual Testing
Review Checklist
rm -rf node_modules/ && npm install
and app works as expected?Optional
ChangeListener
orPromiseRenderer
components with code that updates component state?