Skip to content

Conversation

@abhi-arya1
Copy link
Contributor

@abhi-arya1 abhi-arya1 commented Aug 28, 2025

Problem

For applications that provide version restoration functionality such as Google Docs, they require replacement of the document content. Other attempts to solve this previously resulted in a bug based on how Y.js works, where the document that's stored includes clock information about updates, so when it's applied on top of a newer doc, the algorithm simply discards it because it already has a newer version.

Resolution

Implemented and documented the replaceDocument method in YServer to allow changing document states without removing connections or other data using Y.js's UndoManager, and document snapshot information.

This can be tested using the drop-in replacement for fixtures/tiptap-yjs/src/client/index.tsx available here: https://abhi-arya.opennote.space/4b830300-a8a7-4015-a39b-48a9c68a2170 (this also contains a video example of a use case that i'm using in prod)

Side Effects

This required removing the readonly property on WSSharedDoc to allow for replacements.

Other References

@changeset-bot
Copy link

changeset-bot bot commented Aug 28, 2025

🦋 Changeset detected

Latest commit: 3053118

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

This PR includes changesets to release 2 packages
Name Type
y-partyserver Patch
@partyserver/fixture-tiptap-yjs 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

@abhi-arya1 abhi-arya1 marked this pull request as draft August 28, 2025 18:01
@abhi-arya1 abhi-arya1 changed the title Add base64 utility functions and implement document state replacement feat: Add Document State Replacement for document-versioning based applications to Y-Partykit Aug 28, 2025
@abhi-arya1 abhi-arya1 marked this pull request as ready for review August 28, 2025 23:51
@abhi-arya1 abhi-arya1 changed the title feat: Add Document State Replacement for document-versioning based applications to Y-Partykit feat: Add Document State Replacement for document-versioning based applications to y-partykit Aug 29, 2025
abhi-arya1 and others added 10 commits September 2, 2025 14:48
… in y-partyserver

- Introduced `base64ToUint8Array` and `uint8ArrayToBase64` functions in `utils.ts` for base64 encoding/decoding.
- Added `replaceDocumentState` method in `index.ts` to handle document state updates using base64 encoded data.
- Updated `package.json` to include `@types/node` and changed `partyserver` dependency to use workspace.
- Created new `bun.lockb` file.
- Removed the `base64ToUint8Array` and `uint8ArrayToBase64` functions from `utils.ts` and added them to a new `shared/utils.ts` file for better organization.
- Implemented a `revertUpdate` method in `YServer` to allow reverting document states using Yjs's UndoManager.
- Cleaned up the `replaceDocumentState` method, which is no longer present in the codebase.
- Updated the `document` property in `YServer` to be explicitly typed as `WSSharedDoc`.
- Renamed `revertUpdate` method to `replaceDocument` to better reflect its functionality of replacing the document with a previous state.
- Updated method documentation to clarify parameters and error messages related to document replacement.
- Modified the comment for the document replacement method to clarify that it replaces the document with a different state instead of a previous state, enhancing the accuracy of the method's description.
@threepointone
Copy link
Collaborator

looks like you don't need to remove readonly, so I'm bringing it back?

@threepointone threepointone merged commit a80231d into cloudflare:main Sep 2, 2025
1 check passed
@threepointone threepointone mentioned this pull request Sep 2, 2025
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.

2 participants