Skip to content

Commit 6ff2c13

Browse files
committed
chore(refactor): handle open data in websocket polyfill
Signed-off-by: Max <max@nextcloud.com>
1 parent 970d9bb commit 6ff2c13

3 files changed

Lines changed: 21 additions & 8 deletions

File tree

src/helpers/yjs.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as decoding from 'lib0/decoding.js'
77
import * as encoding from 'lib0/encoding.js'
88
import * as syncProtocol from 'y-protocols/sync'
99
import * as Y from 'yjs'
10+
import type { OpenData } from '../apis/connect'
1011
import type { Step } from '../services/SyncService'
1112
import { messageSync } from '../services/y-websocket.js'
1213
import { decodeArrayBuffer, encodeArrayBuffer } from './base64'
@@ -37,6 +38,21 @@ export function applyDocumentState(
3738
Y.applyUpdate(ydoc, update, origin)
3839
}
3940

41+
/**
42+
* Create a steps from the open response
43+
* i.e. create a sync protocol update message from the document state
44+
* and encode it and wrap it in a step data structure.
45+
*
46+
* @param data - data returned by the open request
47+
* @return steps extracted from the open data.
48+
*/
49+
export function stepsFromOpenData(data: OpenData): Step[] {
50+
if (!data.documentState) {
51+
return []
52+
}
53+
return [documentStateToStep(data.documentState, data.document.lastSavedVersion)]
54+
}
55+
4056
/**
4157
* Create a step from a document state
4258
* i.e. create a sync protocol update message from it

src/services/SyncService.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,6 @@ class SyncService {
176176
this.backend = new PollingBackend(this, this.connection.value, data)
177177
// Make sure to only emit this once the backend is in place.
178178
this.bus.emit('opened', data)
179-
// Emit sync after opened, so websocket onmessage comes after onopen.
180-
if (data.documentState) {
181-
this.#emitDocumentStateStep(
182-
data.documentState,
183-
data.document.lastSavedVersion,
184-
)
185-
}
186179
}
187180

188181
startSync() {

src/services/WebSocketPolyfill.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
* SPDX-License-Identifier: AGPL-3.0-or-later
44
*/
55

6+
import type { OpenData } from '../apis/connect'
67
import { decodeArrayBuffer, encodeArrayBuffer } from '../helpers/base64'
78
import { logger } from '../helpers/logger.js'
9+
import { stepsFromOpenData } from '../helpers/yjs'
810
import getNotifyBus from './NotifyService'
911
import type { Step, SyncService } from './SyncService'
1012

@@ -35,10 +37,11 @@ export default function initWebSocketPolyfill(
3537
this.#url = url
3638
logger.debug('WebSocketPolyfill#constructor', { url, fileId })
3739

38-
this.#onOpened = () => {
40+
this.#onOpened = (data: OpenData) => {
3941
if (syncService.hasActiveConnection()) {
4042
this.onopen?.()
4143
}
44+
this.#processSteps(stepsFromOpenData(data))
4245
}
4346
syncService.bus.on('opened', this.#onOpened)
4447

@@ -104,6 +107,7 @@ export default function initWebSocketPolyfill(
104107

105108
async close() {
106109
syncService.bus.off('sync', this.#onSync)
110+
syncService.bus.off('opened', this.#onOpened)
107111
this.#notifyPushBus?.off('notify_push', this.#onNotifyPush.bind(this))
108112
this.onclose?.(new CloseEvent('closing'))
109113
logger.debug('Websocket closed')

0 commit comments

Comments
 (0)