feat: add beforeSync hook
#919
Merged
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.
This change introduces a
beforeSynchook which is invoked just before the Y.js update is applied to the Y doc.This is different from the existing
onChangehook in that the update has not yet been applied to the document.We have a use-case where we want to inspect the contents of an update before it is actually applied to the Y doc.
This specifically would enable us to revert changes to the document, if we consider them to be invalid, you can read more information here: TypeCellOS/BlockNote-demo-nextjs-hocuspocus#1
We were able to use the
beforeHandleMessagehook for this (implemented here), but it required us to skip over parts of the hocuspocus binary message format, which we felt was brittle.This
beforeSynchook allows us to inspect the Y.js update at the y-sync/protocols level which would be ideal context to revert messages with.