|
| 1 | +/** |
| 2 | + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +import { encodeArrayBuffer } from '../helpers/base64.js' |
| 7 | +import { logger } from '../helpers/logger.js' |
| 8 | + |
| 9 | +type Sendable = { |
| 10 | + steps: string[], awareness: string |
| 11 | +} |
| 12 | + |
| 13 | +export default class Outbox { |
| 14 | + #awarenessUpdate = '' |
| 15 | + #syncUpdate = '' |
| 16 | + #syncQuery = '' |
| 17 | + |
| 18 | + storeStep(step: Uint8Array) { |
| 19 | + const encoded = encodeArrayBuffer(step) |
| 20 | + if (encoded < 'AAA') { |
| 21 | + logger.warn('Unexpected step type:', { step, encoded }) |
| 22 | + return |
| 23 | + } |
| 24 | + if (encoded < 'AAE') { |
| 25 | + this.#syncQuery = encoded |
| 26 | + return |
| 27 | + } |
| 28 | + if (encoded < 'AQ') { |
| 29 | + this.#syncUpdate = encoded |
| 30 | + return |
| 31 | + } |
| 32 | + if (encoded < 'Ag') { |
| 33 | + this.#awarenessUpdate = encoded |
| 34 | + return |
| 35 | + } |
| 36 | + logger.warn('Unexpected step type:', { step, encoded }) |
| 37 | + } |
| 38 | + |
| 39 | + getDataToSend(): Sendable { |
| 40 | + return { |
| 41 | + steps: [this.#syncUpdate, this.#syncQuery].filter(s => s), |
| 42 | + awareness: this.#awarenessUpdate, |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + get hasUpdate(): boolean { |
| 47 | + return !!this.#syncUpdate |
| 48 | + } |
| 49 | + |
| 50 | + /* |
| 51 | + * Clear data that has just been |
| 52 | + * |
| 53 | + * Only clear data that has not changed in the meantime. |
| 54 | + * @param {Sendable} - data that was to the server |
| 55 | + */ |
| 56 | + clearSentData({ steps, awareness }: Sendable) { |
| 57 | + if (steps.includes(this.#syncUpdate)) { |
| 58 | + this.#syncUpdate = '' |
| 59 | + } |
| 60 | + if (steps.includes(this.#syncQuery)) { |
| 61 | + this.#syncQuery = '' |
| 62 | + } |
| 63 | + if (this.#awarenessUpdate === awareness) { |
| 64 | + this.#awarenessUpdate = '' |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | +} |
0 commit comments