Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. //
  2. // Debouncer.swift
  3. //
  4. // Created by Sam Oakley on 20/03/2019.
  5. //
  6.  
  7. import Foundation
  8.  
  9. class Debouncer {
  10.  
  11. private static var pendingRequestWorkItems: [AnyHashable: DispatchWorkItem] = [:]
  12.  
  13. static func perform(context: AnyHashable, after: DispatchTimeInterval, block: @escaping () -> Void) {
  14. let pendingRequestWorkItem = pendingRequestWorkItems[context]
  15. pendingRequestWorkItem?.cancel()
  16.  
  17. let requestWorkItem = DispatchWorkItem(block: block)
  18. pendingRequestWorkItems[context] = requestWorkItem
  19. DispatchQueue.main.asyncAfter(deadline: .now() + after,
  20. execute: requestWorkItem)
  21. }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement