diff --git a/lib/utils/fc-cache.js b/lib/utils/fc-cache.js --- a/lib/utils/fc-cache.js +++ b/lib/utils/fc-cache.js @@ -186,6 +186,41 @@ } const fetchFarcasterChannelPromise = (async () => { + // First, we finish any ongoing fetches of followed channels, + // since our channel might be one of them + const fidsInFollowedChannelQueryCache = [ + ...this.followedFarcasterChannelsQueryCache.keys(), + ]; + const followedChannelQueryPromises = fidsInFollowedChannelQueryCache + .map(fid => { + const entry = this.getCachedFollowedFarcasterChannelsEntryForFID(fid); + if (!entry) { + return null; + } + const { followedFarcasterChannels } = entry; + if (followedFarcasterChannels instanceof Promise) { + return followedFarcasterChannels; + } + return null; + }) + .filter(Boolean); + if (followedChannelQueryPromises.length > 0) { + const followedChannelQueryResults = await Promise.all( + followedChannelQueryPromises, + ); + for (const followedChannelQueryResult of followedChannelQueryResults) { + if (!followedChannelQueryResult) { + continue; + } + for (const channel of followedChannelQueryResult) { + if (channel.id !== channelID) { + continue; + } + return channel; + } + } + } + let farcasterChannel; try { farcasterChannel = await Promise.race([ @@ -279,6 +314,20 @@ followedFarcasterChannels, }); + if (followedFarcasterChannels) { + for (const channel of followedFarcasterChannels) { + const channelID = channel.id; + if (this.getCachedFarcasterChannelForChannelID(channelID)) { + continue; + } + this.farcasterChannelQueryCache.set(channelID, { + channelID, + expirationTime: Date.now() + cacheTimeout, + farcasterChannel: channel, + }); + } + } + return followedFarcasterChannels; })();