-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(sync): cache steps in sync service
Store the steps that need to be send where we also do the debouncing. They will be updated whenever there is a new message from y-websocket. Signed-off-by: Max <[email protected]>
- Loading branch information
1 parent
3bc7015
commit a52bc33
Showing
6 changed files
with
97 additions
and
181 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
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
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 = '' | ||
} | ||
} | ||
|
||
} |
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
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
Oops, something went wrong.