From ea1a4fb70ecbd923a1e752d0bedad4cfabf0831a 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 | 9 +++++++-- src/services/SyncService.js | 7 ++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/components/Editor.vue b/src/components/Editor.vue index 3546ecf5e24..d3124aa28dc 100644 --- a/src/components/Editor.vue +++ b/src/components/Editor.vue @@ -354,6 +354,7 @@ export default { // }); this.$providers = [] this.$editor = null + this.$api = null this.$syncService = null this.$attachmentResolver = null }, @@ -379,16 +380,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 f424767bebb..669731503d7 100644 --- a/src/services/SyncService.js +++ b/src/services/SyncService.js @@ -10,7 +10,7 @@ import debounce from 'debounce' import PollingBackend from './PollingBackend.js' import Outbox from './Outbox.ts' -import SessionApi, { Connection } from './SessionApi.js' +import { Connection } from './SessionApi.js' import { documentStateToStep } from '../helpers/yjs.js' import { logger } from '../helpers/logger.js' @@ -59,13 +59,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 = [] @@ -124,6 +124,7 @@ class SyncService { this.baseVersionEtag = this.#connection.document.baseVersionEtag this.emit('opened', this.connectionState) this.emit('loaded', this.connectionState) + return this.connectionState }