From ec6c80b42fbb08b07d8bbd4ed2aad936da647680 Mon Sep 17 00:00:00 2001 From: acaldas Date: Tue, 2 Jul 2024 16:42:24 +0100 Subject: [PATCH] feat: push operations by chunks --- src/hooks/useDocumentDriveServer.ts | 41 +++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/src/hooks/useDocumentDriveServer.ts b/src/hooks/useDocumentDriveServer.ts index 2e064e6b..705d4132 100644 --- a/src/hooks/useDocumentDriveServer.ts +++ b/src/hooks/useDocumentDriveServer.ts @@ -24,6 +24,9 @@ import { loadFile } from 'src/utils/file'; import { useDocumentDrives } from './useDocumentDrives'; import { useUserPermissions } from './useUserPermissions'; +export const FILE_UPLOAD_OPERATIONS_CHUNK_SIZE = + parseInt(import.meta.env.FILE_UPLOAD_OPERATIONS_CHUNK_SIZE as string) || 50; + // TODO this should be added to the document model export interface SortOptions { afterNodePath?: string; @@ -123,7 +126,6 @@ export function useDocumentDriveServer( name, parentFolder: parentFolder ?? null, documentType, - document, }, ['global'], @@ -169,8 +171,43 @@ export function useDocumentDriveServer( ); // then add all the operations + const operationsLimit = FILE_UPLOAD_OPERATIONS_CHUNK_SIZE; for (const operations of Object.values(document.operations)) { - await addOperations(drive, fileNode.id, operations); + for (let i = 0; i < operations.length; i += operationsLimit) { + const chunk = operations.slice(i, i + operationsLimit); + const operation = chunk.at(-1); + if (!operation) { + break; + } + const { scope } = operation; + + await addOperations(drive, fileNode.id, chunk); + await new Promise(resolve => + server.on('strandUpdate', update => { + const sameScope = + update.documentId === fileNode.id && + update.scope == scope; + if (!sameScope) { + return; + } + + // if all pushed operations are found in the strand + // update then moves on to the next chunk + const operationNotFound = chunk.find( + op => + !update.operations.find(strandOp => + op.id + ? op.id === strandOp.id + : op.index === strandOp.index && + op.hash === strandOp.hash, + ), + ); + if (!operationNotFound) { + resolve(); + } + }), + ); + } } }