Skip to content

Commit

Permalink
Replace old visible rooms range with subscriptions in the room list. (#…
Browse files Browse the repository at this point in the history
…3014)

* Add a SubscriptionTracker actor to RoomProxy.

* Request subscriptions when scrolling rooms.

* Stop unsubscribing in preparation for Simplified Sliding Sync.
  • Loading branch information
pixlwave authored Jul 8, 2024
1 parent bf9e1d6 commit a1329d9
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 58 deletions.
2 changes: 0 additions & 2 deletions ElementX/Sources/FlowCoordinators/RoomFlowCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -680,8 +680,6 @@ class RoomFlowCoordinator: FlowCoordinatorProtocol {
navigationStackCoordinator.setRootCoordinator(nil, animated: false)
}

roomProxy?.unsubscribeFromUpdates()

timelineController = nil

actionsSubject.send(.finished)
Expand Down
35 changes: 0 additions & 35 deletions ElementX/Sources/Mocks/Generated/GeneratedMocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8259,41 +8259,6 @@ class RoomProxyMock: RoomProxyProtocol {
subscribeForUpdatesCallsCount += 1
await subscribeForUpdatesClosure?()
}
//MARK: - unsubscribeFromUpdates

var unsubscribeFromUpdatesUnderlyingCallsCount = 0
var unsubscribeFromUpdatesCallsCount: Int {
get {
if Thread.isMainThread {
return unsubscribeFromUpdatesUnderlyingCallsCount
} else {
var returnValue: Int? = nil
DispatchQueue.main.sync {
returnValue = unsubscribeFromUpdatesUnderlyingCallsCount
}

return returnValue!
}
}
set {
if Thread.isMainThread {
unsubscribeFromUpdatesUnderlyingCallsCount = newValue
} else {
DispatchQueue.main.sync {
unsubscribeFromUpdatesUnderlyingCallsCount = newValue
}
}
}
}
var unsubscribeFromUpdatesCalled: Bool {
return unsubscribeFromUpdatesCallsCount > 0
}
var unsubscribeFromUpdatesClosure: (() -> Void)?

func unsubscribeFromUpdates() {
unsubscribeFromUpdatesCallsCount += 1
unsubscribeFromUpdatesClosure?()
}
//MARK: - timelineFocusedOnEvent

var timelineFocusedOnEventEventIDNumberOfEventsUnderlyingCallsCount = 0
Expand Down
10 changes: 9 additions & 1 deletion ElementX/Sources/Services/Client/ClientProxyProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,15 @@ enum ClientProxyError: Error {
}

enum SlidingSyncConstants {
static let defaultTimelineLimit: UInt = 20
static let defaultTimelineLimit: UInt32 = 20
static let maximumVisibleRangeSize = 30
static let defaultRequiredState = [
RequiredState(key: "m.room.name", value: ""),
RequiredState(key: "m.room.topic", value: ""),
RequiredState(key: "m.room.avatar", value: ""),
RequiredState(key: "m.room.canonical_alias", value: ""),
RequiredState(key: "m.room.join_rules", value: "")
]
}

/// This struct represents the configuration that we are using to register the application through Pusher to Sygnal
Expand Down
19 changes: 2 additions & 17 deletions ElementX/Sources/Services/Room/RoomProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import UIKit
import MatrixRustSDK

class RoomProxy: RoomProxyProtocol {
private static var subscriptionCountPerRoom: [String: Int] = [:]

private let roomListItem: RoomListItemProtocol
private let room: RoomProtocol
let timeline: TimelineProxyProtocol
Expand Down Expand Up @@ -145,15 +143,10 @@ class RoomProxy: RoomProxyProtocol {
}

subscribedForUpdates = true
let settings = RoomSubscription(requiredState: [RequiredState(key: "m.room.name", value: ""),
RequiredState(key: "m.room.topic", value: ""),
RequiredState(key: "m.room.avatar", value: ""),
RequiredState(key: "m.room.canonical_alias", value: ""),
RequiredState(key: "m.room.join_rules", value: "")],
timelineLimit: UInt32(SlidingSyncConstants.defaultTimelineLimit),
let settings = RoomSubscription(requiredState: SlidingSyncConstants.defaultRequiredState,
timelineLimit: SlidingSyncConstants.defaultTimelineLimit,
includeHeroes: false) // We don't need heroes here as they're already included in the `all_rooms` list
roomListItem.subscribe(settings: settings)
Self.subscriptionCountPerRoom[roomListItem.id()] = (Self.subscriptionCountPerRoom[roomListItem.id()] ?? 0) + 1

await timeline.subscribeForUpdates()

Expand All @@ -162,14 +155,6 @@ class RoomProxy: RoomProxyProtocol {
subscribeToTypingNotifications()
}

func unsubscribeFromUpdates() {
Self.subscriptionCountPerRoom[roomListItem.id()] = max(0, (Self.subscriptionCountPerRoom[roomListItem.id()] ?? 0) - 1)

if Self.subscriptionCountPerRoom[roomListItem.id()] ?? 0 <= 0 {
roomListItem.unsubscribe()
}
}

func timelineFocusedOnEvent(eventID: String, numberOfEvents: UInt16) async -> Result<TimelineProxyProtocol, RoomProxyError> {
do {
let timeline = try await room.timelineFocusedOnEvent(eventId: eventID, numContextEvents: numberOfEvents, internalIdPrefix: UUID().uuidString)
Expand Down
2 changes: 0 additions & 2 deletions ElementX/Sources/Services/Room/RoomProxyProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ protocol RoomProxyProtocol {

func subscribeForUpdates() async

func unsubscribeFromUpdates()

func timelineFocusedOnEvent(eventID: String, numberOfEvents: UInt16) async -> Result<TimelineProxyProtocol, RoomProxyError>

func redact(_ eventID: String) async -> Result<Void, RoomProxyError>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class RoomSummaryProvider: RoomSummaryProviderProtocol {

/// Build a new summary provider with the given parameters
/// - Parameters:
/// - shouldUpdateVisibleRange: whether this summary provider should foward visible ranges
/// - shouldUpdateVisibleRange: whether this summary provider should forward visible ranges
/// 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,
Expand Down Expand Up @@ -124,6 +124,25 @@ class RoomSummaryProvider: RoomSummaryProviderProtocol {
guard shouldUpdateVisibleRange else {
return
}

// The scroll view content size based visible range calculations might create large ranges
// This is just a safety check to not overload the backend
var range = range
if range.upperBound - range.lowerBound > SlidingSyncConstants.maximumVisibleRangeSize {
let upperBound = range.lowerBound + SlidingSyncConstants.maximumVisibleRangeSize
range = range.lowerBound..<upperBound
}

MXLog.info("\(name): Requesting subscriptions for visible range: \(range)")

for index in range {
guard index < rooms.count else { return }
// Note, we must use the item received by the diff. Asking the roomListService to get
// a new item instance will result in /sync being called when already subscribed.
rooms[index].roomListItem.subscribe(settings: .init(requiredState: SlidingSyncConstants.defaultRequiredState,
timelineLimit: SlidingSyncConstants.defaultTimelineLimit,
includeHeroes: false))
}
}

func setFilter(_ filter: RoomSummaryProviderFilter) {
Expand Down

0 comments on commit a1329d9

Please sign in to comment.