Batch notification loads to avoid layout reflows due to async
This commit is contained in:
parent
b9c9895c45
commit
6ec33dd89f
3 changed files with 86 additions and 28 deletions
|
@ -1,6 +1,7 @@
|
|||
import {makeAutoObservable} from 'mobx'
|
||||
import {makeAutoObservable, runInAction} from 'mobx'
|
||||
import * as ListNotifications from '../../third-party/api/src/client/types/app/bsky/notification/list'
|
||||
import {RootStoreModel} from './root-store'
|
||||
import {PostThreadViewModel} from './post-thread-view'
|
||||
import {Declaration} from './_common'
|
||||
import {hasProp} from '../lib/type-guards'
|
||||
import {APP_BSKY_GRAPH} from '../../third-party/api'
|
||||
|
@ -34,6 +35,9 @@ export class NotificationsViewItemModel implements GroupedNotification {
|
|||
indexedAt: string = ''
|
||||
additional?: NotificationsViewItemModel[]
|
||||
|
||||
// additional data
|
||||
additionalPost?: PostThreadViewModel
|
||||
|
||||
constructor(
|
||||
public rootStore: RootStoreModel,
|
||||
reactKey: string,
|
||||
|
@ -89,6 +93,13 @@ export class NotificationsViewItemModel implements GroupedNotification {
|
|||
return this.reason === 'assertion'
|
||||
}
|
||||
|
||||
get needsAdditionalData() {
|
||||
if (this.isUpvote || this.isRepost || this.isTrend || this.isReply) {
|
||||
return !this.additionalPost
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
get isInvite() {
|
||||
return (
|
||||
this.isAssertion && this.record.assertion === APP_BSKY_GRAPH.AssertMember
|
||||
|
@ -107,6 +118,27 @@ export class NotificationsViewItemModel implements GroupedNotification {
|
|||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
async fetchAdditionalData() {
|
||||
if (!this.needsAdditionalData) {
|
||||
return
|
||||
}
|
||||
let postUri
|
||||
if (this.isReply) {
|
||||
postUri = this.uri
|
||||
} else if (this.isUpvote || this.isRead || this.isTrend) {
|
||||
postUri = this.subjectUri
|
||||
}
|
||||
if (postUri) {
|
||||
this.additionalPost = new PostThreadViewModel(this.rootStore, {
|
||||
uri: postUri,
|
||||
depth: 0,
|
||||
})
|
||||
await this.additionalPost.setup().catch(e => {
|
||||
console.error('Failed to load post needed by notification', e)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class NotificationsViewModel {
|
||||
|
@ -246,7 +278,7 @@ export class NotificationsViewModel {
|
|||
limit: PAGE_SIZE,
|
||||
})
|
||||
const res = await this.rootStore.api.app.bsky.notification.list(params)
|
||||
this._replaceAll(res)
|
||||
await this._replaceAll(res)
|
||||
this._xIdle()
|
||||
} catch (e: any) {
|
||||
this._xIdle(`Failed to load notifications: ${e.toString()}`)
|
||||
|
@ -264,7 +296,7 @@ export class NotificationsViewModel {
|
|||
before: this.loadMoreCursor,
|
||||
})
|
||||
const res = await this.rootStore.api.app.bsky.notification.list(params)
|
||||
this._appendAll(res)
|
||||
await this._appendAll(res)
|
||||
this._xIdle()
|
||||
} catch (e: any) {
|
||||
this._xIdle(`Failed to load notifications: ${e.toString()}`)
|
||||
|
@ -296,25 +328,37 @@ export class NotificationsViewModel {
|
|||
}
|
||||
}
|
||||
|
||||
private _replaceAll(res: ListNotifications.Response) {
|
||||
private async _replaceAll(res: ListNotifications.Response) {
|
||||
this.notifications.length = 0
|
||||
this._appendAll(res)
|
||||
return this._appendAll(res)
|
||||
}
|
||||
|
||||
private _appendAll(res: ListNotifications.Response) {
|
||||
private async _appendAll(res: ListNotifications.Response) {
|
||||
this.loadMoreCursor = res.data.cursor
|
||||
this.hasMore = !!this.loadMoreCursor
|
||||
let counter = this.notifications.length
|
||||
const promises = []
|
||||
const itemModels: NotificationsViewItemModel[] = []
|
||||
for (const item of groupNotifications(res.data.notifications)) {
|
||||
this._append(counter++, item)
|
||||
const itemModel = new NotificationsViewItemModel(
|
||||
this.rootStore,
|
||||
`item-${counter++}`,
|
||||
item,
|
||||
)
|
||||
if (itemModel.needsAdditionalData) {
|
||||
promises.push(itemModel.fetchAdditionalData())
|
||||
}
|
||||
itemModels.push(itemModel)
|
||||
}
|
||||
}
|
||||
|
||||
private _append(keyId: number, item: GroupedNotification) {
|
||||
// TODO: validate .record
|
||||
this.notifications.push(
|
||||
new NotificationsViewItemModel(this.rootStore, `item-${keyId}`, item),
|
||||
)
|
||||
await Promise.all(promises).catch(e => {
|
||||
console.error(
|
||||
'Uncaught failure during notifications-view _appendAll()',
|
||||
e,
|
||||
)
|
||||
})
|
||||
runInAction(() => {
|
||||
this.notifications = this.notifications.concat(itemModels)
|
||||
})
|
||||
}
|
||||
|
||||
private _updateAll(res: ListNotifications.Response) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue