Skip to content
Merged
Changes from all commits
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
28 changes: 18 additions & 10 deletions src/services/WebSocketPolyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { decodeArrayBuffer } from '../helpers/base64.js'
import { decodeArrayBuffer, encodeArrayBuffer } from '../helpers/base64.js'
import { logger } from '../helpers/logger.js'
import getNotifyBus from './NotifyService'
import type { Step, SyncService } from './SyncService.js'
Expand Down Expand Up @@ -78,16 +78,24 @@ export default function initWebSocketPolyfill(
send(step: Uint8Array<ArrayBufferLike>) {
// Useful for debugging what steps are sent and how they were initiated
// logStep(step)
if (this.#processingVersion) {
// this is a direct response while processing the step
console.error(`Failed to process step ${this.#processingVersion}.`, {
lastSuccessfullyProcessed: syncService.version,
sendingSyncStep1: step,
})
// Do not increase the syncService.version for the current steps
// as we failed to process them.
this.#processingVersion = 0

const encoded = encodeArrayBuffer(step)
const isSyncStep1 = encoded < 'AAE'
if (!this.#processingVersion || !isSyncStep1) {
syncService.sendStep(step)
return
}

// If `this.#processingVersion` is set, we're in the middle of applying steps of one version.
// If `isSyncStep1`, Yjs failed to integrate a message due to pending structs.
// Log and ask for recovery due to a not applied/missing step.
console.error(`Failed to process step ${this.#processingVersion}.`, {
lastSuccessfullyProcessed: syncService.version,
sendingSyncStep1: step,
})
// Do not increase the syncService.version for the current steps
// as we failed to process them.
this.#processingVersion = 0
syncService.sendRecoveryStep(step)
}

Expand Down
Loading