Improvements to feed assembly to avoid possible state issues (#1318)

* Avoid potential dropped posts due to pruning when checking for latest

* Add a sanity check to ensure dup react keys never occur (close #1315)
zio/stable
Paul Frazee 2023-08-28 17:54:59 -07:00 committed by GitHub
parent e2f0770b88
commit 5ee754e6f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 4 deletions

View File

@ -105,6 +105,7 @@ export class FeedTuner {
tune(
feed: FeedViewPost[],
tunerFns: FeedTunerFn[] = [],
{dryRun}: {dryRun: boolean} = {dryRun: false},
): FeedViewPostsSlice[] {
let slices: FeedViewPostsSlice[] = []
@ -156,11 +157,13 @@ export class FeedTuner {
}
}
if (!dryRun) {
for (const slice of slices) {
for (const item of slice.items) {
this.seenUris.add(item.post.uri)
}
}
}
return slices
}

View File

@ -277,7 +277,9 @@ export class PostsFeedModel {
}
const res = await this._getFeed({limit: 1})
if (res.data.feed[0]) {
const slices = this.tuner.tune(res.data.feed, this.feedTuners)
const slices = this.tuner.tune(res.data.feed, this.feedTuners, {
dryRun: true,
})
if (slices[0]) {
const sliceModel = new PostsFeedSliceModel(this.rootStore, slices[0])
if (sliceModel.moderation.content.filter) {
@ -374,6 +376,15 @@ export class PostsFeedModel {
const toAppend: PostsFeedSliceModel[] = []
for (const slice of slices) {
const sliceModel = new PostsFeedSliceModel(this.rootStore, slice)
const dupTest = (item: PostsFeedSliceModel) =>
item._reactKey === sliceModel._reactKey
// sanity check
// if a duplicate _reactKey passes through, the UI breaks hard
if (!replace) {
if (this.slices.find(dupTest) || toAppend.find(dupTest)) {
continue
}
}
toAppend.push(sliceModel)
}
runInAction(() => {