deno-express
Zef Hemel 2023-05-30 15:28:36 +02:00
parent 3a5e49e938
commit 0cef714198
2 changed files with 24 additions and 5 deletions

View File

@ -172,6 +172,7 @@ const clientId = store.get("collabClientId").then(async (clientId) => {
return clientId;
});
let lastCollabPage: string | undefined;
let currentCollabId: string | undefined;
const localCollabServer = location.protocol === "http:"
@ -189,16 +190,18 @@ async function ping() {
if (!collabId && currentCollabId) {
// Stop collab
console.log("Stopping collab");
editor.flashNotification(
"Other users have left this page, switched back to single-user mode.",
);
if (lastCollabPage === currentPage) {
editor.flashNotification(
"Other users have left this page, switched back to single-user mode.",
);
}
currentCollabId = undefined;
await collab.stop();
} else if (collabId && collabId !== currentCollabId) {
// Start collab
console.log("Starting collab");
editor.flashNotification(
"Another device has joined this page, switched to multi-user mode.",
"Opening page in multi-user mode.",
);
currentCollabId = collabId;
await collab.start(
@ -207,6 +210,9 @@ async function ping() {
"you",
);
}
if (currentCollabId) {
lastCollabPage = currentPage;
}
} catch (e: any) {
// console.error("Ping error", e);
if (e.message.includes("Failed to fetch") && currentCollabId) {

View File

@ -111,7 +111,20 @@ export class CollabServer {
quiet: true,
onLoadDocument: async (doc) => {
console.log("[Hocuspocus]", "Requesting doc load", doc.documentName);
const pageName = doc.documentName.split("/").slice(1).join("/");
const [collabId, ...pageNamePieces] = doc.documentName.split("/");
const pageName = pageNamePieces.join("/");
const collabPage = this.pages.get(pageName);
if (!collabPage || collabPage.collabId !== collabId) {
// This can happen after a server restart, where old clients are still trying to continue on an old session
// This will self-correct when the client discovers that the collabId has changed
// Until then: HARD PASS (meaning: don't send a document)
console.warn(
"[Hocuspocus]",
"Client tried to connect to old session",
doc.documentName,
);
return;
}
try {
const yText = doc.document.getText("codemirror");
const { data } = await this.spacePrimitives.readFile(