Skip to content

Commit 7085dc6

Browse files
max-nextcloudmejo-
authored andcommitted
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 <max@nextcloud.com> Signed-off-by: Jonas <jonas@freesources.org>
1 parent ee3893d commit 7085dc6

2 files changed

Lines changed: 24 additions & 6 deletions

File tree

src/services/SyncService.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,16 @@ class SyncService {
152152
return new Promise((resolve, reject) => {
153153
this.#sendIntervalId = setInterval(() => {
154154
if (this.connection && !this.sending) {
155-
clearInterval(this.#sendIntervalId)
156-
this.#sendIntervalId = null
157-
this._sendSteps(getSendable).then(resolve).catch(reject)
155+
this.sendStepsNow(getSendable).then(resolve).catch(reject)
158156
}
159157
}, 200)
160158
})
161159
}
162160

163-
_sendSteps(getSendable) {
161+
sendStepsNow(getSendable) {
164162
this.sending = true
163+
clearInterval(this.#sendIntervalId)
164+
this.#sendIntervalId = null
165165
const data = getSendable()
166166
if (data.steps.length > 0) {
167167
this.emit('stateChange', { dirty: true })

src/services/WebSocketPolyfill.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export default function initWebSocketPolyfill(syncService, fileId, initialSessio
8282
send(...data) {
8383
this.#queue.push(...data)
8484
let outbox = []
85-
syncService.sendSteps(() => {
85+
return syncService.sendSteps(() => {
8686
outbox = [...this.#queue]
8787
const data = {
8888
steps: this.#steps,
@@ -109,7 +109,8 @@ export default function initWebSocketPolyfill(syncService, fileId, initialSessio
109109
.findLast(s => s > 'AQ') || ''
110110
}
111111

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

123+
#sendRemainingSteps() {
124+
if (this.#queue.length) {
125+
return syncService.sendStepsNow(() => {
126+
const data = {
127+
steps: this.#steps,
128+
awareness: this.#awareness,
129+
version: this.#version,
130+
}
131+
this.#queue = []
132+
logger.debug('sending final steps ', data)
133+
return data
134+
})?.catch(err => {
135+
logger.error(err)
136+
})
137+
}
138+
}
139+
122140
}
123141
}

0 commit comments

Comments
 (0)