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[]) { export async function extendWithStack(photos: IPhoto[]) {
// Add Live Photos files // Add Live Photos files
const livePhotos = ( const livePhotos: IPhoto[] = [];
await Promise.all( for await (const res of runInParallel(
photos photos
.filter((p) => p.liveid && !p.liveid.startsWith('self__')) .filter((p) => p.liveid && !p.liveid.startsWith('self__'))
.map(async (p) => { .map((p) => async () => {
try { try {
const url = API.Q(utils.getLivePhotoVideoUrl(p, false), { format: 'json' }); const base = utils.getLivePhotoVideoUrl(p, false);
return (await axios.get<IPhoto>(url)).data; const url = API.Q(base, { format: 'json' });
const res = await axios.get<IPhoto>(url);
return res.data;
} catch (error) { } catch (error) {
console.error(error); console.error(error);
return null; return null;
} }
}), }),
) 10,
).filter((p) => p !== null) as IPhoto[]; )) {
livePhotos.push(...utils.filterTruthy(res));
}
// Add stacked RAW files // Add stacked RAW files (deduped)
const stackRaw = photos.map((p) => p.stackraw ?? []).flat(); const stackRaw = Array.from(new Set(photos.map((p) => p.stackraw ?? []).flat()));
// Combine and return // Combine and return
return photos.concat(livePhotos, stackRaw); 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] } { export function truthy<T, K extends keyof T>(obj: T, prop: K): obj is T & { [P in K]-?: T[K] } {
return !!obj[prop]; return !!obj[prop];
} }
/** Filter truthy values from an array */
export function filterTruthy<T>(arr: T[]): NonNullable<T>[] {
return arr.filter(Boolean) as NonNullable<T>[];
}