Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #1468 .
Motivation
Imperative handlers sometimes need context to choose their next behavior. It's critical we don't lock this into the React lifecycle as that is an antipattern (the useEffect-trap) - not just annoying from a coding perspective.
Solution
We already have imperative access with middlewares, so use the same function to provide to controller.
It's important we do not hide getState() inside any accessors, as it is critical to understand where state comes from so it always represents the expected value.
Open questions
controller.getState() won't include updated state til the providers useEffect() is called. This means it could potentially not have the result. We might need another promise to listen to next state update to complete this functionality.
Until this is resolved, one can likely just use a useIdleCallback() as the lower priority nature compared to useEffects should guarantee running after the update.