Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds (optional) custom DispatchQueue for Debouncer #44

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Sources/Repeat/Debouncer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,19 @@ open class Debouncer {
/// Internal timer to fire callback event.
private var timer: Repeater?

/// Dispatch queue of the timer
private let queue: DispatchQueue?

/// Initialize a new debouncer with given delay and callback.
/// Debouncer class to delay functions that only get delay each other until the timer fires.
///
/// - Parameters:
/// - delay: delay interval
/// - callback: callback to activate
public init(_ delay: Repeater.Interval, callback: Callback? = nil) {
public init(_ delay: Repeater.Interval, callback: Callback? = nil, queue: DispatchQueue? = nil) {
self.delay = delay
self.callback = callback
self.queue = queue
}

/// Call debouncer to start the callback after the delayed time.
Expand All @@ -68,7 +72,7 @@ open class Debouncer {
}

if self.timer == nil {
self.timer = Repeater.once(after: self.delay, { [weak self] _ in
self.timer = Repeater.once(after: self.delay, queue: queue, { [weak self] _ in
guard let callback = self?.callback else {
debugPrint("Debouncer fired but callback not set.")
return
Expand Down