Use abstraction

This commit is contained in:
Manav Rathi
2025-06-26 19:19:27 +05:30
parent 8b46cf6bc5
commit 5b6d0e7a2a
2 changed files with 22 additions and 30 deletions

View File

@@ -34,9 +34,9 @@ import {
createMagicMetadata,
encryptMagicMetadata,
} from "ente-media/magic-metadata";
import { batch, splitByPredicate } from "ente-utils/array";
import { splitByPredicate } from "ente-utils/array";
import { z } from "zod/v4";
import { requestBatchSize, type UpdateMagicMetadataRequest } from "./file";
import { batched, type UpdateMagicMetadataRequest } from "./file";
import {
removeCollectionIDLastSyncTime,
saveCollectionFiles,
@@ -531,8 +531,8 @@ export interface MoveToCollectionRequest {
export const addToCollection = async (
collection: Collection,
files: EnteFile[],
) => {
for (const batchFiles of batch(files, requestBatchSize)) {
) =>
batched(files, async (batchFiles) => {
const encryptedFileKeys = await encryptWithCollectionKey(
collection,
batchFiles,
@@ -547,8 +547,7 @@ export const addToCollection = async (
}),
}),
);
}
};
});
/**
* Make a remote request to restore the given {@link files} to the given
@@ -559,8 +558,8 @@ export const addToCollection = async (
export const restoreToCollection = async (
collection: Collection,
files: EnteFile[],
) => {
for (const batchFiles of batch(files, requestBatchSize)) {
) =>
batched(files, async (batchFiles) => {
const encryptedFileKeys = await encryptWithCollectionKey(
collection,
batchFiles,
@@ -575,8 +574,7 @@ export const restoreToCollection = async (
}),
}),
);
}
};
});
/**
* Make a remote request to move the given {@link files} from a collection (as
@@ -589,8 +587,8 @@ export const moveToCollection = async (
fromCollectionID: number,
toCollection: Collection,
files: EnteFile[],
) => {
for (const batchFiles of batch(files, requestBatchSize)) {
) =>
batched(files, async (batchFiles) => {
const encryptedFileKeys = await encryptWithCollectionKey(
toCollection,
batchFiles,
@@ -606,8 +604,7 @@ export const moveToCollection = async (
}),
}),
);
}
};
});
/**
* Return an array of {@link CollectionFileItem}s, one for each file in
@@ -638,8 +635,8 @@ const encryptWithCollectionKey = async (
*
* Remote only, does not modify local state.
*/
export const moveToTrash = async (files: EnteFile[]) => {
for (const batchFiles of batch(files, requestBatchSize)) {
export const moveToTrash = async (files: EnteFile[]) =>
batched(files, async (batchFiles) =>
ensureOk(
await fetch(await apiURL("/files/trash"), {
method: "POST",
@@ -651,26 +648,24 @@ export const moveToTrash = async (files: EnteFile[]) => {
})),
}),
}),
);
}
};
),
);
/**
* Make a remote request to delete the given {@link fileIDs} from trash.
*
* Remote only, does not modify local state.
*/
export const deleteFromTrash = async (fileIDs: number[]) => {
for (const batchIDs of batch(fileIDs, requestBatchSize)) {
export const deleteFromTrash = async (fileIDs: number[]) =>
batched(fileIDs, async (batchIDs) =>
ensureOk(
await fetch(await apiURL("/trash/delete"), {
method: "POST",
headers: await authenticatedRequestHeaders(),
body: JSON.stringify({ fileIDs: batchIDs }),
}),
);
}
};
),
);
/**
* Rename a collection on remote.

View File

@@ -24,7 +24,7 @@ import { savedCollectionFiles } from "./photos-fdb";
* selected files) are expected to be batched to keep each request of a
* reasonable size. By default, we break the request into batches of 1000.
*/
export const requestBatchSize = 1000;
const requestBatchSize = 1000;
/**
* Perform an operation on batches, concurrently.
@@ -43,7 +43,7 @@ export const requestBatchSize = 1000;
* @returns An array of results, one from each batch operation. For details,
* including behaviour on errors, see `Promise.all`.
*/
export const performInBatches = <T, U>(
export const batched = <T, U>(
items: T[],
op: (batchItems: T[]) => Promise<U>,
): Promise<U[]> => Promise.all(batch(items, requestBatchSize).map(op));
@@ -86,10 +86,7 @@ export const computeNormalCollectionFilesFromSaved = async () => {
export const updateFilesVisibility = async (
files: EnteFile[],
visibility: ItemVisibility,
) =>
performInBatches(files, (b) =>
updateFilesPrivateMagicMetadata(b, { visibility }),
);
) => batched(files, (b) => updateFilesPrivateMagicMetadata(b, { visibility }));
/**
* Update the private magic metadata of a list of files on remote.