diff --git a/src/services/SyncService.js b/src/services/SyncService.js index 2fa7ac317cb..cb4b4eb4d81 100644 --- a/src/services/SyncService.js +++ b/src/services/SyncService.js @@ -90,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 } @@ -305,7 +309,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() diff --git a/src/services/WebSocketPolyfill.js b/src/services/WebSocketPolyfill.js index df5a28c25b6..474ecc89d41 100644 --- a/src/services/WebSocketPolyfill.js +++ b/src/services/WebSocketPolyfill.js @@ -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 @@ -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() {