Skip to content

Instantly share code, notes, and snippets.

@wongzigii
Forked from Fausto-R91/debounce.swift
Created March 15, 2021 02:49
Show Gist options
  • Save wongzigii/adda2ab36498b4f4ae6d49e77451d67a to your computer and use it in GitHub Desktop.
Save wongzigii/adda2ab36498b4f4ae6d49e77451d67a to your computer and use it in GitHub Desktop.

Revisions

  1. @Fausto-R91 Fausto-R91 revised this gist Jan 2, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion debounce.swift
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ extension OperationQueue {
    /// - underlyingQueue: An optional background queue to run the function
    /// - action: The function to debounce.
    /// - Returns: Returns the new debounced function.
    static func debounce(delay: TimeInterval, underlyingQueue: DispatchQueue? = nil, action: @escaping () -> Void) -> (() -> Void) {
    open class func debounce(delay: TimeInterval, underlyingQueue: DispatchQueue? = nil, action: @escaping () -> Void) -> (() -> Void) {
    // Init a new serial queue
    let queue = OperationQueue()
    queue.maxConcurrentOperationCount = 1
  2. @Fausto-R91 Fausto-R91 revised this gist Jan 2, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions debounce.swift
    Original file line number Diff line number Diff line change
    @@ -19,13 +19,13 @@ extension OperationQueue {

    return {
    // Check if the first not cancelled or finished operation is executing
    var isExecuting = true
    var isExecuting = false
    for op in queue.operations {
    if op.isFinished || op.isCancelled {
    continue
    }

    isExecuting = op.name == actionOpName
    isExecuting = op.isExecuting && op.name == actionOpName
    break
    }
    // print("isExecuting: \(isExecuting), count: \(queue.operations.count)")
  3. @Fausto-R91 Fausto-R91 revised this gist Jan 2, 2017. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions debounce.swift
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,7 @@ extension OperationQueue {
    /// - delay: The number of seconds to delay.
    /// - underlyingQueue: An optional background queue to run the function
    /// - action: The function to debounce.
    /// - Returns: Returns the new debounced function.
    static func debounce(delay: TimeInterval, underlyingQueue: DispatchQueue? = nil, action: @escaping () -> Void) -> (() -> Void) {
    // Init a new serial queue
    let queue = OperationQueue()
  4. @Fausto-R91 Fausto-R91 revised this gist Jan 2, 2017. 1 changed file with 11 additions and 2 deletions.
    13 changes: 11 additions & 2 deletions debounce.swift
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,23 @@
    extension OperationQueue {

    /// Creates a debounced function that delays invoking `action` until after `delay` seconds have elapsed since the last time the debounced function was invoked.
    ///
    /// - Parameters:
    /// - delay: The number of seconds to delay.
    /// - underlyingQueue: An optional background queue to run the function
    /// - action: The function to debounce.
    static func debounce(delay: TimeInterval, underlyingQueue: DispatchQueue? = nil, action: @escaping () -> Void) -> (() -> Void) {
    // Init a new serial queue
    let queue = OperationQueue()
    queue.maxConcurrentOperationCount = 1
    queue.underlyingQueue = underlyingQueue

    let sleepOpName = "__SleepOp"
    let actionOpName = "__ActionOp"
    let sleepOpName = "__SleepOp" // Sleep operation name
    let actionOpName = "__ActionOp" // Action operation name


    return {
    // Check if the first not cancelled or finished operation is executing
    var isExecuting = true
    for op in queue.operations {
    if op.isFinished || op.isCancelled {
  5. @Fausto-R91 Fausto-R91 renamed this gist Jan 2, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. @Fausto-R91 Fausto-R91 created this gist Jan 2, 2017.
    61 changes: 61 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    extension OperationQueue {
    static func debounce(delay: TimeInterval, underlyingQueue: DispatchQueue? = nil, action: @escaping () -> Void) -> (() -> Void) {
    let queue = OperationQueue()
    queue.maxConcurrentOperationCount = 1
    queue.underlyingQueue = underlyingQueue

    let sleepOpName = "__SleepOp"
    let actionOpName = "__ActionOp"


    return {
    var isExecuting = true
    for op in queue.operations {
    if op.isFinished || op.isCancelled {
    continue
    }

    isExecuting = op.name == actionOpName
    break
    }
    // print("isExecuting: \(isExecuting), count: \(queue.operations.count)")
    if !isExecuting {
    queue.cancelAllOperations()
    }

    let sleepOp = BlockOperation(block: {
    Thread.sleep(forTimeInterval: delay)
    })
    sleepOp.name = sleepOpName

    let actionOp = BlockOperation(block: {
    action()
    })

    actionOp.name = actionOpName

    queue.addOperation(sleepOp)
    queue.addOperation(actionOp)
    }
    }
    }

    func testDebounce() {
    var gi = 0
    let queue = DispatchQueue.global(qos: .default)
    let debounced = OperationQueue.debounce(delay: 0.2, underlyingQueue: queue) {
    print("Fired: \(gi)")
    }

    for i in 0...9 {
    let r = Double(Int(arc4random()) % 2)
    let s = 0.1 + (0.2 * r)
    print("Run: \(i) (sleep for \(s)s)")

    gi = i
    debounced()
    Thread.sleep(forTimeInterval: s)
    }
    }

    testDebounce()