Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/sync error cleanup #6202

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 9 additions & 2 deletions src/components/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,13 @@ export default {
subscribe('text:image-node:delete', this.onDeleteImageNode)
this.emit('update:loaded', true)
subscribe('text:translate-modal:show', this.showTranslateModal)

window.addEventListener('beforeunload', (e) => {
if (this.$queue.length > 0) {
e.preventDefault()
e.returnValue = t('text', 'Some changes have not been synced. You might loose your edits when leaving the page.')
}
})
},
created() {
this.$ydoc = new Doc()
Expand Down Expand Up @@ -559,14 +566,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
43 changes: 29 additions & 14 deletions src/components/Editor/Status.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
<template>
<div class="text-editor__session-list">
<div :title="lastSavedStatusTooltip" class="save-status" :class="saveStatusClass">
<NcButton type="tertiary"
<NcButton v-if="statusIndicatorError" type="tertiary">
<template #icon>
<SyncAlertIcon />
</template>
</NcButton>
<NcButton v-else type="tertiary"

Check failure on line 14 in src/components/Editor/Status.vue

View workflow job for this annotation

GitHub Actions / NPM lint

'type' should be on a new line
:aria-label="t('text', 'Save document')"
@click="onClickSave">
<template #icon>
<NcSavingIndicatorIcon :saving="saveStatusClass === 'saving'"
:error="saveStatusClass === 'error'" />
<NcSavingIndicatorIcon :saving="statusIndicatorSaving" />
</template>
</NcButton>
</div>
Expand All @@ -29,6 +33,7 @@
import { ERROR_TYPE } from '../../services/SyncService.js'
import moment from '@nextcloud/moment'
import { NcButton, NcSavingIndicatorIcon } from '@nextcloud/vue'
import SyncAlertIcon from 'vue-material-design-icons/SyncAlert.vue'
import {
useIsMobileMixin,
useIsPublicMixin,
Expand All @@ -40,6 +45,7 @@
name: 'Status',

components: {
SyncAlertIcon,
NcButton,
NcSavingIndicatorIcon,
SessionList: () => import(/* webpackChunkName: "editor-collab" */'./SessionList.vue'),
Expand Down Expand Up @@ -77,14 +83,6 @@
},

computed: {
lastSavedStatus() {
if (this.hasConnectionIssue) {
return this.$isMobile
? t('text', 'Offline')
: t('text', 'Offline, changes will be saved when online')
}
return this.dirtyStateIndicator ? t('text', 'Saving …') : t('text', 'Saved')
},
dirtyStateIndicator() {
return this.dirty || this.hasUnsavedChanges
},
Expand All @@ -93,6 +91,11 @@
if (this.hasSyncCollission) {
message = t('text', 'The document has been changed outside of the editor. The changes cannot be applied.')
}
if (this.hasConnectionIssue) {
return this.$isMobile
? t('text', 'Offline')
: t('text', 'Offline, changes will be saved when online')
}
if (this.dirty || this.hasUnsavedChanges) {
message += ' - ' + t('text', 'Unsaved changes')
}
Expand All @@ -105,11 +108,23 @@
hasSyncCollission() {
return this.syncError && this.syncError.type === ERROR_TYPE.SAVE_COLLISSION
},
saveStatusClass() {
statusIndicatorSaving() {
if (this.syncError && this.lastSavedString !== '') {
return 'error'
return false
}
return this.dirtyStateIndicator ? 'saving' : 'saved'

return this.dirtyStateIndicator
},
statusIndicatorError() {
if (this.syncError && this.lastSavedString !== '') {
return true
}

if (this.hasConnectionIssue) {
return true
}

return false
},
currentSession() {
return Object.values(this.sessions).find((session) => session.isCurrent)
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
Loading