Skip to content

Commit

Permalink
Delay setting up subscriptions until the RoomListService is running i…
Browse files Browse the repository at this point in the history
…n order to avoid cancelling in flight initial sync requests
  • Loading branch information
stefanceriu committed Sep 5, 2024
1 parent dc85cb1 commit b43797f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
12 changes: 10 additions & 2 deletions ElementX/Sources/Services/Client/ClientProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ class ClientProxy: ClientProxyProtocol {
.asCurrentValuePublisher()
}

private let roomListServiceStateSubject = CurrentValueSubject<RoomListServiceState, Never>(.initial)

private var cancellables = Set<AnyCancellable>()

/// Will be `true` whilst the app cleans up and forces a logout. Prevents the sync service from restarting
Expand Down Expand Up @@ -759,6 +761,7 @@ class ClientProxy: ClientProxyProtocol {
shouldPrefixSenderName: true)

roomSummaryProvider = RoomSummaryProvider(roomListService: roomListService,
roomListServiceStatePublisher: roomListServiceStateSubject.asCurrentValuePublisher(),
eventStringBuilder: eventStringBuilder,
name: "AllRooms",
shouldUpdateVisibleRange: true,
Expand All @@ -767,6 +770,7 @@ class ClientProxy: ClientProxyProtocol {
try await roomSummaryProvider?.setRoomList(roomListService.allRooms())

alternateRoomSummaryProvider = RoomSummaryProvider(roomListService: roomListService,
roomListServiceStatePublisher: roomListServiceStateSubject.asCurrentValuePublisher(),
eventStringBuilder: eventStringBuilder,
name: "MessageForwarding",
notificationSettings: notificationSettings,
Expand Down Expand Up @@ -802,9 +806,13 @@ class ClientProxy: ClientProxyProtocol {

private func createRoomListServiceObserver(_ roomListService: RoomListService) -> TaskHandle {
roomListService.state(listener: RoomListStateListenerProxy { [weak self] state in
guard let self else { return }

MXLog.info("Received room list update: \(state)")
guard let self,
state != .error,

roomListServiceStateSubject.send(state)

guard state != .error,
state != .terminated else {
// The sync service is responsible of handling error and termination
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import MatrixRustSDK

class RoomSummaryProvider: RoomSummaryProviderProtocol {
private let roomListService: RoomListServiceProtocol
private let roomListServiceStatePublisher: CurrentValuePublisher<RoomListServiceState, Never>
private let eventStringBuilder: RoomEventStringBuilder
private let name: String
private let shouldUpdateVisibleRange: Bool
Expand All @@ -36,6 +37,7 @@ class RoomSummaryProvider: RoomSummaryProviderProtocol {
private var roomList: RoomListProtocol?

private var cancellables = Set<AnyCancellable>()
private var roomListServiceStateCancellable: AnyCancellable?
private var listUpdatesSubscriptionResult: RoomListEntriesWithDynamicAdaptersResult?
private var stateUpdatesTaskHandle: TaskHandle?

Expand Down Expand Up @@ -64,12 +66,14 @@ class RoomSummaryProvider: RoomSummaryProviderProtocol {
/// to the room list service through the `applyInput(input: .viewport(ranges` api. Only useful for
/// lists that need to update the visible range on Sliding Sync
init(roomListService: RoomListServiceProtocol,
roomListServiceStatePublisher: CurrentValuePublisher<RoomListServiceState, Never>,
eventStringBuilder: RoomEventStringBuilder,
name: String,
shouldUpdateVisibleRange: Bool = false,
notificationSettings: NotificationSettingsProxyProtocol,
appSettings: AppSettings) {
self.roomListService = roomListService
self.roomListServiceStatePublisher = roomListServiceStatePublisher
serialDispatchQueue = DispatchQueue(label: "io.element.elementx.roomsummaryprovider", qos: .default)
self.eventStringBuilder = eventStringBuilder
self.name = name
Expand All @@ -82,7 +86,16 @@ class RoomSummaryProvider: RoomSummaryProviderProtocol {
.sink { [weak self] in self?.updateRoomsWithDiffs($0) }
.store(in: &cancellables)

setupVisibleRangeObservers()
roomListServiceStateCancellable = roomListServiceStatePublisher
.receive(on: DispatchQueue.main)
.sink { [weak self] state in
guard let self else { return }

if state == .running {
setupVisibleRangeObservers()
roomListServiceStateCancellable = nil
}
}

setupNotificationSettingsSubscription()
}
Expand Down

0 comments on commit b43797f

Please sign in to comment.