From 475c38ba0c12f9282495d33dd7c198401db7fa60 Mon Sep 17 00:00:00 2001 From: Shiba <44804845+DeepDoge@users.noreply.github.com> Date: Sun, 9 Jan 2022 12:46:18 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=8D=99=20URL=20resolver=20caching=20impro?= =?UTF-8?q?vements?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Updated types for url because it can also be null - Cached 404 as null --- src/common/yt.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/common/yt.ts b/src/common/yt.ts index 1f00e64..6d255e2 100644 --- a/src/common/yt.ts +++ b/src/common/yt.ts @@ -70,7 +70,7 @@ const URLResolverCache = (() => } else console.warn(`IndexedDB not supported`) - async function put(url: string, id: string) : Promise + async function put(url: string | null, id: string) : Promise { return await new Promise((resolve, reject) => { @@ -82,7 +82,7 @@ const URLResolverCache = (() => putRequest.addEventListener('error', () => reject(putRequest.error)) }) } - async function get(id: string): Promise + async function get(id: string): Promise { return (await new Promise((resolve, reject) => { @@ -171,10 +171,10 @@ export const ytService = { * @param descriptorsWithIndex YT resource IDs to check * @returns a promise with the list of channels that were found on lbry */ - async resolveById(descriptors: YtIdResolverDescriptor[], progressCallback?: (progress: number) => void): Promise { + async resolveById(descriptors: YtIdResolverDescriptor[], progressCallback?: (progress: number) => void): Promise<(string | null)[]> { const descriptorsWithIndex: (YtIdResolverDescriptor & { index: number })[] = descriptors.map((descriptor, index) => ({...descriptor, index})) descriptors = null as any - const results: string[] = [] + const results: (string | null)[] = [] await Promise.all(descriptorsWithIndex.map(async (descriptor, index) => { if (!descriptor) return @@ -240,7 +240,11 @@ export const ytService = { url.searchParams.set(urlResolverFunction.paramName, descriptor.id) const apiResponse = await fetch(url.toString(), { cache: 'no-store' }); - if (!apiResponse.ok) break + if (!apiResponse.ok) { + // Some API might not respond with 200 if it can't find the url + if (apiResponse.status === 404) await URLResolverCache.put(null, descriptor.id) + break + } const value = followResponsePath(await apiResponse.json(), urlResolverFunction.responsePath) if (value) results[descriptor.index] = value