2121 */
2222
2323import { 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 */
5660export 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 */
8083export 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