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 binding === null due to asynchronous creation. Fixes #42 #54

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

whawker
Copy link

@whawker whawker commented Jun 18, 2021

This PR fixes an issue with the sync plugin where binding === null leading to issues when calculated relative/absolute positions.

Previously binding was set via an asynchronous transaction (using setTimeout) - anything that occurred before the execution of the callback could error.

This PR changes the sync plugin to PluginState pattern (see here) and uses an init method to pass the view and create the binding.

Comment on lines -102 to -107
if (change !== undefined) {
pluginState = Object.assign({}, pluginState)
for (const key in change) {
pluginState[key] = change[key]
}
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed this, I'm unsure of the larger impact, but everything seems to work fine without it in my project?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other plugins might use this to set properties like snapshot or colorMapping. We can't just remove this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dmonad I have restored this, but dropped the line pluginState = Object.assign({}, pluginState)

I guess this is to make the plugin state immutable? In the example I copied this pattern from, it looks like the plugin apply step returns a new instance of the state to achieve this immutability.

However with the asynchronous setting of the binding using the view, I'm not 100% sure how to achieve this, anybody have any suggestions?

if (this.binding) {
this.binding.destroy()
}
this.binding = null
Copy link
Contributor

@ankon ankon Jun 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly for consistency: Wouldn't hurt to also explicitly init this.binding = null in the constructor?

(Given the below this.binding !== null check -- this may actually be needed, but I don't understand enough about the life-cycle here)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good spot, fixed.

@ronnyroeller
Copy link

@dmonad We're relying on this fix (currently running on a fork of y-prosemirror). Is there a chance to get this PR merged into master?

@whawker
Copy link
Author

whawker commented Aug 31, 2021

@dmonad Any chance you could take a look over this?

@flaviouk
Copy link
Contributor

Can also confirm that this fixes an issue related to the binding null I was having.

Copy link
Member

@dmonad dmonad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @whawker for providing a fix.

However, this PR will definitely break other people's code. The y-sync plugin state is supposed to be an Object (although I agree that it should have been an instance of YSyncState, but that wasn't clear to me at the time when I wrote the code). Also, you break some of the features (e.g. adding/overwriting properties to the y-sync state). Can you please revert the changes that are not necessary for this PR? It should be easy to review for me.

Comment on lines -102 to -107
if (change !== undefined) {
pluginState = Object.assign({}, pluginState)
for (const key in change) {
pluginState[key] = change[key]
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other plugins might use this to set properties like snapshot or colorMapping. We can't just remove this.

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

Successfully merging this pull request may close these issues.

5 participants