Prefetch feed refreshes to speed up 'load more posts'

zio/stable
Paul Frazee 2022-12-16 15:37:13 -06:00
parent 4ef3afb604
commit d7e71e079f
3 changed files with 25 additions and 6 deletions

View File

@ -202,6 +202,7 @@ export class FeedModel {
_loadMorePromise: Promise<void> | undefined _loadMorePromise: Promise<void> | undefined
_loadLatestPromise: Promise<void> | undefined _loadLatestPromise: Promise<void> | undefined
_updatePromise: Promise<void> | undefined _updatePromise: Promise<void> | undefined
_prefetchedRefresh: GetTimeline.Response | GetAuthorFeed.Response | undefined
// data // data
feed: FeedItemModel[] = [] feed: FeedItemModel[] = []
@ -221,6 +222,7 @@ export class FeedModel {
_loadMorePromise: false, _loadMorePromise: false,
_loadLatestPromise: false, _loadLatestPromise: false,
_updatePromise: false, _updatePromise: false,
_prefetchedRefresh: false,
}, },
{autoBind: true}, {autoBind: true},
) )
@ -278,7 +280,15 @@ export class FeedModel {
* Reset and load * Reset and load
*/ */
async refresh() { async refresh() {
return this.setup(true) if (this._prefetchedRefresh) {
await this._pendingWork()
this._replaceAll(this._prefetchedRefresh)
this.setHasNewLatest(false)
this._xIdle()
} else {
await this.setup(true)
}
this._prefetchedRefresh = undefined
} }
/** /**
@ -330,10 +340,19 @@ export class FeedModel {
} }
await this._pendingWork() await this._pendingWork()
const res = await this._getFeed({limit: 1}) const res = await this._getFeed({limit: 1})
this.setHasNewLatest( const prefetchedLatestUri = this._prefetchedRefresh?.data.feed[0]?.uri
res.data.feed[0] && const currentLatestUri = prefetchedLatestUri || this.pollCursor
(this.feed.length === 0 || res.data.feed[0].uri !== this.pollCursor), const receivedLatestUri = res.data.feed[0]
? res.data.feed[0].uri
: undefined
const hasNewLatest = Boolean(
receivedLatestUri &&
(this.feed.length === 0 || receivedLatestUri !== currentLatestUri),
) )
this.setHasNewLatest(hasNewLatest)
if (hasNewLatest && prefetchedLatestUri !== receivedLatestUri) {
this._prefetchedRefresh = await this._getFeed({limit: PAGE_SIZE})
}
} }
// state transitions // state transitions

View File

@ -38,7 +38,6 @@ export function AutoSizedImage({
Image.getSize( Image.getSize(
uri, uri,
(width: number, height: number) => { (width: number, height: number) => {
console.log('gotSize')
if (!aborted) { if (!aborted) {
setImgInfo({width, height}) setImgInfo({width, height})
} }

View File

@ -51,6 +51,7 @@ export const Home = observer(function Home({
if (hasSetup) { if (hasSetup) {
console.log('Updating home feed') console.log('Updating home feed')
store.me.mainFeed.update() store.me.mainFeed.update()
doPoll()
} else { } else {
store.nav.setTitle(navIdx, 'Home') store.nav.setTitle(navIdx, 'Home')
console.log('Fetching home feed') console.log('Fetching home feed')
@ -88,7 +89,7 @@ export const Home = observer(function Home({
onPressTryAgain={onPressTryAgain} onPressTryAgain={onPressTryAgain}
onScroll={onMainScroll} onScroll={onMainScroll}
/> />
{store.me.mainFeed.hasNewLatest ? ( {store.me.mainFeed.hasNewLatest && !store.me.mainFeed.isRefreshing ? (
<TouchableOpacity <TouchableOpacity
style={[ style={[
styles.loadLatest, styles.loadLatest,