Skip to content

Commit

Permalink
refactor(WebSocketPolyfill): cleanup redundant open handling
Browse files Browse the repository at this point in the history
Remove return value from `syncService.open`
and redundant handler in `WebSocketPolyfill`.

`syncService.open` triggers `opened` and `loaded` events
which both set the `version` and `session`.

Signed-off-by: Max <[email protected]>
  • Loading branch information
max-nextcloud committed Oct 1, 2024
1 parent a6c13a0 commit 22f72b8
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 18 deletions.
6 changes: 2 additions & 4 deletions src/services/SyncService.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class SyncService {

async open({ fileId, initialSession }) {
if (this.hasActiveConnection) {
return this.connectionState
return
}
const connect = initialSession
? Promise.resolve(new Connection({ data: initialSession }, {}))
Expand All @@ -115,15 +115,13 @@ class SyncService {
this.#connection = await connect
if (!this.#connection) {
// Error was already emitted in connect
return null
return
}
this.backend = new PollingBackend(this, this.#connection)
this.version = this.#connection.docStateVersion
this.baseVersionEtag = this.#connection.document.baseVersionEtag
this.emit('opened', this.connectionState)
this.emit('loaded', this.connectionState)

return this.connectionState
}

startSync() {
Expand Down
15 changes: 1 addition & 14 deletions src/services/WebSocketPolyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ export default function initWebSocketPolyfill(syncService, fileId, initialSessio
this.#version = version
this.#session = session
},
loaded: ({ version, session, content }) => {
logger.debug('loaded ', { version, session })
this.#version = version
this.#session = session
},
sync: ({ steps, version }) => {
logger.debug('synced ', { version, steps })
this.#version = version
Expand All @@ -57,15 +52,7 @@ export default function initWebSocketPolyfill(syncService, fileId, initialSessio
},
})

syncService.open({ fileId, initialSession }).then((data) => {
if (syncService.hasActiveConnection) {
const { version, session } = data
this.#version = version
this.#session = session

this.onopen?.()
}
})
syncService.open({ fileId, initialSession }).then(() => this.onopen?.())
}

#registerHandlers(handlers) {
Expand Down

0 comments on commit 22f72b8

Please sign in to comment.