Guard against rooms prematurely unsubscribing from sliding sync:

- with the advent of permalinks the same room can be now pushed multiple times onto the stack which means multiple calls to `subscribeForUpdates`
- the reverse doesn't hold and one call to `unsubscribeFromUpdates` will unsubscribe all the rooms at the same time
- we guard against that by tracking the number of subscriptions and calling the underlying `roomListItem.unsubscribe()` when the count goes back to zero
This commit is contained in:
Stefan Ceriu
2024-04-18 17:21:34 +03:00
committed by Stefan Ceriu
parent 20d57e4998
commit 8ef6fee95f
@@ -21,6 +21,8 @@ import UIKit
import MatrixRustSDK
class RoomProxy: RoomProxyProtocol {
private static var subscriptionCountPerRoom: [String: Int] = [:]
private let roomListItem: RoomListItemProtocol
private let room: RoomProtocol
let timeline: TimelineProxyProtocol
@@ -89,6 +91,7 @@ class RoomProxy: RoomProxyProtocol {
RequiredState(key: "m.room.join_rules", value: "")],
timelineLimit: UInt32(SlidingSyncConstants.defaultTimelineLimit))
roomListItem.subscribe(settings: settings)
Self.subscriptionCountPerRoom[roomListItem.id()] = (Self.subscriptionCountPerRoom[roomListItem.id()] ?? 0) + 1
await timeline.subscribeForUpdates()
@@ -98,7 +101,11 @@ class RoomProxy: RoomProxyProtocol {
}
func unsubscribeFromUpdates() {
roomListItem.unsubscribe()
Self.subscriptionCountPerRoom[roomListItem.id()] = max(0, (Self.subscriptionCountPerRoom[roomListItem.id()] ?? 0) - 1)
if Self.subscriptionCountPerRoom[roomListItem.id()] ?? 0 <= 0 {
roomListItem.unsubscribe()
}
}
lazy var id: String = room.id()