-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup! refactor(sync): cache steps in sync service
- Loading branch information
1 parent
d5c82a2
commit e786a18
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = '' | ||
} | ||
} | ||
|
||
} |