chore: Move to Typescript (#2783)
This PR moves the entire project to Typescript. Due to the ~1000 ignores this will lead to a messy codebase for a while, but the churn is worth it – all of those ignore comments are places that were never type-safe previously. closes #1282
This commit is contained in:
71
server/collaboration/persistence.ts
Normal file
71
server/collaboration/persistence.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import { onChangePayload, onLoadDocumentPayload } from "@hocuspocus/server";
|
||||
import { debounce } from "lodash";
|
||||
import * as Y from "yjs";
|
||||
import Logger from "@server/logging/logger";
|
||||
import { Document, User } from "@server/models";
|
||||
import documentUpdater from "../commands/documentUpdater";
|
||||
import markdownToYDoc from "./utils/markdownToYDoc";
|
||||
|
||||
const DELAY = 3000;
|
||||
|
||||
export default class Persistence {
|
||||
async onLoadDocument({ documentName, ...data }: onLoadDocumentPayload) {
|
||||
const [, documentId] = documentName.split(".");
|
||||
const fieldName = "default";
|
||||
|
||||
// Check if the given field already exists in the given y-doc. This is import
|
||||
// so we don't import a document fresh if it exists already.
|
||||
if (!data.document.isEmpty(fieldName)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const document = await Document.findByPk(documentId);
|
||||
|
||||
if (document.state) {
|
||||
const ydoc = new Y.Doc();
|
||||
Logger.info("database", `Document ${documentId} is in database state`);
|
||||
Y.applyUpdate(ydoc, document.state);
|
||||
return ydoc;
|
||||
}
|
||||
|
||||
Logger.info(
|
||||
"database",
|
||||
`Document ${documentId} is not in state, creating from markdown`
|
||||
);
|
||||
const ydoc = markdownToYDoc(document.text, fieldName);
|
||||
const state = Y.encodeStateAsUpdate(ydoc);
|
||||
await document.update(
|
||||
{
|
||||
state: Buffer.from(state),
|
||||
},
|
||||
{
|
||||
hooks: false,
|
||||
}
|
||||
);
|
||||
return ydoc;
|
||||
}
|
||||
|
||||
onChange = debounce(
|
||||
async ({ document, context, documentName }: onChangePayload) => {
|
||||
const [, documentId] = documentName.split(".");
|
||||
Logger.info("database", `Persisting ${documentId}`);
|
||||
|
||||
try {
|
||||
await documentUpdater({
|
||||
documentId,
|
||||
ydoc: document,
|
||||
userId: context.user?.id,
|
||||
});
|
||||
} catch (err) {
|
||||
Logger.error("Unable to persist document", err, {
|
||||
documentId,
|
||||
userId: context.user?.id,
|
||||
});
|
||||
}
|
||||
},
|
||||
DELAY,
|
||||
{
|
||||
maxWait: DELAY * 3,
|
||||
}
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user