aboutsummaryrefslogtreecommitdiff
path: root/Map/Logic/Debouncer.swift
diff options
context:
space:
mode:
authorRuben Beltran del Rio <ruben@unlimited.pizza>2023-05-07 15:22:54 +0200
committerRuben Beltran del Rio <ruben@unlimited.pizza>2023-05-07 15:22:54 +0200
commitfdb4633d3e9158e457d57e820df6e1efb4df39c2 (patch)
tree176cad99cb9befc0a65a7af02561019e811814de /Map/Logic/Debouncer.swift
parent75a0e4509a70055851b085f3f7293ae1cf48164c (diff)
Update to support notes + new style2.0.0
Diffstat (limited to 'Map/Logic/Debouncer.swift')
-rw-r--r--Map/Logic/Debouncer.swift21
1 files changed, 21 insertions, 0 deletions
diff --git a/Map/Logic/Debouncer.swift b/Map/Logic/Debouncer.swift
new file mode 100644
index 0000000..cd7960a
--- /dev/null
+++ b/Map/Logic/Debouncer.swift
@@ -0,0 +1,21 @@
+import Foundation
+
+class Debouncer {
+
+ // MARK: - Properties
+ private let queue = DispatchQueue.global(qos: .utility)
+ 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)
+ }
+}