Stop the timeline from requesting back pagination whilst loading existing items. (#2969)

* Handle Rust error "can't subscribe to the back-pagination status on a focused timeline"

* Fix a bug where the timeline was attempting to paginate before adding the items.

* Stop the timeline from paginating whilst loading existing items.
This commit is contained in:
Doug
2024-06-27 14:20:44 +01:00
committed by GitHub
parent 41d40d8de0
commit d977efdf31
5 changed files with 94 additions and 35 deletions
@@ -23,7 +23,8 @@ final class TimelineProxy: TimelineProxyProtocol {
private var backPaginationStatusObservationToken: TaskHandle?
private let backPaginationSubscriptionSubject = CurrentValueSubject<PaginationStatus, Never>(.idle)
// The default values don't matter here, they will be updated when calling subscribeToPagination.
private let backPaginationStatusSubject = CurrentValueSubject<PaginationStatus, Never>(.timelineEndReached)
private let forwardPaginationStatusSubject = CurrentValueSubject<PaginationStatus, Never>(.timelineEndReached)
let isLive: Bool
@@ -48,14 +49,19 @@ final class TimelineProxy: TimelineProxyProtocol {
return
}
let paginationStatePublisher = backPaginationSubscriptionSubject
let paginationStatePublisher = backPaginationStatusSubject
.combineLatest(forwardPaginationStatusSubject)
.map { PaginationState(backward: $0.0, forward: $0.1) }
.eraseToAnyPublisher()
await subscribeToPagination()
innerTimelineProvider = await RoomTimelineProvider(timeline: timeline, isLive: isLive, paginationStatePublisher: paginationStatePublisher)
let provider = await RoomTimelineProvider(timeline: timeline, isLive: isLive, paginationStatePublisher: paginationStatePublisher)
// Make sure the existing items are built so that we have content in the timeline before
// determining whether or not the timeline should paginate to load more items.
await provider.waitForInitialItems()
innerTimelineProvider = provider
}
func fetchDetails(for eventID: String) {
@@ -75,6 +81,22 @@ final class TimelineProxy: TimelineProxyProtocol {
}
func paginateBackwards(requestSize: UInt16) async -> Result<Void, TimelineProxyError> {
// We can't subscribe to back pagination on detached timelines and as live timelines
// can be shared between multiple instances of the same room on the stack, it is
// safer to still use the subscription logic for back pagination when live.
await if isLive {
paginateBackwardsOnLive(requestSize: requestSize)
} else {
focussedPaginate(.backwards, requestSize: requestSize)
}
}
func paginateForwards(requestSize: UInt16) async -> Result<Void, TimelineProxyError> {
await focussedPaginate(.forwards, requestSize: requestSize)
}
/// Paginate backwards using the subscription from Rust to drive the pagination state.
private func paginateBackwardsOnLive(requestSize: UInt16) async -> Result<Void, TimelineProxyError> {
MXLog.info("Paginating backwards")
do {
@@ -88,26 +110,36 @@ final class TimelineProxy: TimelineProxyProtocol {
}
}
func paginateForwards(requestSize: UInt16) async -> Result<Void, TimelineProxyError> {
// This extra check is necessary as forwards pagination status doesn't support subscribing.
/// Paginate forward or backwards using our own logic to drive the pagination state as the
/// Rust subscription isn't allowed on focussed/detached timelines.
private func focussedPaginate(_ direction: PaginationDirection, requestSize: UInt16) async -> Result<Void, TimelineProxyError> {
let subject = switch direction {
case .backwards: backPaginationStatusSubject
case .forwards: forwardPaginationStatusSubject
}
// This extra check is necessary as detached timelines don't support subscribing to pagination status.
// We need it to make sure we send a valid status after a failure.
guard forwardPaginationStatusSubject.value == .idle else {
MXLog.error("Attempting to paginate forwards when already at the end.")
guard subject.value == .idle else {
MXLog.error("Attempting to paginate \(direction.rawValue) when already at the end.")
return .failure(.failedPaginatingEndReached)
}
MXLog.info("Paginating forwards")
forwardPaginationStatusSubject.send(.paginating)
MXLog.info("Paginating \(direction.rawValue)")
subject.send(.paginating)
do {
let timelineEndReached = try await timeline.focusedPaginateForwards(numEvents: requestSize)
MXLog.info("Finished paginating forwards")
let timelineEndReached = try await switch direction {
case .backwards: timeline.paginateBackwards(numEvents: requestSize)
case .forwards: timeline.focusedPaginateForwards(numEvents: requestSize)
}
MXLog.info("Finished paginating \(direction.rawValue)")
forwardPaginationStatusSubject.send(timelineEndReached ? .timelineEndReached : .idle)
subject.send(timelineEndReached ? .timelineEndReached : .idle)
return .success(())
} catch {
MXLog.error("Failed paginating forwards with error: \(error)")
forwardPaginationStatusSubject.send(.idle)
MXLog.error("Failed paginating \(direction.rawValue) with error: \(error)")
subject.send(.idle)
return .failure(.sdkError(error))
}
}
@@ -499,26 +531,31 @@ final class TimelineProxy: TimelineProxyProtocol {
}
private func subscribeToPagination() async {
let backPaginationListener = RoomPaginationStatusListener { [weak self] status in
guard let self else {
return
if isLive {
let backPaginationListener = RoomPaginationStatusListener { [weak self] status in
guard let self else {
return
}
switch status {
case .idle(let hitStartOfTimeline):
backPaginationStatusSubject.send(hitStartOfTimeline ? .timelineEndReached : .idle)
case .paginating:
backPaginationStatusSubject.send(.paginating)
}
}
switch status {
case .idle(let hitStartOfTimeline):
backPaginationSubscriptionSubject.send(hitStartOfTimeline ? .timelineEndReached : .idle)
case .paginating:
backPaginationSubscriptionSubject.send(.paginating)
do {
backPaginationStatusObservationToken = try await timeline.subscribeToBackPaginationStatus(listener: backPaginationListener)
} catch {
MXLog.error("Failed to subscribe to back pagination status with error: \(error)")
}
} else {
// Detached timelines don't support observation, set the initial state ourself.
backPaginationStatusSubject.send(.idle)
}
do {
backPaginationStatusObservationToken = try await timeline.subscribeToBackPaginationStatus(listener: backPaginationListener)
} catch {
MXLog.error("Failed to subscribe to back pagination status with error: \(error)")
}
// Forward pagination doesn't support observation, set the initial state ourself.
// Detached timelines don't support observation, set the initial state ourself.
forwardPaginationStatusSubject.send(isLive ? .timelineEndReached : .idle)
}
}