|
3 | 3 | * SPDX-License-Identifier: AGPL-3.0-or-later |
4 | 4 | */ |
5 | 5 |
|
6 | | -import { decodeArrayBuffer } from '../helpers/base64.js' |
| 6 | +import { decodeArrayBuffer, encodeArrayBuffer } from '../helpers/base64.js' |
7 | 7 | import { logger } from '../helpers/logger.js' |
8 | 8 | import getNotifyBus from './NotifyService' |
9 | 9 | import type { Step, SyncService } from './SyncService.js' |
@@ -78,16 +78,24 @@ export default function initWebSocketPolyfill( |
78 | 78 | send(step: Uint8Array<ArrayBufferLike>) { |
79 | 79 | // Useful for debugging what steps are sent and how they were initiated |
80 | 80 | // logStep(step) |
81 | | - if (this.#processingVersion) { |
82 | | - // this is a direct response while processing the step |
83 | | - console.error(`Failed to process step ${this.#processingVersion}.`, { |
84 | | - lastSuccessfullyProcessed: syncService.version, |
85 | | - sendingSyncStep1: step, |
86 | | - }) |
87 | | - // Do not increase the syncService.version for the current steps |
88 | | - // as we failed to process them. |
89 | | - this.#processingVersion = 0 |
| 81 | + |
| 82 | + const encoded = encodeArrayBuffer(step) |
| 83 | + const isSyncStep1 = encoded < 'AAE' |
| 84 | + if (!this.#processingVersion || !isSyncStep1) { |
| 85 | + syncService.sendStep(step) |
| 86 | + return |
90 | 87 | } |
| 88 | + |
| 89 | + // If `this.#processingVersion` is set, we're in the middle of applying steps of one version. |
| 90 | + // If `isSyncStep1`, Yjs failed to integrate a message due to pending structs. |
| 91 | + // Log and ask for recovery due to a not applied/missing step. |
| 92 | + console.error(`Failed to process step ${this.#processingVersion}.`, { |
| 93 | + lastSuccessfullyProcessed: syncService.version, |
| 94 | + sendingSyncStep1: step, |
| 95 | + }) |
| 96 | + // Do not increase the syncService.version for the current steps |
| 97 | + // as we failed to process them. |
| 98 | + this.#processingVersion = 0 |
91 | 99 | syncService.sendRecoveryStep(step) |
92 | 100 | } |
93 | 101 |
|
|
0 commit comments