From 909148f0bebaee11f59f63bab43791a7ccd48555 Mon Sep 17 00:00:00 2001 From: William Casarin Date: Wed, 15 Feb 2023 19:15:19 -0800 Subject: [PATCH] add missing file --- damus/Util/Debouncer.swift | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 damus/Util/Debouncer.swift diff --git a/damus/Util/Debouncer.swift b/damus/Util/Debouncer.swift new file mode 100644 index 00000000..140cdd71 --- /dev/null +++ b/damus/Util/Debouncer.swift @@ -0,0 +1,27 @@ +// +// Debouncer.swift +// damus +// +// Created by William Casarin on 2023-02-15. +// + +import Foundation + +class Debouncer { + private let queue = DispatchQueue.main + private var workItem: DispatchWorkItem? + private var interval: TimeInterval + + init(interval: TimeInterval) { + self.interval = interval + } + + func debounce(action: @escaping () -> Void) { + // Cancel the previous work item if it hasn't yet executed + workItem?.cancel() + + // Create a new work item with a delay + workItem = DispatchWorkItem { action() } + queue.asyncAfter(deadline: .now() + interval, execute: workItem!) + } +}