Skip to content

Commit da3fcaf

Browse files
max-nextcloudmejo-
authored andcommitted
fix(sync): yjs messages are Uint8Arrays in the queue
Remove duplicate encoding for updateMessage Signed-off-by: Max <[email protected]> Signed-off-by: Jonas <[email protected]>
1 parent 66e5128 commit da3fcaf

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

cypress/component/helpers/yjs.cy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ describe('Yjs base64 wrapped with our helpers', function() {
7070
const stateA = getDocumentState(source)
7171
const update0A = getUpdateMessage(source, state0)
7272
const updateAA = getUpdateMessage(source, stateA)
73-
expect(update0A.length).to.be.eq(40)
73+
expect(update0A.length).to.be.eq(29)
7474
expect(updateAA).to.be.eq(undefined)
7575
})
7676

src/helpers/yjs.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121
*/
2222

2323
import { encodeArrayBuffer, decodeArrayBuffer } from '../helpers/base64.js'
24-
import { Doc, encodeStateAsUpdate, applyUpdate } from 'yjs'
24+
import * as Y from 'yjs'
25+
import * as decoding from 'lib0/decoding.js'
26+
import * as encoding from 'lib0/encoding.js'
27+
import * as syncProtocol from 'y-protocols/sync'
28+
import { messageSync } from 'y-websocket'
2529

2630
/**
2731
* Get Document state encode as base64.
@@ -51,7 +55,7 @@ export function applyDocumentState(ydoc, documentState, origin) {
5155
*
5256
* @param {Y.Doc} ydoc - encode state of this doc
5357
* @param {string} encodedBaseUpdate - base64 encoded doc update to build upon
54-
* @return {string|undefined}
58+
* @return {Uint8Array|undefined}
5559
*/
5660
export function getUpdateMessage(ydoc, encodedBaseUpdate) {
5761
const baseUpdate = decodeArrayBuffer(encodedBaseUpdate)
@@ -65,21 +69,19 @@ export function getUpdateMessage(ydoc, encodedBaseUpdate) {
6569
encoding.writeVarUint(encoder, messageSync)
6670
const update = Y.encodeStateAsUpdate(ydoc, baseStateVector)
6771
syncProtocol.writeUpdate(encoder, update)
68-
const buf = encoding.toUint8Array(encoder)
69-
return encodeArrayBuffer(buf)
72+
return encoding.toUint8Array(encoder)
7073
}
7174

7275
/**
7376
* Apply an updated message to the ydoc.
7477
*
7578
* Only used in tests right now.
7679
* @param {Y.Doc} ydoc - encode state of this doc
77-
* @param {string} updateMessage - base64 encoded y-websocket sync message with update
80+
* @param {Uint8Array} updateMessage - y-websocket sync message with update
7881
* @param {object} origin - initiator object e.g. WebsocketProvider
7982
*/
8083
export function applyUpdateMessage(ydoc, updateMessage, origin = 'origin') {
81-
const updateBuffer = decodeArrayBuffer(updateMessage)
82-
const decoder = decoding.createDecoder(updateBuffer)
84+
const decoder = decoding.createDecoder(updateMessage)
8385
const messageType = decoding.readVarUint(decoder)
8486
if (messageType !== messageSync) {
8587
console.error('y.js update message with invalid type', messageType)
@@ -94,3 +96,13 @@ export function applyUpdateMessage(ydoc, updateMessage, origin = 'origin') {
9496
origin,
9597
)
9698
}
99+
100+
/**
101+
* Helper function to check if two state vectors have the same state
102+
* @param {Array} arr - state vector to compare
103+
* @param {Array} other - state vector to compare against
104+
*/
105+
function sameState(arr, other) {
106+
return arr.length === other.length
107+
&& arr.every((value, index) => other[index] === value)
108+
}

0 commit comments

Comments
 (0)