From d2aea687f3bbb6176e9bf100ed345e1c74a254e8 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Thu, 25 Aug 2022 10:43:05 +0200 Subject: [PATCH] Remove collection fetch on document delete --- app/components/SocketProvider.tsx | 23 +++++++++++----- app/models/Collection.ts | 27 +++++++++++++++++++ app/scenes/Document/components/DataLoader.tsx | 2 +- app/stores/BaseStore.ts | 4 +-- app/types.ts | 5 ++++ .../queues/processors/WebsocketsProcessor.ts | 14 ++-------- 6 files changed, 53 insertions(+), 22 deletions(-) diff --git a/app/components/SocketProvider.tsx b/app/components/SocketProvider.tsx index d95ad4647..824b1380f 100644 --- a/app/components/SocketProvider.tsx +++ b/app/components/SocketProvider.tsx @@ -17,6 +17,7 @@ import { PartialWithId, WebsocketCollectionUpdateIndexEvent, WebsocketCollectionUserEvent, + WebsocketDocumentDeletedEvent, WebsocketEntitiesEvent, WebsocketEntityDeletedEvent, } from "~/types"; @@ -247,15 +248,23 @@ class SocketProvider extends React.Component { } ); - this.socket.on("documents.delete", (event: WebsocketEntityDeletedEvent) => { - const document = documents.get(event.modelId); + this.socket.on( + "documents.delete", + (event: WebsocketDocumentDeletedEvent) => { + const document = documents.get(event.modelId); + const collection = collections.get(event.collectionId); - if (document) { - document.deletedAt = new Date().toISOString(); + if (collection) { + collection.removeDocument(event.modelId); + } + + if (document) { + document.deletedAt = new Date().toISOString(); + } + + policies.remove(event.modelId); } - - policies.remove(event.modelId); - }); + ); this.socket.on( "documents.permanent_delete", diff --git a/app/models/Collection.ts b/app/models/Collection.ts index 0f4acc19e..c13d5efe9 100644 --- a/app/models/Collection.ts +++ b/app/models/Collection.ts @@ -101,6 +101,12 @@ export default class Collection extends ParanoidModel { return sortNavigationNodes(this.documents, this.sort); } + /** + * Updates the document identified by the given id in the collection in memory. + * Does not update the document in the database. + * + * @param document The document properties stored in the collection + */ @action updateDocument(document: Pick) { const travelNodes = (nodes: NavigationNode[]) => @@ -116,6 +122,27 @@ export default class Collection extends ParanoidModel { travelNodes(this.documents); } + /** + * Removes the document identified by the given id from the collection in + * memory. Does not remove the document from the database. + * + * @param documentId The id of the document to remove. + */ + @action + removeDocument(documentId: string) { + this.documents = this.documents.filter(function f(node): boolean { + if (node.id === documentId) { + return false; + } + + if (node.children) { + node.children = node.children.filter(f); + } + + return true; + }); + } + @action updateIndex(index: string) { this.index = index; diff --git a/app/scenes/Document/components/DataLoader.tsx b/app/scenes/Document/components/DataLoader.tsx index 0c8251316..b4d924304 100644 --- a/app/scenes/Document/components/DataLoader.tsx +++ b/app/scenes/Document/components/DataLoader.tsx @@ -57,7 +57,7 @@ function DataLoader({ match, children }: Props) { : undefined; const isEditRoute = match.path === matchDocumentEdit; const isEditing = isEditRoute || !!auth.team?.collaborativeEditing; - const can = usePolicy(document); + const can = usePolicy(document?.id); const location = useLocation(); React.useEffect(() => { diff --git a/app/stores/BaseStore.ts b/app/stores/BaseStore.ts index d35c35b29..a678270b5 100644 --- a/app/stores/BaseStore.ts +++ b/app/stores/BaseStore.ts @@ -113,8 +113,8 @@ export default abstract class BaseStore { }; @action - remove(id: string): boolean { - return this.data.delete(id); + remove(id: string): void { + this.data.delete(id); } save( diff --git a/app/types.ts b/app/types.ts index 76c5a7c35..fbf766772 100644 --- a/app/types.ts +++ b/app/types.ts @@ -188,6 +188,11 @@ export type WebsocketEntityDeletedEvent = { modelId: string; }; +export type WebsocketDocumentDeletedEvent = WebsocketEntityDeletedEvent & { + modelId: string; + collectionId: string; +}; + export type WebsocketEntitiesEvent = { documentIds: { id: string; updatedAt?: string }[]; collectionIds: { id: string; updatedAt?: string }[]; diff --git a/server/queues/processors/WebsocketsProcessor.ts b/server/queues/processors/WebsocketsProcessor.ts index 96ac954ba..b3d89f7fa 100644 --- a/server/queues/processors/WebsocketsProcessor.ts +++ b/server/queues/processors/WebsocketsProcessor.ts @@ -64,7 +64,7 @@ export default class WebsocketsProcessor { return; } - socketio + return socketio .to( document.publishedAt ? `collection-${document.collectionId}` @@ -72,17 +72,7 @@ export default class WebsocketsProcessor { ) .emit(event.name, { modelId: event.documentId, - }); - - return socketio - .to(`collection-${document.collectionId}`) - .emit("entities", { - event: event.name, - collectionIds: [ - { - id: document.collectionId, - }, - ], + collectionId: event.collectionId, }); }