mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-06-16 05:55:20 +00:00
57 lines
1.8 KiB
Swift
57 lines
1.8 KiB
Swift
import Foundation
|
|
import SwiftSignalKit
|
|
|
|
public typealias ListViewTransaction = (@escaping (Void) -> Void) -> Void
|
|
|
|
public final class ListViewTransactionQueue {
|
|
private var transactions: [ListViewTransaction] = []
|
|
public final var transactionCompleted: (Void) -> Void = { }
|
|
|
|
public init() {
|
|
}
|
|
|
|
public func addTransaction(_ transaction: @escaping ListViewTransaction) {
|
|
let beginTransaction = self.transactions.count == 0
|
|
self.transactions.append(transaction)
|
|
|
|
if beginTransaction {
|
|
transaction({ [weak self] in
|
|
if Thread.isMainThread {
|
|
if let strongSelf = self {
|
|
strongSelf.endTransaction()
|
|
}
|
|
} else {
|
|
Queue.mainQueue().async {
|
|
if let strongSelf = self {
|
|
strongSelf.endTransaction()
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
private func endTransaction() {
|
|
Queue.mainQueue().async {
|
|
self.transactionCompleted()
|
|
let _ = self.transactions.removeFirst()
|
|
|
|
if let nextTransaction = self.transactions.first {
|
|
nextTransaction({ [weak self] in
|
|
if Thread.isMainThread {
|
|
if let strongSelf = self {
|
|
strongSelf.endTransaction()
|
|
}
|
|
} else {
|
|
Queue.mainQueue().async {
|
|
if let strongSelf = self {
|
|
strongSelf.endTransaction()
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}
|