ios: add throttling for incoming messages

This commit is contained in:
Levitating Pineapple
2024-08-01 18:41:07 +03:00
parent bfab2d9fb6
commit e9797da238
3 changed files with 46 additions and 1 deletions
+1 -1
View File
@@ -1711,7 +1711,7 @@ func processReceivedMsg(_ res: ChatResponse) async {
let cItem = aChatItem.chatItem
await MainActor.run {
if active(user) {
m.addChatItem(cInfo, cItem)
NewItemThrottler.shared.receive(cItem, for: cInfo)
} else if cItem.isRcvNew && cInfo.ntfsEnabled {
m.increaseUnreadCounter(user: user)
}
@@ -0,0 +1,41 @@
//
// Collector.swift
// SimpleX (iOS)
//
// Created by User on 01/08/2024.
// Copyright © 2024 SimpleX Chat. All rights reserved.
//
import Foundation
import Combine
import SimpleXChat
class NewItemThrottler {
static let shared = NewItemThrottler()
private let subject = PassthroughSubject<Void, Never>()
private var bag = Set<AnyCancellable>()
private var accumulated = [(ChatInfo, ChatItem)]()
private init() {
subject
.throttle(for: 1, scheduler: DispatchQueue.main, latest: true)
.sink {
self.accumulated.forEach { cInfo, cItem in
ChatModel.shared.addChatItem(cInfo, cItem)
}
self.accumulated = []
}
.store(in: &bag)
}
func receive(_ cItem: ChatItem, for cInfo: ChatInfo) {
// Messages sent to the current chat are updated directly
if ChatModel.shared.chatId == cInfo.id {
ChatModel.shared.addChatItem(cInfo, cItem)
} else {
DispatchQueue.main.async { self.accumulated.append((cInfo, cItem)) }
subject.send()
}
}
}
@@ -200,6 +200,7 @@
CE38A29A2C3FCA54005ED185 /* ImageUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5CBD2859295711D700EC2CF4 /* ImageUtils.swift */; };
CE38A29C2C3FCD72005ED185 /* SwiftyGif in Frameworks */ = {isa = PBXBuildFile; productRef = CE38A29B2C3FCD72005ED185 /* SwiftyGif */; };
CE984D4B2C36C5D500E3AEFF /* ChatItemClipShape.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE984D4A2C36C5D500E3AEFF /* ChatItemClipShape.swift */; };
CEC171222C5BCD86006D5472 /* NewItemThrottler.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEC171212C5BCD86006D5472 /* NewItemThrottler.swift */; };
CEDE70222C48FD9500233B1F /* SEChatState.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEDE70212C48FD9500233B1F /* SEChatState.swift */; };
CEE723AA2C3BD3D70009AE93 /* ShareViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEE723A92C3BD3D70009AE93 /* ShareViewController.swift */; };
CEE723B12C3BD3D70009AE93 /* SimpleX SE.appex in Embed App Extensions */ = {isa = PBXBuildFile; fileRef = CEE723A72C3BD3D70009AE93 /* SimpleX SE.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
@@ -535,6 +536,7 @@
CE2AD9CD2C452A4D00E844E3 /* ChatUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatUtils.swift; sourceTree = "<group>"; };
CE3097FA2C4C0C9F00180898 /* ErrorAlert.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ErrorAlert.swift; sourceTree = "<group>"; };
CE984D4A2C36C5D500E3AEFF /* ChatItemClipShape.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatItemClipShape.swift; sourceTree = "<group>"; };
CEC171212C5BCD86006D5472 /* NewItemThrottler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewItemThrottler.swift; sourceTree = "<group>"; };
CEDE70212C48FD9500233B1F /* SEChatState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SEChatState.swift; sourceTree = "<group>"; };
CEE723A72C3BD3D70009AE93 /* SimpleX SE.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = "SimpleX SE.appex"; sourceTree = BUILT_PRODUCTS_DIR; };
CEE723A92C3BD3D70009AE93 /* ShareViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShareViewController.swift; sourceTree = "<group>"; };
@@ -786,6 +788,7 @@
8C74C3ED2C1B942300039E77 /* ChatWallpaper.swift */,
8C9BC2642C240D5100875A27 /* ThemeModeEditor.swift */,
CE984D4A2C36C5D500E3AEFF /* ChatItemClipShape.swift */,
CEC171212C5BCD86006D5472 /* NewItemThrottler.swift */,
);
path = Helpers;
sourceTree = "<group>";
@@ -1490,6 +1493,7 @@
18415C6C56DBCEC2CBBD2F11 /* WebRTCClient.swift in Sources */,
184152CEF68D2336FC2EBCB0 /* CallViewRenderers.swift in Sources */,
5CB634AD29E46CF70066AD6B /* LocalAuthView.swift in Sources */,
CEC171222C5BCD86006D5472 /* NewItemThrottler.swift in Sources */,
18415FEFE153C5920BFB7828 /* GroupWelcomeView.swift in Sources */,
18415F9A2D551F9757DA4654 /* CIVideoView.swift in Sources */,
184158C131FDB829D8A117EA /* VideoPlayerView.swift in Sources */,