mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-06-16 05:55:20 +00:00
50 lines
1.6 KiB
Swift
50 lines
1.6 KiB
Swift
import Foundation
|
|
import Postbox
|
|
import SwiftSignalKit
|
|
|
|
final class ChatMessageThrottledProcessingManager {
|
|
private let queue = Queue(target: Queue.concurrentBackgroundQueue())
|
|
|
|
var process: ((Set<MessageId>) -> Void)?
|
|
|
|
private var timer: SwiftSignalKit.Timer?
|
|
private var processed = Set<MessageId>()
|
|
private var buffer = Set<MessageId>()
|
|
|
|
func setProcess(process: @escaping (Set<MessageId>) -> Void) {
|
|
self.queue.async {
|
|
self.process = process
|
|
}
|
|
}
|
|
|
|
func add(_ messageIds: [MessageId]) {
|
|
self.queue.async {
|
|
for id in messageIds {
|
|
if !self.processed.contains(id) {
|
|
self.processed.insert(id)
|
|
self.buffer.insert(id)
|
|
}
|
|
}
|
|
|
|
if self.timer == nil {
|
|
var completionImpl: (() -> Void)?
|
|
let timer = SwiftSignalKit.Timer(timeout: 1.0, 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
|
|
}
|
|
let buffer = strongSelf.buffer
|
|
strongSelf.buffer.removeAll()
|
|
strongSelf.process?(buffer)
|
|
}
|
|
}
|
|
self.timer = timer
|
|
timer.start()
|
|
}
|
|
}
|
|
}
|
|
}
|