Skip to content

Instantly share code, notes, and snippets.

@staminajim
Created January 8, 2019 11:47
Show Gist options
  • Save staminajim/b5e89c6611eef81910502db2a01f1a83 to your computer and use it in GitHub Desktop.
Save staminajim/b5e89c6611eef81910502db2a01f1a83 to your computer and use it in GitHub Desktop.
Simple CGD Based Debouncing / Deduping
// Copyright (c) 2019, SeatGeek, Inc
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// DispatchQueue+Dedupe.swift
// Created by James Van-As on 21/09/18.
import Foundation
extension DispatchQueue {
/**
- parameters:
- target: Object used as the sentinel for de-duplication.
- delay: The time window for de-duplication to occur
- work: The work item to be invoked on the queue.
Performs work only once for the given target, given the time window. The last added work closure
is the work that will finally execute.
Note: This is currently only safe to call from the main thread.
Example usage:
```
DispatchQueue.main.asyncDeduped(target: self, after: 1.0) { [weak self] in
self?.doTheWork()
}
```
*/
public func asyncDeduped(target: AnyObject, after delay: TimeInterval, execute work: @escaping @convention(block) () -> Void) {
let dedupeIdentifier = DispatchQueue.dedupeIdentifierFor(target)
if let existingWorkItem = DispatchQueue.workItems.removeValue(forKey: dedupeIdentifier) {
existingWorkItem.cancel()
NSLog("Deduped work item: \(dedupeIdentifier)")
}
let workItem = DispatchWorkItem {
DispatchQueue.workItems.removeValue(forKey: dedupeIdentifier)
for ptr in DispatchQueue.weakTargets.allObjects {
if dedupeIdentifier == DispatchQueue.dedupeIdentifierFor(ptr as AnyObject) {
work()
NSLog("Ran work item: \(dedupeIdentifier)")
break
}
}
}
DispatchQueue.workItems[dedupeIdentifier] = workItem
DispatchQueue.weakTargets.addPointer(Unmanaged.passUnretained(target).toOpaque())
asyncAfter(deadline: .now() + delay, execute: workItem)
}
}
// MARK: - Static Properties for De-Duping
private extension DispatchQueue {
static var workItems = [AnyHashable : DispatchWorkItem]()
static var weakTargets = NSPointerArray.weakObjects()
static func dedupeIdentifierFor(_ object: AnyObject) -> String {
return "\(Unmanaged.passUnretained(object).toOpaque())." + String(describing: object)
}
}
@ptrkstr
Copy link

ptrkstr commented Dec 28, 2021

Thank you for sharing this, it's fantastic!

@staminajim
Copy link
Author

No worries. To be honest I've moved away from using CGD in my current project so haven't used it in a while. Hopefully it still works!

@S2Ler
Copy link

S2Ler commented Mar 20, 2022

Important to note that this code will have a data race if this method called from a different queue that the target one or the target queue is concurrent.

@hoanphan
Copy link

thank you for sharing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment