From b750c86b880f2154a52c296e4b5430b79fc775f5 Mon Sep 17 00:00:00 2001 From: Shiba <44804845+DeepDoge@users.noreply.github.com> Date: Sun, 9 Jan 2022 13:12:05 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=8D=98=20ctxFromURL=20runs=20once=20for?= =?UTF-8?q?=20the=20same=20url=20at=20a=20time?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/scripts/tabOnUpdated.ts | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/scripts/tabOnUpdated.ts b/src/scripts/tabOnUpdated.ts index bebcf01..cf67bfc 100644 --- a/src/scripts/tabOnUpdated.ts +++ b/src/scripts/tabOnUpdated.ts @@ -16,6 +16,7 @@ async function resolveYT(descriptor: YtIdResolverDescriptor) { return segments.join('/'); } +const ctxFromURLOnGoingPromise: Record> = {} async function ctxFromURL(href: string): Promise { if (!href) return; @@ -26,12 +27,18 @@ async function ctxFromURL(href: string): Promise { const descriptor = ytService.getId(href); if (!descriptor) return; // couldn't get the ID, so we're done - // NOTE: API call cached by resolveYT method automatically - const res = await resolveYT(descriptor); - if (!res) return; // couldn't find it on lbry, so we're done + // Don't create a new Promise for same ID until on going one is over. + const promise = ctxFromURLOnGoingPromise[descriptor.id] ?? (ctxFromURLOnGoingPromise[descriptor.id] = (async () => { + // NOTE: API call cached by resolveYT method automatically + const res = await resolveYT(descriptor) + if (!res) return // couldn't find it on lbry, so we're done - const { redirect, targetPlatform } = await getExtensionSettingsAsync('redirect', 'targetPlatform'); - return { descriptor, lbryPathname: res, redirect, targetPlatform }; + const { redirect, targetPlatform } = await getExtensionSettingsAsync('redirect', 'targetPlatform') + return { descriptor, lbryPathname: res, redirect, targetPlatform } + })()) + await promise + delete ctxFromURLOnGoingPromise[descriptor.id] + return await promise } // handles lbry.tv -> lbry app redirect