From 1702eeeec0baaadba12af2b771885df13e3b033b Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 14 Nov 2024 18:49:14 +0100 Subject: [PATCH] fix(sync): make use of steps in push responses The pushed steps are echoed back with all other steps since version immediately. Processing them reduces the size of the following pushes and syncs. Signed-off-by: Max --- src/components/Editor.vue | 4 +++- src/services/SyncService.js | 24 ++++++++++++------------ 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/components/Editor.vue b/src/components/Editor.vue index fc81550499e..65e5db9cf14 100644 --- a/src/components/Editor.vue +++ b/src/components/Editor.vue @@ -592,7 +592,9 @@ export default { this.$nextTick(() => { this.emit('sync-service:sync') }) - this.document = document + if (document) { + this.document = document + } }, onError({ type, data }) { diff --git a/src/services/SyncService.js b/src/services/SyncService.js index 92fa361b129..8b1dee0bd72 100644 --- a/src/services/SyncService.js +++ b/src/services/SyncService.js @@ -171,19 +171,18 @@ class SyncService { this.sending = true clearInterval(this.#sendIntervalId) this.#sendIntervalId = null - const data = getSendable() - if (data.steps.length > 0) { + const sendable = getSendable() + if (sendable.steps.length > 0) { this.emit('stateChange', { dirty: true }) } - return this.#connection.push(data) + return this.#connection.push(sendable) .then((response) => { + const { steps } = response.data this.pushError = 0 this.sending = false - this.emit('sync', { - steps: [], - document: this.#connection.document, - version: this.version, - }) + if (steps?.length > 0) { + this._receiveSteps({ steps }) + } }).catch(err => { const { response, code } = err this.sending = false @@ -194,11 +193,13 @@ class SyncService { if (response?.status === 412) { this.emit('error', { type: ERROR_TYPE.LOAD_ERROR, data: response }) } else if (response?.status === 403) { - if (!data.document) { + // TODO: is this really about sendable? + if (!sendable.document) { // either the session is invalid or the document is read only. logger.error('failed to write to document - not allowed') this.emit('error', { type: ERROR_TYPE.PUSH_FORBIDDEN, data: {} }) } + // TODO: does response.data ever have a document? maybe for errors? // Only emit conflict event if we have synced until the latest version if (response.data.document?.currentVersion === this.version) { this.emit('error', { type: ERROR_TYPE.PUSH_FAILURE, data: {} }) @@ -211,7 +212,7 @@ class SyncService { }) } - _receiveSteps({ steps, document, sessions }) { + _receiveSteps({ steps, document = null, sessions = [] }) { const awareness = sessions .filter(s => s.lastContact > (Math.floor(Date.now() / 1000) - COLLABORATOR_DISCONNECT_TIME)) .filter(s => s.lastAwarenessMessage) @@ -239,8 +240,7 @@ class SyncService { this.lastStepPush = Date.now() this.emit('sync', { steps: newSteps, - // TODO: do we actually need to dig into the connection here? - document: this.#connection.document, + document, version: this.version, }) }