From 44737f20095f30ef2440f8667a21e21decf9d648 Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 7 Mar 2022 09:13:59 +0100 Subject: refactor: prepare loading content in session creation Simplify the session creation code a bit: * handle the two ways of initializing a sesson only in SyncService. * use async instead of .then(...) * handle errors in helper function. Signed-off-by: Max --- src/components/EditorWrapper.vue | 21 +++++++-------------- src/services/SyncService.js | 39 +++++++++++++++------------------------ 2 files changed, 22 insertions(+), 38 deletions(-) (limited to 'src') diff --git a/src/components/EditorWrapper.vue b/src/components/EditorWrapper.vue index 65cba4a61..e72f769d8 100644 --- a/src/components/EditorWrapper.vue +++ b/src/components/EditorWrapper.vue @@ -463,20 +463,13 @@ export default { this.readOnly = true this.tiptap.setOptions({ editable: !this.readOnly }) }) - if (this.initialSession === null) { - this.syncService.open({ - fileId: this.fileId, - filePath: this.relativePath, - }).catch((e) => { - this.hasConnectionIssue = true - }) - } else { - this.syncService.open({ - initialSession: this.initialSession, - }).catch((e) => { - this.hasConnectionIssue = true - }) - } + this.syncService.open({ + fileId: this.fileId, + filePath: this.relativePath, + initialSession: this.initialSession, + }).catch((e) => { + this.hasConnectionIssue = true + }) this.forceRecreate = false }, diff --git a/src/services/SyncService.js b/src/services/SyncService.js index 651a5ee2b..34d10b15e 100644 --- a/src/services/SyncService.js +++ b/src/services/SyncService.js @@ -97,37 +97,20 @@ class SyncService { } async open({ fileId, filePath, initialSession }) { - let connectionData = null - if (typeof initialSession === 'undefined') { - try { - const response = await this._openDocument({ fileId, filePath }) - connectionData = response.data - } catch (error) { - if (!error.response || error.code === 'ECONNABORTED') { - this.emit('error', ERROR_TYPE.CONNECTION_FAILED, {}) - } else { - this.emit('error', ERROR_TYPE.LOAD_ERROR, error.response.status) - } - throw error - } - } else { - connectionData = initialSession - } - + const connectionData = initialSession + || await this._openDocument({ fileId, filePath }) this.document = connectionData.document this.document.readOnly = connectionData.readOnly this.session = connectionData.session - this.emit('opened', { document: this.document, session: this.session, }) - return this._fetchDocument().then(({ data }) => { - this.emit('loaded', { - document: this.document, - session: this.session, - documentSource: '' + data, - }) + const fetched = await this._fetchDocument() + this.emit('loaded', { + document: this.document, + session: this.session, + documentSource: '' + fetched.data, }) } @@ -143,6 +126,14 @@ class SyncService { guestName: this.options.guestName, forceRecreate: this.options.forceRecreate, }) + .then(response => response.data, error => { + if (!error.response || error.code === 'ECONNABORTED') { + this.emit('error', ERROR_TYPE.CONNECTION_FAILED, {}) + } else { + this.emit('error', ERROR_TYPE.LOAD_ERROR, error.response.status) + } + throw error + }) } _fetchDocument() { -- cgit v1.2.3