mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-05 11:58:32 +00:00
Caching the room proxies and their messages. Improved performance throughout.
This commit is contained in:
@@ -10,7 +10,6 @@ import Foundation
|
||||
|
||||
class MemberDetailsProvider: MemberDetailsProviderProtocol {
|
||||
private let roomProxy: RoomProxyProtocol?
|
||||
private let processingQueue = DispatchQueue(label: "MemberDetailsProviderProcessingQueue")
|
||||
private var memberAvatars = [String: String]()
|
||||
private var memberDisplayNames = [String: String]()
|
||||
|
||||
@@ -31,25 +30,19 @@ class MemberDetailsProvider: MemberDetailsProviderProtocol {
|
||||
completion(.success(avatarURL))
|
||||
}
|
||||
|
||||
processingQueue.async {
|
||||
roomProxy.avatarURLForUserId(userId, completion: { [weak self] result in
|
||||
guard let self = self else {
|
||||
return
|
||||
}
|
||||
|
||||
switch result {
|
||||
case .success(let avatarURL):
|
||||
DispatchQueue.main.async {
|
||||
self.memberAvatars[userId] = avatarURL
|
||||
completion(.success(avatarURL))
|
||||
}
|
||||
case .failure:
|
||||
DispatchQueue.main.async {
|
||||
completion(.failure(.failedRetrievingUserAvatarURL))
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
roomProxy.avatarURLForUserId(userId, completion: { [weak self] result in
|
||||
guard let self = self else {
|
||||
return
|
||||
}
|
||||
|
||||
switch result {
|
||||
case .success(let avatarURL):
|
||||
self.memberAvatars[userId] = avatarURL
|
||||
completion(.success(avatarURL))
|
||||
case .failure:
|
||||
completion(.failure(.failedRetrievingUserAvatarURL))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func displayNameForUserId(_ userId: String) -> String? {
|
||||
@@ -65,24 +58,18 @@ class MemberDetailsProvider: MemberDetailsProviderProtocol {
|
||||
completion(.success(avatarURL))
|
||||
}
|
||||
|
||||
processingQueue.async {
|
||||
roomProxy.displayNameForUserId(userId, completion: { [weak self] result in
|
||||
guard let self = self else {
|
||||
return
|
||||
}
|
||||
|
||||
switch result {
|
||||
case .success(let displayName):
|
||||
DispatchQueue.main.async {
|
||||
self.memberDisplayNames[userId] = displayName
|
||||
completion(.success(displayName))
|
||||
}
|
||||
case .failure:
|
||||
DispatchQueue.main.async {
|
||||
completion(.failure(.failedRetrievingUserDisplayName))
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
roomProxy.displayNameForUserId(userId, completion: { [weak self] result in
|
||||
guard let self = self else {
|
||||
return
|
||||
}
|
||||
|
||||
switch result {
|
||||
case .success(let displayName):
|
||||
self.memberDisplayNames[userId] = displayName
|
||||
completion(.success(displayName))
|
||||
case .failure:
|
||||
completion(.failure(.failedRetrievingUserDisplayName))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ struct MockRoomProxy: RoomProxyProtocol {
|
||||
let displayName: String
|
||||
|
||||
let topic: String? = nil
|
||||
let lastMessage: String? = "Last message"
|
||||
let messages: [RoomMessageProtocol] = []
|
||||
|
||||
let avatarURL: String? = nil
|
||||
|
||||
@@ -35,7 +35,7 @@ struct MockRoomProxy: RoomProxyProtocol {
|
||||
|
||||
}
|
||||
|
||||
func paginateBackwards(count: UInt, callback: ((Result<[RoomMessageProtocol], RoomProxyError>) -> Void)?) {
|
||||
func paginateBackwards(count: UInt, callback: ((Result<Void, RoomProxyError>) -> Void)?) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -27,22 +27,31 @@ private class WeakRoomProxyWrapper: RoomDelegate {
|
||||
}
|
||||
}
|
||||
|
||||
class RoomProxy: RoomProxyProtocol, Equatable {
|
||||
class RoomProxy: RoomProxyProtocol {
|
||||
private let room: Room
|
||||
private let messageFactory: RoomMessageFactory
|
||||
private let processingQueue: DispatchQueue
|
||||
|
||||
private let generalProcessingQueue: DispatchQueue
|
||||
private let messageProcessingQueue: DispatchQueue
|
||||
|
||||
private var backwardStream: BackwardsStreamProtocol?
|
||||
|
||||
let callbacks = PassthroughSubject<RoomProxyCallback, Never>()
|
||||
|
||||
private(set) var messages: [RoomMessageProtocol]
|
||||
|
||||
init(room: Room, messageFactory: RoomMessageFactory) {
|
||||
self.room = room
|
||||
self.messageFactory = messageFactory
|
||||
processingQueue = DispatchQueue(label: "RoomProxyProcessingQueue")
|
||||
generalProcessingQueue = DispatchQueue(label: "RoomProxyGeneralProcessingQueue")
|
||||
messageProcessingQueue = DispatchQueue(label: "RoomProxyMessageProcessingQueue")
|
||||
messages = []
|
||||
|
||||
processingQueue.async {
|
||||
self.backwardStream = room.startLiveEventListener()
|
||||
messageProcessingQueue.async {
|
||||
let backwardStream = room.startLiveEventListener()
|
||||
DispatchQueue.main.async {
|
||||
self.backwardStream = backwardStream
|
||||
}
|
||||
}
|
||||
|
||||
room.setDelegate(delegate: WeakRoomProxyWrapper(roomProxy: self))
|
||||
@@ -80,22 +89,12 @@ class RoomProxy: RoomProxyProtocol, Equatable {
|
||||
room.isTombstoned()
|
||||
}
|
||||
|
||||
var lastMessage: String? {
|
||||
didSet {
|
||||
if lastMessage == oldValue {
|
||||
return
|
||||
}
|
||||
|
||||
callbacks.send(.updatedLastMessage)
|
||||
}
|
||||
}
|
||||
|
||||
var avatarURL: String? {
|
||||
room.avatarUrl()
|
||||
}
|
||||
|
||||
func avatarURLForUserId(_ userId: String, completion: @escaping (Result<String?, RoomProxyError>) -> Void) {
|
||||
processingQueue.async {
|
||||
generalProcessingQueue.async {
|
||||
do {
|
||||
let avatarURL = try self.room.memberAvatarUrl(userId: userId)
|
||||
|
||||
@@ -111,10 +110,10 @@ class RoomProxy: RoomProxyProtocol, Equatable {
|
||||
}
|
||||
|
||||
func displayNameForUserId(_ userId: String, completion: @escaping (Result<String?, RoomProxyError>) -> Void) {
|
||||
processingQueue.async {
|
||||
generalProcessingQueue.async {
|
||||
do {
|
||||
let displayName = try self.room.memberDisplayName(userId: userId)
|
||||
|
||||
|
||||
DispatchQueue.main.async {
|
||||
completion(.success(displayName))
|
||||
}
|
||||
@@ -127,10 +126,10 @@ class RoomProxy: RoomProxyProtocol, Equatable {
|
||||
}
|
||||
|
||||
func displayName(_ completion: @escaping (Result<String, RoomProxyError>) -> Void) {
|
||||
processingQueue.async {
|
||||
generalProcessingQueue.async {
|
||||
do {
|
||||
let displayName = try self.room.displayName()
|
||||
|
||||
|
||||
DispatchQueue.main.async {
|
||||
completion(.success(displayName))
|
||||
}
|
||||
@@ -142,8 +141,8 @@ class RoomProxy: RoomProxyProtocol, Equatable {
|
||||
}
|
||||
}
|
||||
|
||||
func paginateBackwards(count: UInt, callback: ((Result<[RoomMessageProtocol], RoomProxyError>) -> Void)?) {
|
||||
processingQueue.async {
|
||||
func paginateBackwards(count: UInt, callback: ((Result<Void, RoomProxyError>) -> Void)?) {
|
||||
messageProcessingQueue.async {
|
||||
guard let backwardStream = self.backwardStream else {
|
||||
DispatchQueue.main.async {
|
||||
callback?(.failure(.backwardStreamNotAvailable))
|
||||
@@ -153,29 +152,20 @@ class RoomProxy: RoomProxyProtocol, Equatable {
|
||||
|
||||
let messages = backwardStream.paginateBackwards(count: UInt64(count)).map { message in
|
||||
self.messageFactory.buildRoomMessageFrom(message)
|
||||
}
|
||||
}.reversed()
|
||||
|
||||
DispatchQueue.main.async {
|
||||
callback?(.success(messages))
|
||||
if self.lastMessage == nil {
|
||||
self.lastMessage = messages.last?.body ?? ""
|
||||
}
|
||||
self.messages.insert(contentsOf: messages, at: 0)
|
||||
callback?(.success(()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Equatable
|
||||
|
||||
static func == (lhs: RoomProxy, rhs: RoomProxy) -> Bool {
|
||||
lhs.id == rhs.id
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
fileprivate func appendMessage(_ message: AnyMessage) {
|
||||
let message = self.messageFactory.buildRoomMessageFrom(message)
|
||||
lastMessage = message.body
|
||||
|
||||
callbacks.send(.addedMessage(message))
|
||||
messages.append(message)
|
||||
callbacks.send(.updatedMessages)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,8 +17,7 @@ enum RoomProxyError: Error {
|
||||
}
|
||||
|
||||
enum RoomProxyCallback {
|
||||
case addedMessage(RoomMessageProtocol)
|
||||
case updatedLastMessage
|
||||
case updatedMessages
|
||||
}
|
||||
|
||||
protocol RoomProxyProtocol {
|
||||
@@ -32,7 +31,7 @@ protocol RoomProxyProtocol {
|
||||
var name: String? { get }
|
||||
|
||||
var topic: String? { get }
|
||||
var lastMessage: String? { get }
|
||||
var messages: [RoomMessageProtocol] { get }
|
||||
|
||||
var avatarURL: String? { get }
|
||||
|
||||
@@ -42,7 +41,7 @@ protocol RoomProxyProtocol {
|
||||
|
||||
func displayNameForUserId(_ userId: String, completion: @escaping (Result<String?, RoomProxyError>) -> Void)
|
||||
|
||||
func paginateBackwards(count: UInt, callback: ((Result<[RoomMessageProtocol], RoomProxyError>) -> Void)?)
|
||||
func paginateBackwards(count: UInt, callback: ((Result<Void, RoomProxyError>) -> Void)?)
|
||||
|
||||
var callbacks: PassthroughSubject<RoomProxyCallback, Never> { get }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user