1 import type { VersionHistoryUpdate } from './VersionHistoryBatch'
2 import type { VersionHistoryBatch } from './VersionHistoryBatch'
3 import { Result } from '@proton/docs-shared'
4 import type { SyncUseCaseInterface } from '../Domain/UseCase/SyncUseCaseInterface'
7 * BatchDocumentUpdates takes a list of DecryptedMessages and creates batches of them
8 * based on the threshold provided.
9 * For example, if the threshold is 100, and there are 200 updates, the result will be
10 * two batches of 100 updates each.
12 export class BatchDocumentUpdates implements SyncUseCaseInterface<VersionHistoryBatch[]> {
13 execute(updates: VersionHistoryUpdate[], batchThreshold: number): Result<VersionHistoryBatch[]> {
14 if (!updates.length) {
18 const numberOfBatches = Math.round(updates.length / batchThreshold)
19 const batches: VersionHistoryBatch[] = []
21 for (let batchIndex = 0; batchIndex <= numberOfBatches; batchIndex++) {
22 const start = batchIndex * batchThreshold
23 const nextIndex = batchIndex + 1
24 const end = nextIndex * batchThreshold
25 const batch = updates.slice(start, end)
26 if (batch.length > 0) {
29 if (batch.length < batchThreshold) {
33 return Result.ok(batches)