Skip to content

Commit

Permalink
fix: emit onerror for websocket and reopen connection on reconnect at…
Browse files Browse the repository at this point in the history
…tempts of y-websocket

Signed-off-by: Julius Härtl <[email protected]>
  • Loading branch information
juliusknorr committed Aug 13, 2024
1 parent 5360c23 commit 4f310a5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/services/SyncService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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()
Expand Down
8 changes: 7 additions & 1 deletion src/services/WebSocketPolyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 4f310a5

Please sign in to comment.