diff --git a/src/helpers/yjs.js b/src/helpers/yjs.js index a34331d2ebb..869626b693d 100644 --- a/src/helpers/yjs.js +++ b/src/helpers/yjs.js @@ -97,6 +97,26 @@ export function applyUpdateMessage(ydoc, updateMessage, origin = 'origin') { ) } +/** + * Get the steps for sending to the server + * + * @param {object[]} queue - queue for the outgoing steps + */ +export function getSteps(queue) { + return queue.map(s => encodeArrayBuffer(s)) + .filter(s => s < 'AQ') +} + +/** + * Encode the latest awareness message for sending + * + * @param {object[]} queue - queue for the outgoing steps + */ +export function getAwareness(queue) { + return queue.map(s => encodeArrayBuffer(s)) + .findLast(s => s > 'AQ') || '' +} + /** * Log y.js messages with their type and initiator call stack * diff --git a/src/services/SyncService.js b/src/services/SyncService.js index fb2ca94d9d5..d58a6b18c80 100644 --- a/src/services/SyncService.js +++ b/src/services/SyncService.js @@ -25,7 +25,7 @@ import debounce from 'debounce' import PollingBackend from './PollingBackend.js' import SessionApi, { Connection } from './SessionApi.js' -import { encodeArrayBuffer } from '../helpers/base64.ts' +import { getSteps, getAwareness } from '../helpers/yjs.js' import { logger } from '../helpers/logger.js' /** @@ -291,10 +291,8 @@ class SyncService { return } let outbox = [] - const steps = queue.map(s => encodeArrayBuffer(s)) - .filter(s => s < 'AQ') - const awareness = queue.map(s => encodeArrayBuffer(s)) - .findLast(s => s > 'AQ') || '' + const steps = getSteps(queue) + const awareness = getAwareness(queue) return this.sendStepsNow(() => { const data = { steps, awareness, version: this.version } outbox = [...queue] diff --git a/src/services/WebSocketPolyfill.js b/src/services/WebSocketPolyfill.js index b1214b9f8f3..efc84adfa0b 100644 --- a/src/services/WebSocketPolyfill.js +++ b/src/services/WebSocketPolyfill.js @@ -86,8 +86,8 @@ export default function initWebSocketPolyfill(syncService, fileId, initialSessio let outbox = [] return syncService.sendSteps(() => { const data = { - steps: this.#steps, - awareness: this.#awareness, + steps: getSteps(queue), + awareness: getAwareness(queue), version: this.#version, } outbox = [...queue] @@ -103,16 +103,6 @@ export default function initWebSocketPolyfill(syncService, fileId, initialSessio }, err => logger.error(err)) } - get #steps() { - return queue.map(s => encodeArrayBuffer(s)) - .filter(s => s < 'AQ') - } - - get #awareness() { - return queue.map(s => encodeArrayBuffer(s)) - .findLast(s => s > 'AQ') || '' - } - async close() { Object.entries(this.#handlers) .forEach(([key, value]) => syncService.off(key, value))