Skip to content

Commit

Permalink
fixup! refactor(sync): cache steps in sync service
Browse files Browse the repository at this point in the history
  • Loading branch information
max-nextcloud committed Nov 25, 2024
1 parent d5c82a2 commit e786a18
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/services/Outbox.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { encodeArrayBuffer } from '../helpers/base64.js'
import { logger } from '../helpers/logger.js'

type Sendable = {
steps: string[], awareness: string
}

export default class Outbox {
#awarenessUpdate = ''
#syncUpdate = ''
#syncQuery = ''

storeStep(step: Uint8Array) {
const encoded = encodeArrayBuffer(step)
if (encoded < 'AAA') {
logger.warn('Unexpected step type:', { step, encoded })
return
}
if (encoded < 'AAE') {
this.#syncQuery = encoded
return
}
if (encoded < 'AQ') {
this.#syncUpdate = encoded
return
}
if (encoded < 'Ag') {
this.#awarenessUpdate = encoded
return
}
logger.warn('Unexpected step type:', { step, encoded })
}

getDataToSend(): Sendable {
return {
steps: [this.#syncUpdate, this.#syncQuery].filter(s => s),
awareness: this.#awarenessUpdate,
}
}

get hasUpdate(): boolean {
return !!this.#syncUpdate
}

/*
* Clear data that has just been
*
* Only clear data that has not changed in the meantime.
* @param {Sendable} - data that was to the server
*/
clearSentData({ steps, awareness }: Sendable) {
if (steps.includes(this.#syncUpdate)) {
this.#syncUpdate = ''
}
if (steps.includes(this.#syncQuery)) {
this.#syncQuery = ''
}
if (this.#awarenessUpdate === awareness) {
this.#awarenessUpdate = ''
}
}

}

0 comments on commit e786a18

Please sign in to comment.