Fixing loadInitial happening multiple times

This commit is contained in:
Shadowfacts 2022-09-24 11:31:52 -04:00
parent 426b31d46c
commit d18a4b3c42
1 changed files with 13 additions and 3 deletions

View File

@ -29,7 +29,7 @@ actor TimelineLikeController<Item> {
unowned var delegate: any TimelineLikeControllerDelegate<Item>
private(set) var state = State.idle {
private(set) var state = State.notLoadedInitial {
willSet {
precondition(state.canTransition(to: newValue))
logger.debug("State: \(self.state.debugDescription, privacy: .public) -> \(newValue.debugDescription, privacy: .public)")
@ -41,7 +41,7 @@ actor TimelineLikeController<Item> {
}
func loadInitial() async {
guard state == .idle else {
guard state == .notLoadedInitial else {
return
}
let token = LoadAttemptToken()
@ -120,6 +120,7 @@ actor TimelineLikeController<Item> {
}
enum State: Equatable, CustomDebugStringConvertible {
case notLoadedInitial
case idle
case loadingInitial(LoadAttemptToken, hasAddedLoadingIndicator: Bool)
case loadingNewer(LoadAttemptToken)
@ -128,6 +129,8 @@ actor TimelineLikeController<Item> {
var debugDescription: String {
switch self {
case .notLoadedInitial:
return "notLoadedInitial"
case .idle:
return "idle"
case .loadingInitial(let token, let hasAddedLoadingIndicator):
@ -143,9 +146,16 @@ actor TimelineLikeController<Item> {
func canTransition(to: State) -> Bool {
switch self {
case .notLoadedInitial:
switch to {
case .loadingInitial(_, hasAddedLoadingIndicator: _):
return true
default:
return false
}
case .idle:
switch to {
case .loadingInitial(_, _), .loadingNewer(_)/*, .waitingForLoadOlderPermission*/, .loadingOlder(_, _):
case .loadingNewer(_)/*, .waitingForLoadOlderPermission*/, .loadingOlder(_, _):
return true
default:
return false