aboutsummaryrefslogtreecommitdiff
path: root/Map/Debouncer.swift
diff options
context:
space:
mode:
authorRuben Beltran del Rio <ruben@unlimited.pizza>2021-02-04 23:46:55 +0100
committerRuben Beltran del Rio <ruben@unlimited.pizza>2021-02-04 23:46:55 +0100
commit77d0155b661e813a85a7312ed809fc7e5a9f7440 (patch)
treee9b99a94e9b5937c70d9d768d56e8092686f01aa /Map/Debouncer.swift
parent491463f421c86d3a7a5482108b818aa5b5e441ea (diff)
Release 1.1.01.1.0
Diffstat (limited to 'Map/Debouncer.swift')
-rw-r--r--Map/Debouncer.swift21
1 files changed, 21 insertions, 0 deletions
diff --git a/Map/Debouncer.swift b/Map/Debouncer.swift
new file mode 100644
index 0000000..f119d8a
--- /dev/null
+++ b/Map/Debouncer.swift
@@ -0,0 +1,21 @@
+import Foundation
+
+class Debouncer {
+
+ // MARK: - Properties
+ private let queue = DispatchQueue.main
+ private var workItem = DispatchWorkItem(block: {})
+ private var interval: TimeInterval
+
+ // MARK: - Initializer
+ init(seconds: TimeInterval) {
+ self.interval = seconds
+ }
+
+ // MARK: - Debouncing function
+ func debounce(action: @escaping (() -> Void)) {
+ workItem.cancel()
+ workItem = DispatchWorkItem(block: { action() })
+ queue.asyncAfter(deadline: .now() + interval, execute: workItem)
+ }
+}