Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix refresh behavior around a series of models (repost, graph, vote) #163

Merged
merged 2 commits into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions src/state/models/reposted-by-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@ export class RepostedByViewModel {
if (this._loadMorePromise) {
return this._loadMorePromise
}
if (!this.resolvedUri) {
await this._resolveUri()
}
this._loadMorePromise = this._loadMore(isRefreshing)
this._loadMorePromise = this._load(isRefreshing)
await this._loadMorePromise
this._loadMorePromise = undefined
}
Expand Down Expand Up @@ -106,25 +103,34 @@ export class RepostedByViewModel {
})
}

private async _loadMore(isRefreshing = false) {
this._xLoading(isRefreshing)
private async _load(replace = false) {
this._xLoading(replace)
try {
if (!this.resolvedUri) {
await this._resolveUri()
}
const params = Object.assign({}, this.params, {
uri: this.resolvedUri,
limit: PAGE_SIZE,
before: this.loadMoreCursor,
before: replace ? undefined : this.loadMoreCursor,
})
if (this.isRefreshing) {
this.repostedBy = []
}
const res = await this.rootStore.api.app.bsky.feed.getRepostedBy(params)
await this._appendAll(res)
if (replace) {
this._replaceAll(res)
} else {
this._appendAll(res)
}
this._xIdle()
} catch (e: any) {
this._xIdle(e)
}
}

private _replaceAll(res: GetRepostedBy.Response) {
this.repostedBy = []
this._appendAll(res)
}

private _appendAll(res: GetRepostedBy.Response) {
this.loadMoreCursor = res.data.cursor
this.hasMore = !!this.loadMoreCursor
Expand Down
17 changes: 9 additions & 8 deletions src/state/models/suggested-actors-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class SuggestedActorsViewModel {
if (this._loadMorePromise) {
return this._loadMorePromise
}
this._loadMorePromise = this._loadMore(isRefreshing)
this._loadMorePromise = this._load(isRefreshing)
await this._loadMorePromise
this._loadMorePromise = undefined
}
Expand All @@ -80,17 +80,18 @@ export class SuggestedActorsViewModel {
// loader functions
// =

private async _loadMore(isRefreshing = false) {
if (!this.hasMore) {
private async _load(replace = false) {
if (!replace && !this.hasMore) {
return
}
this._xLoading(isRefreshing)
this._xLoading(replace)
try {
if (this.isRefreshing) {
this.suggestions = []
let items: SuggestedActor[] = this.suggestions
if (replace) {
items = []
this.loadMoreCursor = undefined
}
let res
let items: SuggestedActor[] = []
do {
res = await this.rootStore.api.app.bsky.actor.getSuggestions({
limit: PAGE_SIZE,
Expand All @@ -111,7 +112,7 @@ export class SuggestedActorsViewModel {
)
} while (items.length < PAGE_SIZE && this.hasMore)
runInAction(() => {
this.suggestions = this.suggestions.concat(items)
this.suggestions = items
})
this._xIdle()
} catch (e: any) {
Expand Down
26 changes: 16 additions & 10 deletions src/state/models/user-followers-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class UserFollowersViewModel {
if (this._loadMorePromise) {
return this._loadMorePromise
}
this._loadMorePromise = this._loadMore(isRefreshing)
this._loadMorePromise = this._load(isRefreshing)
await this._loadMorePromise
this._loadMorePromise = undefined
}
Expand All @@ -94,28 +94,34 @@ export class UserFollowersViewModel {
// loader functions
// =

private async _loadMore(isRefreshing = false) {
if (!this.hasMore) {
private async _load(replace = false) {
if (!replace && !this.hasMore) {
return
}
this._xLoading(isRefreshing)
this._xLoading(replace)
try {
const params = Object.assign({}, this.params, {
limit: PAGE_SIZE,
before: this.loadMoreCursor,
before: replace ? undefined : this.loadMoreCursor,
})
if (this.isRefreshing) {
this.followers = []
}
const res = await this.rootStore.api.app.bsky.graph.getFollowers(params)
await this._appendAll(res)
if (replace) {
this._replaceAll(res)
} else {
this._appendAll(res)
}
this._xIdle()
} catch (e: any) {
this._xIdle(e)
}
}

private async _appendAll(res: GetFollowers.Response) {
private _replaceAll(res: GetFollowers.Response) {
this.followers = []
this._appendAll(res)
}

private _appendAll(res: GetFollowers.Response) {
this.loadMoreCursor = res.data.cursor
this.hasMore = !!this.loadMoreCursor
this.followers = this.followers.concat(res.data.followers)
Expand Down
26 changes: 16 additions & 10 deletions src/state/models/user-follows-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class UserFollowsViewModel {
if (this._loadMorePromise) {
return this._loadMorePromise
}
this._loadMorePromise = this._loadMore(isRefreshing)
this._loadMorePromise = this._load(isRefreshing)
await this._loadMorePromise
this._loadMorePromise = undefined
}
Expand All @@ -94,28 +94,34 @@ export class UserFollowsViewModel {
// loader functions
// =

private async _loadMore(isRefreshing = false) {
if (!this.hasMore) {
private async _load(replace = false) {
if (!replace && !this.hasMore) {
return
}
this._xLoading(isRefreshing)
this._xLoading(replace)
try {
const params = Object.assign({}, this.params, {
limit: PAGE_SIZE,
before: this.loadMoreCursor,
before: replace ? undefined : this.loadMoreCursor,
})
if (this.isRefreshing) {
this.follows = []
}
const res = await this.rootStore.api.app.bsky.graph.getFollows(params)
await this._appendAll(res)
if (replace) {
this._replaceAll(res)
} else {
this._appendAll(res)
}
this._xIdle()
} catch (e: any) {
this._xIdle(e)
}
}

private async _appendAll(res: GetFollows.Response) {
private _replaceAll(res: GetFollows.Response) {
this.follows = []
this._appendAll(res)
}

private _appendAll(res: GetFollows.Response) {
this.loadMoreCursor = res.data.cursor
this.hasMore = !!this.loadMoreCursor
this.follows = this.follows.concat(res.data.follows)
Expand Down
31 changes: 20 additions & 11 deletions src/state/models/votes-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ export class VotesViewModel {
if (this._loadMorePromise) {
return this._loadMorePromise
}
if (!this.resolvedUri) {
await this._resolveUri()
}
this._loadMorePromise = this._loadMore(isRefreshing)
this._loadMorePromise = this._load(isRefreshing)
await this._loadMorePromise
this._loadMorePromise = undefined
}
Expand Down Expand Up @@ -103,25 +100,37 @@ export class VotesViewModel {
})
}

private async _loadMore(isRefreshing = false) {
this._xLoading(isRefreshing)
private async _load(replace = false) {
if (!replace && !this.hasMore) {
return
}
this._xLoading(replace)
try {
if (!this.resolvedUri) {
await this._resolveUri()
}
const params = Object.assign({}, this.params, {
uri: this.resolvedUri,
limit: PAGE_SIZE,
before: this.loadMoreCursor,
before: replace ? undefined : this.loadMoreCursor,
})
if (this.isRefreshing) {
this.votes = []
}
const res = await this.rootStore.api.app.bsky.feed.getVotes(params)
this._appendAll(res)
if (replace) {
this._replaceAll(res)
} else {
this._appendAll(res)
}
this._xIdle()
} catch (e: any) {
this._xIdle(e)
}
}

private _replaceAll(res: GetVotes.Response) {
this.votes = []
this._appendAll(res)
}

private _appendAll(res: GetVotes.Response) {
this.loadMoreCursor = res.data.cursor
this.hasMore = !!this.loadMoreCursor
Expand Down