Skip to content

Commit

Permalink
fix(sync): make use of steps in push responses
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
max-nextcloud committed Nov 14, 2024
1 parent b31e1e5 commit 4b6666e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
4 changes: 3 additions & 1 deletion src/components/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,9 @@ export default {
this.$nextTick(() => {
this.emit('sync-service:sync')
})
this.document = document
if (document) {
this.document = document
}
},
onError({ type, data }) {
Expand Down
22 changes: 11 additions & 11 deletions src/services/SyncService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
// TODO: is this really about sendable?
if (!data.document) {

Check failure on line 197 in src/services/SyncService.js

View workflow job for this annotation

GitHub Actions / NPM lint

'data' is not defined
// 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: {} })
Expand All @@ -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)
Expand Down Expand Up @@ -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,
})
}
Expand Down

0 comments on commit 4b6666e

Please sign in to comment.