From 2e93e4a919c42ae13d93ec0b060b60119b1d6886 Mon Sep 17 00:00:00 2001 From: Paul Frazee Date: Sat, 1 Jul 2023 14:25:13 -0500 Subject: [PATCH] Performance improvement (#937) * Perf: Wait until a feed is visible in the home screen before fetching data * perf: update feed checkForLatest to use limit=1 * Fix lint --- src/state/models/feeds/posts.ts | 8 ++-- src/view/screens/Home.tsx | 75 +++++++++++++++------------------ 2 files changed, 37 insertions(+), 46 deletions(-) diff --git a/src/state/models/feeds/posts.ts b/src/state/models/feeds/posts.ts index cd5e3c05..4e6633d3 100644 --- a/src/state/models/feeds/posts.ts +++ b/src/state/models/feeds/posts.ts @@ -278,13 +278,11 @@ export class PostsFeedModel { * Check if new posts are available */ async checkForLatest() { - if (this.hasNewLatest) { + if (this.hasNewLatest || this.isLoading) { return } - const res = await this._getFeed({limit: this.pageSize}) - const tuner = new FeedTuner() - const slices = tuner.tune(res.data.feed, this.feedTuners) - this.setHasNewLatest(slices[0]?.uri !== this.slices[0]?.uri) + const res = await this._getFeed({limit: 1}) + this.setHasNewLatest(res.data.feed[0]?.post.uri !== this.pollCursor) } /** diff --git a/src/view/screens/Home.tsx b/src/view/screens/Home.tsx index 95fb6940..ff2b2a0b 100644 --- a/src/view/screens/Home.tsx +++ b/src/view/screens/Home.tsx @@ -8,7 +8,6 @@ import isEqual from 'lodash.isequal' import {NativeStackScreenProps, HomeTabNavigatorParams} from 'lib/routes/types' import {PostsFeedModel} from 'state/models/feeds/posts' import {withAuthRequired} from 'view/com/auth/withAuthRequired' -import {useTabFocusEffect} from 'lib/hooks/useTabFocusEffect' import {Feed} from '../com/posts/Feed' import {FollowingEmptyState} from 'view/com/posts/FollowingEmptyState' import {CustomFeedEmptyState} from 'view/com/posts/CustomFeedEmptyState' @@ -204,52 +203,46 @@ const FeedPage = observer( ) }, []) - // fires when screen is activated/deactivated - // - set up polls/listeners, update content - useFocusEffect( - React.useCallback(() => { - const softResetSub = store.onScreenSoftReset(onSoftReset) - const feedCleanup = feed.registerListeners() - const pollInterval = setInterval(doPoll, POLL_FREQ) - - screen('Feed') - store.log.debug('HomeScreen: Updating feed') - if (feed.hasContent) { - feed.update() - } - - return () => { - clearInterval(pollInterval) - softResetSub.remove() - feedCleanup() - } - }, [store, doPoll, onSoftReset, screen, feed]), - ) - // fires when tab is activated/deactivated - // - check for latest - useTabFocusEffect( - 'Home', - React.useCallback( - isInside => { - if (!isPageFocused || !isInside) { - return - } - feed.checkForLatest() - }, - [isPageFocused, feed], - ), - ) // fires when page within screen is activated/deactivated // - check for latest React.useEffect(() => { - if (isPageFocused && isScreenFocused) { - feed.checkForLatest() + if (!isPageFocused || !isScreenFocused) { + return } - isWeb && window.addEventListener('resize', listenForResize) + + const softResetSub = store.onScreenSoftReset(onSoftReset) + const feedCleanup = feed.registerListeners() + const pollInterval = setInterval(doPoll, POLL_FREQ) + + screen('Feed') + store.log.debug('HomeScreen: Updating feed') + feed.checkForLatest() + if (feed.hasContent) { + feed.update() + } + + if (isWeb) { + window.addEventListener('resize', listenForResize) + } + return () => { - isWeb && window.removeEventListener('resize', listenForResize) + clearInterval(pollInterval) + softResetSub.remove() + feedCleanup() + if (isWeb) { + isWeb && window.removeEventListener('resize', listenForResize) + } } - }, [isPageFocused, isScreenFocused, feed, listenForResize]) + }, [ + store, + doPoll, + onSoftReset, + screen, + feed, + isPageFocused, + isScreenFocused, + listenForResize, + ]) const onPressCompose = React.useCallback(() => { track('HomeScreen:PressCompose')