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

feat(session): Send save request via sendBeacon at beforeunload #6798

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions src/components/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,10 @@
) {
this.dirty = state.dirty
if (this.dirty) {
window.addEventListener('beforeunload', this.saveBeforeUnload)

Check warning on line 681 in src/components/Editor.vue

View check run for this annotation

Codecov / codecov/patch

src/components/Editor.vue#L681

Added line #L681 was not covered by tests
this.$syncService.autosave()
} else {
window.removeEventListener('beforeunload', this.saveBeforeUnload)

Check warning on line 684 in src/components/Editor.vue

View check run for this annotation

Codecov / codecov/patch

src/components/Editor.vue#L683-L684

Added lines #L683 - L684 were not covered by tests
}
}
}
Expand Down Expand Up @@ -887,6 +890,13 @@
updateEditorWidth(newWidth) {
document.documentElement.style.setProperty('--text-editor-max-width', newWidth)
},

async saveBeforeUnload(event) {
if (!this.dirty && !this.document.hasUnsavedChanges) {
return
}
await this.$syncService.saveNoWait()
},

Check warning on line 899 in src/components/Editor.vue

View check run for this annotation

Codecov / codecov/patch

src/components/Editor.vue#L894-L899

Added lines #L894 - L899 were not covered by tests
},
}
</script>
Expand Down
7 changes: 2 additions & 5 deletions src/components/Editor/Status.vue
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,19 @@
return this.dirtyStateIndicator ? t('text', 'Saving …') : t('text', 'Saved')
},
dirtyStateIndicator() {
return this.dirty || this.hasUnsavedChanges
return this.dirty || this.document.hasUnsavedChanges

Check warning on line 92 in src/components/Editor/Status.vue

View check run for this annotation

Codecov / codecov/patch

src/components/Editor/Status.vue#L92

Added line #L92 was not covered by tests
},
lastSavedStatusTooltip() {
let message = t('text', 'Last saved {lastSave}', { lastSave: this.lastSavedString })
if (this.hasSyncCollission) {
message = t('text', 'The document has been changed outside of the editor. The changes cannot be applied.')
}
if (this.dirty || this.hasUnsavedChanges) {
if (this.dirty || this.document.hasUnsavedChanges) {

Check warning on line 99 in src/components/Editor/Status.vue

View check run for this annotation

Codecov / codecov/patch

src/components/Editor/Status.vue#L99

Added line #L99 was not covered by tests
message += ' - ' + t('text', 'Unsaved changes')
}
return message
},

hasUnsavedChanges() {
return this.document && this.document.lastSavedVersion < this.document.currentVersion
},
hasSyncCollission() {
return this.syncError && this.syncError.type === ERROR_TYPE.SAVE_COLLISSION
},
Expand Down
19 changes: 16 additions & 3 deletions src/services/SessionApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import axios from '@nextcloud/axios'
import { getRequestToken } from '@nextcloud/auth'
import { generateUrl } from '@nextcloud/router'

export class ConnectionClosedError extends Error {
Expand Down Expand Up @@ -88,6 +89,10 @@
return this.closed
}

get hasUnsavedChanges() {
return this.#document && this.#document.lastSavedVersion < this.#document.currentVersion
}

Check warning on line 94 in src/services/SessionApi.js

View check run for this annotation

Codecov / codecov/patch

src/services/SessionApi.js#L92-L94

Added lines #L92 - L94 were not covered by tests

get #defaultParams() {
return {
documentId: this.#document.id,
Expand All @@ -106,8 +111,9 @@
})
}

save({ version, autosaveContent, documentState, force, manualSave }) {
return this.#post(this.#url(`session/${this.#document.id}/save`), {
save({ version, autosaveContent, documentState, force, manualSave, useSendBeacon = false }) {
const url = this.#url(`session/${this.#document.id}/save`)
const data = {

Check warning on line 116 in src/services/SessionApi.js

View check run for this annotation

Codecov / codecov/patch

src/services/SessionApi.js#L114-L116

Added lines #L114 - L116 were not covered by tests
...this.#defaultParams,
filePath: this.#options.filePath,
baseVersionEtag: this.#document.baseVersionEtag,
Expand All @@ -116,7 +122,14 @@
documentState,
force,
manualSave,
})
}

Check warning on line 125 in src/services/SessionApi.js

View check run for this annotation

Codecov / codecov/patch

src/services/SessionApi.js#L125

Added line #L125 was not covered by tests

if (useSendBeacon) {
data.requesttoken = getRequestToken() ?? ''
const blob = new Blob([JSON.stringify(data)], { type: 'application/json' })
return navigator.sendBeacon(url, blob)
}
return this.#post(url, data)

Check warning on line 132 in src/services/SessionApi.js

View check run for this annotation

Codecov / codecov/patch

src/services/SessionApi.js#L127-L132

Added lines #L127 - L132 were not covered by tests
}

push({ steps, version, awareness }) {
Expand Down
12 changes: 12 additions & 0 deletions src/services/SyncService.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,18 @@
}
}

async saveNoWait() {
await this.#connection.save({
version: this.version,
autosaveContent: this._getContent(),
documentState: this.getDocumentState(),
force: false,
manualSave: true,
useSendBeacon: true,
})
logger.debug('[SyncService] saved using sendBeacon (nowait)')
}

Check warning on line 312 in src/services/SyncService.js

View check run for this annotation

Codecov / codecov/patch

src/services/SyncService.js#L302-L312

Added lines #L302 - L312 were not covered by tests

forceSave() {
return this.save({ force: true })
}
Expand Down
Loading