Skip to content

Commit

Permalink
fix(sync): yjs messages are Uint8Arrays in the queue
Browse files Browse the repository at this point in the history
Remove duplicate encoding for updateMessage

Signed-off-by: Max <max@nextcloud.com>
Signed-off-by: Jonas <jonas@freesources.org>
  • Loading branch information
max-nextcloud authored and mejo- committed Jan 29, 2024
1 parent 83816af commit 40ef1aa
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cypress/component/helpers/yjs.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe('Yjs base64 wrapped with our helpers', function() {
const stateA = getDocumentState(source)
const update0A = getUpdateMessage(source, state0)
const updateAA = getUpdateMessage(source, stateA)
expect(update0A.length).to.be.eq(40)
expect(update0A.length).to.be.eq(29)
expect(updateAA).to.be.eq(undefined)
})

Expand Down
20 changes: 14 additions & 6 deletions src/helpers/yjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function applyDocumentState(ydoc, documentState, origin) {
*
* @param {Y.Doc} ydoc - encode state of this doc

Check warning on line 52 in src/helpers/yjs.js

View workflow job for this annotation

GitHub Actions / eslint

The type 'Y' is undefined
* @param {string} encodedBaseUpdate - base64 encoded doc update to build upon
* @return {string|undefined}
* @return {Uint8Array|undefined}
*/
export function getUpdateMessage(ydoc, encodedBaseUpdate) {
const baseUpdate = decodeArrayBuffer(encodedBaseUpdate)
Expand All @@ -65,21 +65,19 @@ export function getUpdateMessage(ydoc, encodedBaseUpdate) {
encoding.writeVarUint(encoder, messageSync)

Check failure on line 65 in src/helpers/yjs.js

View workflow job for this annotation

GitHub Actions / eslint

'encoding' is not defined

Check failure on line 65 in src/helpers/yjs.js

View workflow job for this annotation

GitHub Actions / eslint

'messageSync' is not defined
const update = Y.encodeStateAsUpdate(ydoc, baseStateVector)
syncProtocol.writeUpdate(encoder, update)
const buf = encoding.toUint8Array(encoder)
return encodeArrayBuffer(buf)
return encoding.toUint8Array(encoder)
}

/**
* Apply an updated message to the ydoc.
*
* Only used in tests right now.
* @param {Y.Doc} ydoc - encode state of this doc

Check warning on line 75 in src/helpers/yjs.js

View workflow job for this annotation

GitHub Actions / eslint

The type 'Y' is undefined
* @param {string} updateMessage - base64 encoded y-websocket sync message with update
* @param {Uint8Array} updateMessage - y-websocket sync message with update
* @param {object} origin - initiator object e.g. WebsocketProvider
*/
export function applyUpdateMessage(ydoc, updateMessage, origin = 'origin') {
const updateBuffer = decodeArrayBuffer(updateMessage)
const decoder = decoding.createDecoder(updateBuffer)
const decoder = decoding.createDecoder(updateMessage)
const messageType = decoding.readVarUint(decoder)
if (messageType !== messageSync) {
console.error('y.js update message with invalid type', messageType)
Expand All @@ -94,3 +92,13 @@ export function applyUpdateMessage(ydoc, updateMessage, origin = 'origin') {
origin,
)
}

/**
* Helper function to check if two state vectors have the same state
* @param {Array} arr - state vector to compare
* @param {Array} other - state vector to compare against
*/
function sameState(arr, other) {
return arr.length === other.length
&& arr.every((value, index) => other[index] === value)
}

0 comments on commit 40ef1aa

Please sign in to comment.