-
-
Save wongzigii/adda2ab36498b4f4ae6d49e77451d67a to your computer and use it in GitHub Desktop.
Revisions
-
Fausto-R91 revised this gist
Jan 2, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. open class func debounce(delay: TimeInterval, underlyingQueue: DispatchQueue? = nil, action: @escaping () -> Void) -> (() -> Void) { // Init a new serial queue let queue = OperationQueue() queue.maxConcurrentOperationCount = 1 -
Fausto-R91 revised this gist
Jan 2, 2017 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 = false for op in queue.operations { if op.isFinished || op.isCancelled { continue } isExecuting = op.isExecuting && op.name == actionOpName break } // print("isExecuting: \(isExecuting), count: \(queue.operations.count)") -
Fausto-R91 revised this gist
Jan 2, 2017 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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() -
Fausto-R91 revised this gist
Jan 2, 2017 . 1 changed file with 11 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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" // 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 { -
Fausto-R91 renamed this gist
Jan 2, 2017 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
Fausto-R91 created this gist
Jan 2, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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()