Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: emit onerror for websocket and reopen connection on reconnect at…
…tempts of y-websocket

Signed-off-by: Julius Härtl <[email protected]>
  • Loading branch information
juliusknorr authored and mejo- committed Aug 28, 2024
commit e450663f8142a33b7abb8220ccce0bc795a5db93
8 changes: 6 additions & 2 deletions src/services/SyncService.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,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 @@ -320,7 +324,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 @@ -47,6 +47,9 @@ export default function initWebSocketPolyfill(syncService, fileId, initialSessio
constructor(url) {
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 @@ -101,7 +104,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