From b7626c96efd3db59e5018081ffd769a2c0c3005a Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 5 Nov 2024 12:47:57 +0100 Subject: [PATCH] refactor(Editor): instantiate api outside sync service Signed-off-by: Max --- src/components/Editor.vue | 10 ++++++++-- src/services/SyncService.js | 7 ++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/components/Editor.vue b/src/components/Editor.vue index f244557a43d..66cfa0139e2 100644 --- a/src/components/Editor.vue +++ b/src/components/Editor.vue @@ -108,6 +108,7 @@ import ReadonlyBar from './Menu/ReadonlyBar.vue' import { logger } from '../helpers/logger.js' import { getDocumentState, applyDocumentState } from '../helpers/yjs.js' import { SyncService, ERROR_TYPE, IDLE_TIMEOUT } from './../services/SyncService.js' +import SessionApi from '../services/SessionApi.js' import createSyncServiceProvider from './../services/SyncServiceProvider.js' import AttachmentResolver from './../services/AttachmentResolver.js' import { extensionHighlight } from '../helpers/mappings.js' @@ -361,6 +362,7 @@ export default { // }); this.$providers = [] this.$editor = null + this.$api = null this.$syncService = null this.$attachmentResolver = null }, @@ -385,16 +387,20 @@ export default { } const guestName = localStorage.getItem('nick') ? localStorage.getItem('nick') : '' - this.$syncService = new SyncService({ + this.$api = new SessionApi({ guestName, shareToken: this.shareToken, filePath: this.relativePath, - baseVersionEtag: this.$baseVersionEtag, forceRecreate: this.forceRecreate, + }) + + this.$syncService = new SyncService({ + baseVersionEtag: this.$baseVersionEtag, serialize: this.isRichEditor ? (content) => createMarkdownSerializer(this.$editor.schema).serialize(content ?? this.$editor.state.doc) : (content) => serializePlainText(content ?? this.$editor.state.doc), getDocumentState: () => getDocumentState(this.$ydoc), + api: this.$api, }) this.listenSyncServiceEvents() diff --git a/src/services/SyncService.js b/src/services/SyncService.js index 7b408548a75..85d240b9eeb 100644 --- a/src/services/SyncService.js +++ b/src/services/SyncService.js @@ -25,7 +25,7 @@ import debounce from 'debounce' import PollingBackend from './PollingBackend.js' import Outbox from './Outbox.js' -import SessionApi, { Connection } from './SessionApi.js' +import { Connection } from './SessionApi.js' import { documentStateToStep } from '../helpers/yjs.js' import { logger } from '../helpers/logger.js' @@ -74,13 +74,13 @@ class SyncService { #connection #outbox = new Outbox() - constructor({ baseVersionEtag, serialize, getDocumentState, ...options }) { + constructor({ baseVersionEtag, serialize, getDocumentState, api }) { /** @type {import('mitt').Emitter} _bus */ this._bus = mitt() this.serialize = serialize this.getDocumentState = getDocumentState - this._api = new SessionApi(options) + this._api = api this.#connection = null this.stepClientIDs = [] @@ -139,6 +139,7 @@ class SyncService { this.baseVersionEtag = this.#connection.document.baseVersionEtag this.emit('opened', this.connectionState) this.emit('loaded', this.connectionState) + return this.connectionState }