Skip to content

Commit

Permalink
feat: push operations by chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
acaldas committed Jul 2, 2024
1 parent ad620d1 commit ec6c80b
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions src/hooks/useDocumentDriveServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -123,7 +126,6 @@ export function useDocumentDriveServer(
name,
parentFolder: parentFolder ?? null,
documentType,

document,
},
['global'],
Expand Down Expand Up @@ -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<void>(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();
}
}),
);
}
}
}

Expand Down

0 comments on commit ec6c80b

Please sign in to comment.