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

View File

@ -111,7 +111,20 @@ export class CollabServer {
quiet: true, quiet: true,
onLoadDocument: async (doc) => { onLoadDocument: async (doc) => {
console.log("[Hocuspocus]", "Requesting doc load", doc.documentName); 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 { try {
const yText = doc.document.getText("codemirror"); const yText = doc.document.getText("codemirror");
const { data } = await this.spacePrimitives.readFile( const { data } = await this.spacePrimitives.readFile(