From 8ac3c17310249d27fd3dbd2d87fe259787bbcbc9 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Mon, 19 Aug 2024 19:10:30 -0400 Subject: [PATCH] fix: Reduce unneccessary client fetches --- app/components/WebsocketProvider.tsx | 6 ++++ app/types.ts | 1 + .../queues/processors/WebsocketsProcessor.ts | 28 ++++--------------- 3 files changed, 12 insertions(+), 23 deletions(-) diff --git a/app/components/WebsocketProvider.tsx b/app/components/WebsocketProvider.tsx index 8bd90e8d33..701c59a99e 100644 --- a/app/components/WebsocketProvider.tsx +++ b/app/components/WebsocketProvider.tsx @@ -135,6 +135,9 @@ class WebsocketProvider extends React.Component { if (document?.updatedAt === documentDescriptor.updatedAt) { continue; } + if (!document && !event.fetchIfMissing) { + continue; + } // otherwise, grab the latest version of the document try { @@ -180,6 +183,9 @@ class WebsocketProvider extends React.Component { if (collection?.updatedAt === collectionDescriptor.updatedAt) { continue; } + if (!collection?.documents?.length && !event.fetchIfMissing) { + continue; + } try { await collection?.fetchDocuments({ diff --git a/app/types.ts b/app/types.ts index 323948fb61..bd3a897307 100644 --- a/app/types.ts +++ b/app/types.ts @@ -180,6 +180,7 @@ export type WebsocketEntityDeletedEvent = { }; export type WebsocketEntitiesEvent = { + fetchIfMissing?: boolean; documentIds: { id: string; updatedAt?: string }[]; collectionIds: { id: string; updatedAt?: string }[]; groupIds: { id: string; updatedAt?: string }[]; diff --git a/server/queues/processors/WebsocketsProcessor.ts b/server/queues/processors/WebsocketsProcessor.ts index 5bafb17acd..c72b1f5ee3 100644 --- a/server/queues/processors/WebsocketsProcessor.ts +++ b/server/queues/processors/WebsocketsProcessor.ts @@ -36,6 +36,7 @@ import { Event } from "../../types"; export default class WebsocketsProcessor { public async perform(event: Event, socketio: Server) { switch (event.name) { + case "documents.create": case "documents.publish": case "documents.unpublish": case "documents.restore": @@ -46,10 +47,14 @@ export default class WebsocketsProcessor { if (!document) { return; } + if (event.name === "documents.create" && document.importId) { + return; + } const channels = await this.getDocumentEventChannels(event, document); return socketio.to(channels).emit("entities", { event: event.name, + fetchIfMissing: true, documentIds: [ { id: document.id, @@ -86,29 +91,6 @@ export default class WebsocketsProcessor { return socketio.to(channels).emit(event.name, data); } - case "documents.create": { - const document = await Document.findByPk(event.documentId); - if (!document || document.importId) { - return; - } - - const channels = await this.getDocumentEventChannels(event, document); - return socketio.to(channels).emit("entities", { - event: event.name, - documentIds: [ - { - id: document.id, - updatedAt: document.updatedAt, - }, - ], - collectionIds: [ - { - id: document.collectionId, - }, - ], - }); - } - case "documents.move": { const documents = await Document.findAll({ where: {