Add extended media polling

This commit is contained in:
Ilya Laktyushin
2022-09-01 14:25:48 +02:00
parent 39374476bb
commit 95ceb995d0
4 changed files with 101 additions and 27 deletions

View File

@@ -78,16 +78,33 @@ final class ChatMessageThrottledProcessingManager {
final class ChatMessageVisibleThrottledProcessingManager {
private let queue = Queue()
private let delay: Double
private let interval: Double
private var currentIds = Set<MessageId>()
var process: ((Set<MessageId>) -> Void)?
private var timer: SwiftSignalKit.Timer?
private let timer: SwiftSignalKit.Timer
init(delay: Double = 1.0) {
self.delay = delay
init(interval: Double = 5.0) {
self.interval = interval
var completionImpl: (() -> Void)?
let timer = SwiftSignalKit.Timer(timeout: self.interval, repeat: true, completion: {
completionImpl?()
}, queue: self.queue)
self.timer = timer
timer.start()
completionImpl = { [weak self] in
if let strongSelf = self, !strongSelf.currentIds.isEmpty {
strongSelf.process?(strongSelf.currentIds)
}
}
}
deinit {
self.timer.invalidate()
}
func setProcess(process: @escaping (Set<MessageId>) -> Void) {
@@ -100,21 +117,8 @@ final class ChatMessageVisibleThrottledProcessingManager {
self.queue.async {
if self.currentIds != ids {
self.currentIds = ids
if self.timer == nil {
var completionImpl: (() -> Void)?
let timer = SwiftSignalKit.Timer(timeout: self.delay, repeat: false, completion: {
completionImpl?()
}, queue: self.queue)
completionImpl = { [weak self, weak timer] in
if let strongSelf = self {
if let timer = timer, strongSelf.timer === timer {
strongSelf.timer = nil
}
strongSelf.process?(strongSelf.currentIds)
}
}
self.timer = timer
timer.start()
if !self.currentIds.isEmpty {
self.process?(self.currentIds)
}
}
}