Get rid of maxInterval from CoalescingQueue because it didn’t actually work right and isn’t probably needed.

This commit is contained in:
Brent Simmons 2024-06-16 13:02:16 -07:00
parent 9227924f4e
commit 64fc1867c4
6 changed files with 19 additions and 15 deletions

View File

@ -7,6 +7,7 @@
//
import Foundation
import os
// Use when you want to coalesce calls for something like updating visible table cells.
// Calls are uniqued. If you add a call with the same target and selector as a previous call, youll just get one call.
@ -32,31 +33,32 @@ struct QueueCall: Equatable {
@MainActor @objc public final class CoalescingQueue: NSObject {
@MainActor public static let standard = CoalescingQueue(name: "Standard", interval: 0.05, maxInterval: 0.1)
@MainActor public static let standard = CoalescingQueue(name: "Standard", interval: 0.05)
public let name: String
public var isPaused = false
private let interval: TimeInterval
private let maxInterval: TimeInterval
private var lastCallTime = Date.distantFuture
private var timer: Timer? = nil
private var calls = [QueueCall]()
public init(name: String, interval: TimeInterval = 0.05, maxInterval: TimeInterval = 2.0) {
private static let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "CoalescingQueue")
private var logger: Logger {
Self.logger
}
public init(name: String, interval: TimeInterval = 0.05) {
self.name = name
self.interval = interval
self.maxInterval = maxInterval
}
public func add(_ target: AnyObject, _ selector: Selector) {
let queueCall = QueueCall(target: target, selector: selector)
add(queueCall)
if Date().timeIntervalSince1970 - lastCallTime.timeIntervalSince1970 > maxInterval {
timerDidFire(nil)
}
}
public func performCallsImmediately() {
guard !isPaused else { return }
logger.info("CoalescingQueue performing calls: \(self.name)")
let callsToMake = calls // Make a copy in case calls are added to the queue while performing calls.
resetCalls()
for call in callsToMake {
@ -68,12 +70,14 @@ struct QueueCall: Equatable {
lastCallTime = Date()
performCallsImmediately()
}
}
private extension CoalescingQueue {
func add(_ call: QueueCall) {
logger.info("CoalescingQueue adding to queue: \(self.name)")
restartTimer()
if !calls.contains(call) {

View File

@ -37,7 +37,7 @@ public protocol DataFileDelegate: AnyObject {
public init(fileURL: URL) {
self.fileURL = fileURL
self.saveQueue = CoalescingQueue(name: "DataFile \(fileURL.absoluteString)", interval: 1.0, maxInterval: 2.0)
self.saveQueue = CoalescingQueue(name: "DataFile \(fileURL.absoluteString)", interval: 1.0)
}
public func markAsDirty() {

View File

@ -163,7 +163,7 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr
private var currentRowHeight: CGFloat = 0.0
private var didRegisterForNotifications = false
static let fetchAndMergeArticlesQueue = CoalescingQueue(name: "Fetch and Merge Articles", interval: 0.5, maxInterval: 2.0)
static let fetchAndMergeArticlesQueue = CoalescingQueue(name: "Fetch and Merge Articles", interval: 0.5)
private var sortDirection = AppDefaults.shared.timelineSortDirection {
didSet {

View File

@ -48,7 +48,7 @@ import Images
}
#endif
private let fetchUnreadCountsQueue = CoalescingQueue(name: "SmartFeed", interval: 1.0, maxInterval: 2.0)
private let fetchUnreadCountsQueue = CoalescingQueue(name: "SmartFeed", interval: 1.0)
private var fetchUnreadCountsTask: Task<Void, Never>?
private let delegate: SmartFeedDelegate

View File

@ -65,7 +65,7 @@ final class WebViewController: UIViewController {
private(set) var article: Article?
let scrollPositionQueue = CoalescingQueue(name: "Article Scroll Position", interval: 0.3, maxInterval: 0.3)
let scrollPositionQueue = CoalescingQueue(name: "Article Scroll Position", interval: 0.3)
var windowScrollY = 0
private var restoreWindowScrollY: Int?

View File

@ -31,7 +31,7 @@ class TimelineViewController: UITableViewController, UndoableCommandRunner {
weak var coordinator: SceneCoordinator!
var undoableCommands = [UndoableCommand]()
let scrollPositionQueue = CoalescingQueue(name: "Timeline Scroll Position", interval: 0.3, maxInterval: 1.0)
let scrollPositionQueue = CoalescingQueue(name: "Timeline Scroll Position", interval: 0.3)
private let keyboardManager = KeyboardManager(type: .timeline)
override var keyCommands: [UIKeyCommand]? {