Improve perf of pinned feeds with primary algo

This commit is contained in:
Eric Bailey
2024-04-18 09:53:41 -05:00
parent d976684ddf
commit 9075b123d5
+27 -42
View File
@@ -247,38 +247,34 @@ export function usePinnedFeedsInfos() {
const isPrimaryAlgoExperimentEnabled = useGate( const isPrimaryAlgoExperimentEnabled = useGate(
'reduced_onboarding_and_home_algo', 'reduced_onboarding_and_home_algo',
) )
const pinnedUris = (preferences?.feeds?.pinned ?? []).filter(f => { const primaryAlgo = preferences?.primaryAlgorithm
const pinnedUris = preferences?.feeds?.pinned ?? []
const feedUris = pinnedUris.filter(uri => getFeedTypeFromUri(uri) === 'feed')
const listUris = pinnedUris.filter(uri => getFeedTypeFromUri(uri) === 'list')
if ( if (
isPrimaryAlgoExperimentEnabled && isPrimaryAlgoExperimentEnabled &&
hasSession && hasSession &&
preferences?.primaryAlgorithm && primaryAlgo?.enabled &&
preferences?.primaryAlgorithm?.enabled primaryAlgo?.uri
) { ) {
// remove duplicate feed feedUris.unshift(primaryAlgo.uri)
return f !== preferences?.primaryAlgorithm.uri
} }
return true
}) // used for query key
const allUris = feedUris.concat(listUris)
return useQuery({ return useQuery({
staleTime: STALE.INFINITY, staleTime: STALE.INFINITY,
enabled: !isLoadingPrefs, enabled: !isLoadingPrefs,
queryKey: [ queryKey: [
pinnedFeedInfosQueryKeyRoot, pinnedFeedInfosQueryKeyRoot,
(hasSession ? 'authed:' : 'unauthed:') + pinnedUris.join(','), (hasSession ? 'authed:' : 'unauthed:') + allUris.join(','),
`primary:${
preferences?.primaryAlgorithm.uri
? preferences?.primaryAlgorithm.uri
: 'none'
}`,
], ],
queryFn: async () => { queryFn: async () => {
let resolved = new Map() let resolved = new Map()
// Get all feeds. We can do this in a batch. // Get all feeds. We can do this in a batch.
const feedUris = pinnedUris.filter(
uri => getFeedTypeFromUri(uri) === 'feed',
)
let feedsPromise = Promise.resolve() let feedsPromise = Promise.resolve()
if (feedUris.length > 0) { if (feedUris.length > 0) {
feedsPromise = getAgent() feedsPromise = getAgent()
@@ -293,9 +289,6 @@ export function usePinnedFeedsInfos() {
} }
// Get all lists. This currently has to be done individually. // Get all lists. This currently has to be done individually.
const listUris = pinnedUris.filter(
uri => getFeedTypeFromUri(uri) === 'list',
)
const listsPromises = listUris.map(listUri => const listsPromises = listUris.map(listUri =>
getAgent() getAgent()
.app.bsky.graph.getList({ .app.bsky.graph.getList({
@@ -308,34 +301,26 @@ export function usePinnedFeedsInfos() {
}), }),
) )
// The returned result will have the original order.
let result = [hasSession ? HOME_FEED_STUB : DISCOVER_FEED_STUB] let result = [hasSession ? HOME_FEED_STUB : DISCOVER_FEED_STUB]
let primaryAlgoPromise = Promise.resolve() await Promise.allSettled([feedsPromise, ...listsPromises])
if (
isPrimaryAlgoExperimentEnabled && // if primary algo is enabled and was fetched, add it to the front of the list
hasSession && if (primaryAlgo?.enabled && primaryAlgo?.uri) {
preferences?.primaryAlgorithm if (resolved.has(primaryAlgo.uri)) {
) { result = [resolved.get(primaryAlgo.uri), ...result]
const {enabled, uri} = preferences.primaryAlgorithm
// ONLY add the primary algo if we have a URI set
if (enabled && uri) {
primaryAlgoPromise = getAgent()
.app.bsky.feed.getFeedGenerator({
feed: uri,
})
.then(res => {
result.unshift(hydrateFeedGenerator(res.data.view))
})
} }
} }
await Promise.allSettled([ const pinnedUrisSansPrimary = pinnedUris.filter(uri => {
feedsPromise, if (primaryAlgo?.enabled) {
primaryAlgoPromise, return uri !== primaryAlgo?.uri
...listsPromises, }
]) return true
for (let pinnedUri of pinnedUris) { })
// order the feeds/lists in the order they were pinned
for (let pinnedUri of pinnedUrisSansPrimary) {
if (resolved.has(pinnedUri)) { if (resolved.has(pinnedUri)) {
result.push(resolved.get(pinnedUri)) result.push(resolved.get(pinnedUri))
} }