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