perf: Improve collab persistence performance (#4544)

* stash

* Remove query of state in documentCollaborativeUpdater
This commit is contained in:
Tom Moor
2022-12-06 05:21:10 -08:00
committed by GitHub
parent b0b7c7d647
commit 98d79e1e8b
2 changed files with 29 additions and 19 deletions

View File

@@ -7,18 +7,23 @@ import { schema, serializer } from "@server/editor";
import Logger from "@server/logging/Logger";
import { Document, Event } from "@server/models";
type Props = {
/** The document ID to update */
documentId: string;
/** Current collaobrative state */
ydoc: Y.Doc;
/** The user ID that is performing the update, if known */
userId?: string;
};
export default async function documentCollaborativeUpdater({
documentId,
ydoc,
userId,
}: {
documentId: string;
ydoc: Y.Doc;
userId?: string;
}) {
}: Props) {
return sequelize.transaction(async (transaction) => {
const document = await Document.unscoped()
.scope("withState")
.scope("withoutState")
.findOne({
where: {
id: documentId,
@@ -36,48 +41,41 @@ export default async function documentCollaborativeUpdater({
const node = Node.fromJSON(schema, yDocToProsemirrorJSON(ydoc, "default"));
const text = serializer.serialize(node, undefined);
const isUnchanged = document.text === text;
const hasMultiplayerState = !!document.state;
const lastModifiedById = userId ?? document.lastModifiedById;
if (isUnchanged && hasMultiplayerState) {
if (isUnchanged) {
return;
}
Logger.info(
"multiplayer",
`Persisting ${documentId}, attributed to ${userId}`
`Persisting ${documentId}, attributed to ${lastModifiedById}`
);
// extract collaborators from doc user data
const pud = new Y.PermanentUserData(ydoc);
const pudIds = Array.from(pud.clients.values());
const existingIds = document.collaboratorIds;
const collaboratorIds = uniq([...pudIds, ...existingIds]);
const collaboratorIds = uniq([...document.collaboratorIds, ...pudIds]);
await document.update(
{
text,
state: Buffer.from(state),
lastModifiedById:
isUnchanged || !userId ? document.lastModifiedById : userId,
lastModifiedById,
collaboratorIds,
},
{
transaction,
silent: isUnchanged,
hooks: false,
}
);
if (isUnchanged) {
return;
}
await Event.schedule({
name: "documents.update",
documentId: document.id,
collectionId: document.collectionId,
teamId: document.teamId,
actorId: userId ?? document.lastModifiedById,
actorId: lastModifiedById,
data: {
multiplayer: true,
title: document.title,