-
Notifications
You must be signed in to change notification settings - Fork 18
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
Modify useEditorView to flushSync all dispatched transactions #49
Conversation
@ilyaGurevich I'm not sure the ref is getting us what we want here! Is it possible that we just want to useMemo the call to |
Also, how would you feel about trying to add a unit test to capture this use case? Feels edge-case-y enough to earn one! |
I'm confused and want to investigate a little bit more. Using So, using |
I found one bit of interesting info here. The actual dispatch in the case of typing is coming from ProseMirror reading a DOM mutation, not from an event. On React 17, that would be unbatched. On React 18, that'd be batched but not synchronous. I have suggested previously that we should always flush sync, since editor input is high priority for proper user experience. The only reason I hesitated was that I was afraid we had code at NYT that had components dispatching on mount, which is not a legal place to do a I'd be happy to synchronously flush unconditionally here, and work out issues with dispatching at bad lifecycle moments downstream. |
In other words, because ProseMirror uses I think this is on the right track, but doesn't go far enough, and the justification isn't quite correct. We don't want to opt out of batching. We want to be sure the batch commits synchronously. |
I'm ok doing this as well, I think it might also make this overall logic simpler to reason around without needing to specifically understand what composition is in general. What sorts of examples of bad lifecycles moments would you expect in this case? Like a bad case of I think I'm also still somewhat confused on why:
|
Yeah. It's risky to dispatch transactions in
We were purposefully batching transactions so that when a dispatch causes a state change (generally via Redux), it doesn't cause React to invoke any component lifecycle hooks until after we return from the call to Using |
Just to reiterate/clarify: |
I'd be curious about making a custom eslint configuration here to provide warnings when you do stuff like this, similar to calling setState in a useEffect which is discouraged sometimes |
Yeah, it'd be nice, but I think it's low on the priorities for this repo. Right now, we're not even sure we have the implementation at all how we'd like it. |
Thanks for this, @ilyaGurevich! |
@ilyaGurevich do you need a new npm release for this? |
Not yet, I have a follow-up PR I'd like to open and discuss first before doing npm release! |
This change ensures a flushSync-only behavior when dispatching transactions. Because react 18 automatically batches state updates, we can also remove references in the code where we used unstable_batchedUpdates in general.