From 9bc77aeb892ee723b65f1a90c2a68e09390f6196 Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Fri, 24 Nov 2023 13:56:00 -0800 Subject: [PATCH] dav: use pipeline for extendWithStack Related #903 Signed-off-by: Varun Patil --- src/services/dav/base.ts | 38 +++++++++++++++++++++----------------- src/services/utils/algo.ts | 5 +++++ 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/services/dav/base.ts b/src/services/dav/base.ts index efd99145..fdfa23f7 100644 --- a/src/services/dav/base.ts +++ b/src/services/dav/base.ts @@ -173,24 +173,28 @@ export async function* runInParallel(promises: (() => Promise)[], n: numbe */ export async function extendWithStack(photos: IPhoto[]) { // Add Live Photos files - const livePhotos = ( - await Promise.all( - photos - .filter((p) => p.liveid && !p.liveid.startsWith('self__')) - .map(async (p) => { - try { - const url = API.Q(utils.getLivePhotoVideoUrl(p, false), { format: 'json' }); - return (await axios.get(url)).data; - } catch (error) { - console.error(error); - return null; - } - }), - ) - ).filter((p) => p !== null) as IPhoto[]; + const livePhotos: IPhoto[] = []; + for await (const res of runInParallel( + photos + .filter((p) => p.liveid && !p.liveid.startsWith('self__')) + .map((p) => async () => { + try { + const base = utils.getLivePhotoVideoUrl(p, false); + const url = API.Q(base, { format: 'json' }); + const res = await axios.get(url); + return res.data; + } catch (error) { + console.error(error); + return null; + } + }), + 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); diff --git a/src/services/utils/algo.ts b/src/services/utils/algo.ts index cd5e0049..903c51ab 100644 --- a/src/services/utils/algo.ts +++ b/src/services/utils/algo.ts @@ -120,3 +120,8 @@ export function isNumber(num: T): boolean { export function truthy(obj: T, prop: K): obj is T & { [P in K]-?: T[K] } { return !!obj[prop]; } + +/** Filter truthy values from an array */ +export function filterTruthy(arr: T[]): NonNullable[] { + return arr.filter(Boolean) as NonNullable[]; +}