diff --git a/Core/Debouncer.swift b/Core/Debouncer.swift index 9bb42d8ab4..094737b956 100644 --- a/Core/Debouncer.swift +++ b/Core/Debouncer.swift @@ -21,9 +21,22 @@ import Foundation /// A class that provides a debouncing mechanism. public final class Debouncer { + private let runLoop: RunLoop + private let mode: RunLoop.Mode private var timer: Timer? - public init() {} + /// Initializes a new instance of `Debouncer`. + /// + /// - Parameters: + /// - runLoop: The `RunLoop` on which the debounced actions will be scheduled. Defaults to the current run loop. + /// + /// - mode: The `RunLoop.Mode` in which the debounced actions will be scheduled. Defaults to `.default`. + /// + /// Use `RunLoop.main` for UI-related actions to ensure they run on the main thread. + public init(runLoop: RunLoop = .current, mode: RunLoop.Mode = .default) { + self.runLoop = runLoop + self.mode = mode + } /// Debounces the provided block of code, executing it after a specified time interval elapses. /// - Parameters: @@ -41,7 +54,7 @@ public final class Debouncer { block() }) - RunLoop.main.add(timer, forMode: .common) + runLoop.add(timer, forMode: mode) self.timer = timer } diff --git a/DuckDuckGo/TabViewController.swift b/DuckDuckGo/TabViewController.swift index 0a1ab23950..b04ec3a86f 100644 --- a/DuckDuckGo/TabViewController.swift +++ b/DuckDuckGo/TabViewController.swift @@ -186,7 +186,7 @@ class TabViewController: UIViewController { let syncService: DDGSyncing - private let daxDialogsDebouncer = Debouncer() + private let daxDialogsDebouncer = Debouncer(mode: .common) public var url: URL? { willSet {