Skip to content

Commit

Permalink
fix(api): send remaining steps before closing connection
Browse files Browse the repository at this point in the history
y.js will send a last awareness update when closing the websocket.
Try to get this out before closing the sync service connection.

Signed-off-by: Max <[email protected]>
  • Loading branch information
max-nextcloud authored and mejo- committed Nov 7, 2023
1 parent 9521d26 commit 662add0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/services/SyncService.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,16 @@ class SyncService {
return new Promise((resolve, reject) => {
this.#sendIntervalId = setInterval(() => {
if (this.connection && !this.sending) {
clearInterval(this.#sendIntervalId)
this.#sendIntervalId = null
this._sendSteps(getSendable).then(resolve).catch(reject)
this.sendStepsNow(getSendable).then(resolve).catch(reject)
}
}, 200)
})
}

_sendSteps(getSendable) {
sendStepsNow(getSendable) {
this.sending = true
clearInterval(this.#sendIntervalId)
this.#sendIntervalId = null
const data = getSendable()
if (data.steps.length > 0) {
this.emit('stateChange', { dirty: true })
Expand Down
22 changes: 20 additions & 2 deletions src/services/WebSocketPolyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export default function initWebSocketPolyfill(syncService, fileId, initialSessio
send(...data) {
this.#queue.push(...data)
let outbox = []
syncService.sendSteps(() => {
return syncService.sendSteps(() => {
outbox = [...this.#queue]
const data = {
steps: this.#steps,
Expand All @@ -109,7 +109,8 @@ export default function initWebSocketPolyfill(syncService, fileId, initialSessio
.findLast(s => s > 'AQ') || ''
}

close() {
async close() {
await this.#sendRemainingSteps()
Object.entries(this.#handlers)
.forEach(([key, value]) => syncService.off(key, value))
this.#handlers = []
Expand All @@ -119,5 +120,22 @@ export default function initWebSocketPolyfill(syncService, fileId, initialSessio
logger.debug('Websocket closed')
}

#sendRemainingSteps() {
if (this.#queue.length) {
return syncService.sendStepsNow(() => {
const data = {
steps: this.#steps,
awareness: this.#awareness,
version: this.#version,
}
this.#queue = []
logger.debug('sending final steps ', data)
return data
})?.catch(err => {
logger.error(err)
})
}
}

}
}

0 comments on commit 662add0

Please sign in to comment.