From cbf2ea4728a39ae8d636432ede8f9cac29011707 Mon Sep 17 00:00:00 2001 From: danthonywalker Date: Fri, 11 Aug 2023 14:17:22 -0500 Subject: [PATCH] * Optimized lock functionality * Optimized computeIfAbsent functionality * Optimized YouTube requests --- src/cache.ts | 31 ++++++++++++------- .../creators/subscriptions/create/index.ts | 8 ++--- src/features/creators/youtube.ts | 17 ++++------ src/lock.ts | 8 +++-- 4 files changed, 35 insertions(+), 29 deletions(-) diff --git a/src/cache.ts b/src/cache.ts index 52ef11a..f9fcfb4 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -85,18 +85,25 @@ const computeIfAbsent = async ( key: string, callback: () => Promise>, expireInMilliseconds = Number.MAX_VALUE, -) => - lock(key, async (lock) => { - let value = await get>(key); - - if (value === undefined) { - value = await callback(); - const json = JSON.stringify(value); - await atomicSet(lock, key, json, expireInMilliseconds); - } - - return value; - }); +) => { + let value = await get>(key); + + if (value === undefined) { + value = await lock(key, async (lock) => { + let value = await get>(key); + + if (value === undefined) { + value = await callback(); + const json = JSON.stringify(value); + await atomicSet(lock, key, json, expireInMilliseconds); + } + + return value; + }); + } + + return value; +}; export default { computeIfAbsent, diff --git a/src/features/creators/subscriptions/create/index.ts b/src/features/creators/subscriptions/create/index.ts index fdc22df..cdb1a3b 100644 --- a/src/features/creators/subscriptions/create/index.ts +++ b/src/features/creators/subscriptions/create/index.ts @@ -284,13 +284,13 @@ export default async (interaction: ChatInputCommandInteraction) => { } const buttonId = buttonInteraction.customId; - const youtubeChannelId = selectedYoutubeChannel?.channelId; + const { channelId, channelTitle, title } = selectedYoutubeChannel ?? {}; - if (buttonId === cancelButtonId || typeof youtubeChannelId !== "string") + if (buttonId === cancelButtonId || typeof channelId !== "string") return buttonInteraction.update(noResultsExistOptions(null)); await database.createCreatorSubscriptions({ - domainId: youtubeChannelId, + domainId: channelId, creatorType: CreatorType.YOUTUBE, creatorChannelIds: selectedCreatorChannelIds, }); @@ -300,7 +300,7 @@ export default async (interaction: ChatInputCommandInteraction) => { .map(channelMention) .join(", "); - const youtubeChannelName = selectedYoutubeChannel?.title ?? name; + const youtubeChannelName = channelTitle ?? title ?? name; const description = compress` Successfully created a subscription for ${bold(youtubeChannelName)}! Posts will now be automatically created in ${channelMentions} when diff --git a/src/features/creators/youtube.ts b/src/features/creators/youtube.ts index 8eb062b..61008f9 100644 --- a/src/features/creators/youtube.ts +++ b/src/features/creators/youtube.ts @@ -9,6 +9,8 @@ export const getChannels = (query: string) => { const key = CacheKey.channels(query); const callback = async () => { const { data } = await search.list({ + fields: + "items(snippet(channelId,channelTitle,description,publishedAt,thumbnails,title))", maxResults: 50, part: ["snippet"], q: query, @@ -30,19 +32,11 @@ export const getChannel = (channelId: string) => { const key = CacheKey.channel(channelId); const callback = async () => { const { data } = await channels.list({ + fields: + "items(contentDetails(relatedPlaylists(uploads)),id,snippet(title,thumbnails))", id: [channelId], maxResults: 1, - part: [ - "brandingSettings", - "contentDetails", - "contentOwnerDetails", - "id", - "localizations", - "snippet", - "statistics", - "status", - "topicDetails", - ], + part: ["contentDetails", "id", "snippet"], }); const items = data.items ?? []; @@ -59,6 +53,7 @@ export const getVideos = (playlistId: string) => { const key = CacheKey.videos(playlistId); const callback = async () => { const { data } = await playlistItems.list({ + fields: "items(snippet(description,publishedAt,resourceId,title))", maxResults: 50, part: ["snippet"], playlistId, diff --git a/src/lock.ts b/src/lock.ts index 3e8db0b..5d8d401 100644 --- a/src/lock.ts +++ b/src/lock.ts @@ -132,10 +132,14 @@ export default async ( let callbackResult = callback(lockObject); while (callbackResult instanceof Promise) { - await extendLock(lockObject, expireInMilliseconds); const sleepPromise = sleep(expireInMilliseconds / 2); const result = await Promise.race([callbackResult, sleepPromise]); - callbackResult = result === undefined ? callbackResult : result; + + if (result === undefined) { + await extendLock(lockObject, expireInMilliseconds); + } else { + callbackResult = result; + } } return callbackResult;