Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix(api): send remaining steps before closing connection
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 committed Oct 30, 2023
commit d89971462e6233fa0d24be39bfa903c79f6cb6d2
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 @@ -85,7 +85,7 @@ export default function initWebSocketPolyfill(syncService, fileId, initialSessio

this.#queue.push(...data)
let outbox = []
syncService.sendSteps(() => {
return syncService.sendSteps(() => {
outbox = [...this.#queue]
const data = {
steps: this.#steps,
Expand All @@ -112,7 +112,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 @@ -122,5 +123,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)
})
}
}

}
}