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
zio/stable
Paul Frazee 2023-07-01 14:25:13 -05:00 committed by GitHub
parent 93689ad73d
commit 2e93e4a919
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 46 deletions

View File

@ -278,13 +278,11 @@ export class PostsFeedModel {
* Check if new posts are available * Check if new posts are available
*/ */
async checkForLatest() { async checkForLatest() {
if (this.hasNewLatest) { if (this.hasNewLatest || this.isLoading) {
return return
} }
const res = await this._getFeed({limit: this.pageSize}) const res = await this._getFeed({limit: 1})
const tuner = new FeedTuner() this.setHasNewLatest(res.data.feed[0]?.post.uri !== this.pollCursor)
const slices = tuner.tune(res.data.feed, this.feedTuners)
this.setHasNewLatest(slices[0]?.uri !== this.slices[0]?.uri)
} }
/** /**

View File

@ -8,7 +8,6 @@ import isEqual from 'lodash.isequal'
import {NativeStackScreenProps, HomeTabNavigatorParams} from 'lib/routes/types' import {NativeStackScreenProps, HomeTabNavigatorParams} from 'lib/routes/types'
import {PostsFeedModel} from 'state/models/feeds/posts' import {PostsFeedModel} from 'state/models/feeds/posts'
import {withAuthRequired} from 'view/com/auth/withAuthRequired' import {withAuthRequired} from 'view/com/auth/withAuthRequired'
import {useTabFocusEffect} from 'lib/hooks/useTabFocusEffect'
import {Feed} from '../com/posts/Feed' import {Feed} from '../com/posts/Feed'
import {FollowingEmptyState} from 'view/com/posts/FollowingEmptyState' import {FollowingEmptyState} from 'view/com/posts/FollowingEmptyState'
import {CustomFeedEmptyState} from 'view/com/posts/CustomFeedEmptyState' 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 // fires when page within screen is activated/deactivated
// - check for latest // - check for latest
React.useEffect(() => { React.useEffect(() => {
if (isPageFocused && isScreenFocused) { if (!isPageFocused || !isScreenFocused) {
feed.checkForLatest() 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 () => { 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(() => { const onPressCompose = React.useCallback(() => {
track('HomeScreen:PressCompose') track('HomeScreen:PressCompose')