Skip to content

Commit

Permalink
Merge pull request #6200 from nextcloud/fix/sync-bugs
Browse files Browse the repository at this point in the history
Fix reconnecting websocket polyfill and error propagation during push
  • Loading branch information
juliusknorr authored Aug 13, 2024
2 parents 9b22e96 + e8ec6ea commit 806bc9d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/components/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -559,14 +559,14 @@ export default {
this.document = document
this.syncError = null
const editable = !this.readOnly
const editable = !this.readOnly && !this.hasConnectionIssue
if (this.$editor.isEditable !== editable) {
this.$editor.setEditable(editable)
}
},
onSync({ steps, document }) {
this.hasConnectionIssue = false
this.hasConnectionIssue = !this.$providers[0].wsconnected || this.$syncService.pushError > 0
this.$nextTick(() => {
this.emit('sync-service:sync')
})
Expand Down
14 changes: 12 additions & 2 deletions src/services/SyncService.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class SyncService {

this.autosave = debounce(this._autosave.bind(this), AUTOSAVE_INTERVAL)

this.pushError = 0

return this
}

Expand All @@ -88,8 +90,12 @@ class SyncService {
return this.#connection.session.guestName
}

get hasActiveConnection() {
return this.#connection && !this.#connection.isClosed
}

async open({ fileId, initialSession }) {
if (this.#connection && !this.#connection.isClosed) {
if (this.hasActiveConnection) {
// We're already connected.
return
}
Expand Down Expand Up @@ -166,6 +172,7 @@ class SyncService {
}
return this.#connection.push(data)
.then((response) => {
this.pushError = 0
this.sending = false
this.emit('sync', {
steps: [],
Expand All @@ -175,6 +182,7 @@ class SyncService {
}).catch(err => {
const { response, code } = err
this.sending = false
this.pushError++
if (!response || code === 'ECONNABORTED') {
this.emit('error', { type: ERROR_TYPE.CONNECTION_FAILED, data: {} })
}
Expand All @@ -191,6 +199,8 @@ class SyncService {
this.emit('error', { type: ERROR_TYPE.PUSH_FAILURE, data: {} })
OC.Notification.showTemporary('Changes could not be sent yet')
}
} else {
this.emit('error', { type: ERROR_TYPE.PUSH_FAILURE, data: {} })
}
throw new Error('Failed to apply steps. Retry!', { cause: err })
})
Expand Down Expand Up @@ -301,7 +311,7 @@ class SyncService {
// Make sure to leave no pending requests behind.
this.autosave.clear()
this.backend?.disconnect()
if (!this.#connection || this.#connection.isClosed) {
if (!this.hasActiveConnection) {
return
}
return this.#connection.close()
Expand Down
8 changes: 7 additions & 1 deletion src/services/WebSocketPolyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ export default function initWebSocketPolyfill(syncService, fileId, initialSessio
this.#notifyPushBus?.on('notify_push', this.#onNotifyPush.bind(this))
this.url = url
logger.debug('WebSocketPolyfill#constructor', { url, fileId, initialSession })
if (syncService.hasActiveConnection) {
setTimeout(() => this.onopen?.(), 0)
}
this.#registerHandlers({
opened: ({ version, session }) => {
this.#version = version
Expand Down Expand Up @@ -88,7 +91,10 @@ export default function initWebSocketPolyfill(syncService, fileId, initialSessio
...queue.filter(s => !outbox.includes(s)),
)
return ret
}, err => logger.error(err))
}, err => {
logger.error(`Failed to push the queue with ${queue.length} steps to the server`, err)
this.onerror?.(err)
})
}

async close() {
Expand Down

0 comments on commit 806bc9d

Please sign in to comment.