dav: use pipeline for extendWithStack

Related #903

Signed-off-by: Varun Patil <radialapps@gmail.com>
pull/953/head
Varun Patil 2023-11-24 13:56:00 -08:00
parent 294feef80b
commit 9bc77aeb89
2 changed files with 26 additions and 17 deletions

View File

@ -173,24 +173,28 @@ export async function* runInParallel<T>(promises: (() => Promise<T>)[], n: numbe
*/
export async function extendWithStack(photos: IPhoto[]) {
// Add Live Photos files
const livePhotos = (
await Promise.all(
const livePhotos: IPhoto[] = [];
for await (const res of runInParallel(
photos
.filter((p) => p.liveid && !p.liveid.startsWith('self__'))
.map(async (p) => {
.map((p) => async () => {
try {
const url = API.Q(utils.getLivePhotoVideoUrl(p, false), { format: 'json' });
return (await axios.get<IPhoto>(url)).data;
const base = utils.getLivePhotoVideoUrl(p, false);
const url = API.Q(base, { format: 'json' });
const res = await axios.get<IPhoto>(url);
return res.data;
} catch (error) {
console.error(error);
return null;
}
}),
)
).filter((p) => p !== null) as IPhoto[];
10,
)) {
livePhotos.push(...utils.filterTruthy(res));
}
// Add stacked RAW files
const stackRaw = photos.map((p) => p.stackraw ?? []).flat();
// Add stacked RAW files (deduped)
const stackRaw = Array.from(new Set(photos.map((p) => p.stackraw ?? []).flat()));
// Combine and return
return photos.concat(livePhotos, stackRaw);

View File

@ -120,3 +120,8 @@ export function isNumber<T>(num: T): boolean {
export function truthy<T, K extends keyof T>(obj: T, prop: K): obj is T & { [P in K]-?: T[K] } {
return !!obj[prop];
}
/** Filter truthy values from an array */
export function filterTruthy<T>(arr: T[]): NonNullable<T>[] {
return arr.filter(Boolean) as NonNullable<T>[];
}