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(react): always attempt to cleanup editor instances, starting from creation #5492 #5496

Merged
merged 1 commit into from
Aug 15, 2024

Conversation

nperez0111
Copy link
Contributor

@nperez0111 nperez0111 commented Aug 15, 2024

Changes Overview

React.StrictMode has made the way that we create editor instances very strange. There is a tension where React wants for us to create side-effects only within a useEffect so the lifecycle that StrictMode goes through is very strange:

  • A render pass
  • A render pass
  • Call useEffects
  • Call cleanup on useEffects
  • Call useEffects again

This is meant to understand whether the functions are indeed memory safe, which I can appreciate but it is very different behavior from what it actually does at runtime:

  • A render pass
  • Call useEffects
  • Call cleanup on useEffects
  • A render pass
  • Call useEffects
  • Call cleanup on useEffects

So this change makes it safe to run useEditor in StrictMode, our test suite was not previously set up to test StrictMode behavior so it was missed in original development.

Implementation Approach

The core of the change ended up being quite simple, because we can create the editor within the first render, we need to already schedule it's destruction.
Scheduling a destruction, ensures that an instance that was created in that first render pass can be cleaned up.
Waiting one more tick than before ensures that we don't accidentally destroy an editor instance that could actually be valid in the next render pass.

In StrictMode, there will be two editor instances created, the first will be created & quickly destroyed in 2 ticks.
In Normal React, there will only ever be 1 instance created and destroyed only on unmount.

Testing Done

Lots of manual testing and console logging, we are not set up in a way to test React rendering behavior so tests could not be written

Verification Steps

Collaboration Cursors are a great way to test this, because if two instances are active attached to the same ydoc, their clientids will be the same and it will constantly try to send awareness updates even though they are attached to the same instance.
This manifests itself visually too by constantly flickering.

The silver lining with the original bug is that when React was running in normal mode (like in prod), the behavior was actually working properly, it was only an issue in StrictMode

Additional Notes

Checklist

  • I have created a changeset for this PR if necessary.
  • My changes do not break the library.
  • I have added tests where applicable.
  • I have followed the project guidelines.
  • I have fixed any lint issues.

Related Issues

#5492

Copy link

changeset-bot bot commented Aug 15, 2024

🦋 Changeset detected

Latest commit: 4a94b17

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 54 packages
Name Type
@tiptap/react Patch
@tiptap/core Patch
@tiptap/extension-blockquote Patch
@tiptap/extension-bold Patch
@tiptap/extension-bubble-menu Patch
@tiptap/extension-bullet-list Patch
@tiptap/extension-character-count Patch
@tiptap/extension-code-block-lowlight Patch
@tiptap/extension-code-block Patch
@tiptap/extension-code Patch
@tiptap/extension-collaboration-cursor Patch
@tiptap/extension-collaboration Patch
@tiptap/extension-color Patch
@tiptap/extension-document Patch
@tiptap/extension-dropcursor Patch
@tiptap/extension-floating-menu Patch
@tiptap/extension-focus Patch
@tiptap/extension-font-family Patch
@tiptap/extension-gapcursor Patch
@tiptap/extension-hard-break Patch
@tiptap/extension-heading Patch
@tiptap/extension-highlight Patch
@tiptap/extension-history Patch
@tiptap/extension-horizontal-rule Patch
@tiptap/extension-image Patch
@tiptap/extension-italic Patch
@tiptap/extension-link Patch
@tiptap/extension-list-item Patch
@tiptap/extension-list-keymap Patch
@tiptap/extension-mention Patch
@tiptap/extension-ordered-list Patch
@tiptap/extension-paragraph Patch
@tiptap/extension-placeholder Patch
@tiptap/extension-strike Patch
@tiptap/extension-subscript Patch
@tiptap/extension-superscript Patch
@tiptap/extension-table-cell Patch
@tiptap/extension-table-header Patch
@tiptap/extension-table-row Patch
@tiptap/extension-table Patch
@tiptap/extension-task-item Patch
@tiptap/extension-task-list Patch
@tiptap/extension-text-align Patch
@tiptap/extension-text-style Patch
@tiptap/extension-text Patch
@tiptap/extension-typography Patch
@tiptap/extension-underline Patch
@tiptap/extension-youtube Patch
@tiptap/html Patch
@tiptap/pm Patch
@tiptap/starter-kit Patch
@tiptap/suggestion Patch
@tiptap/vue-2 Patch
@tiptap/vue-3 Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

netlify bot commented Aug 15, 2024

Deploy Preview for tiptap-embed ready!

Name Link
🔨 Latest commit 4a94b17
🔍 Latest deploy log https://app.netlify.com/sites/tiptap-embed/deploys/66bda4b2ad96f80008476419
😎 Deploy Preview https://deploy-preview-5496--tiptap-embed.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@nperez0111 nperez0111 merged commit 6a0f4f3 into develop Aug 15, 2024
14 checks passed
@nperez0111 nperez0111 deleted the fix/5492 branch August 15, 2024 06:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Archived in project
Development

Successfully merging this pull request may close these issues.

1 participant