Keep fetching suggestions to reach a full list

zio/stable
Paul Frazee 2022-11-22 15:53:23 -06:00
parent 0f735fc575
commit 99b50e2fd5
1 changed files with 17 additions and 10 deletions

View File

@ -50,10 +50,6 @@ export class SuggestedActorsViewModel {
await this._fetch(true) await this._fetch(true)
} }
async loadMore() {
// TODO
}
// state transitions // state transitions
// = // =
@ -74,19 +70,30 @@ export class SuggestedActorsViewModel {
// = // =
private async _fetch(isRefreshing = false) { private async _fetch(isRefreshing = false) {
this.suggestions.length = 0
this._xLoading(isRefreshing) this._xLoading(isRefreshing)
let cursor
let res
try { try {
const res = await this.rootStore.api.app.bsky.actor.getSuggestions() do {
this._replaceAll(res) res = await this.rootStore.api.app.bsky.actor.getSuggestions({
limit: 20,
cursor,
})
this._appendAll(res)
cursor = res.data.cursor
} while (
cursor &&
res.data.actors.length === 20 &&
this.suggestions.length < 20
)
this._xIdle() this._xIdle()
} catch (e: any) { } catch (e: any) {
this._xIdle(e.toString()) this._xIdle(e.toString())
} }
} }
private _replaceAll(res: GetSuggestions.Response) { private _appendAll(res: GetSuggestions.Response) {
this.suggestions.length = 0
let counter = 0
for (const item of res.data.actors) { for (const item of res.data.actors) {
if (item.did === this.rootStore.me.did) { if (item.did === this.rootStore.me.did) {
continue // skip self continue // skip self
@ -95,7 +102,7 @@ export class SuggestedActorsViewModel {
continue // skip already-followed users continue // skip already-followed users
} }
this._append({ this._append({
_reactKey: `item-${counter++}`, _reactKey: `item-${this.suggestions.length}`,
...item, ...item,
}) })
} }