commit 89e01d823af0babc37a2b2d01ac72aec8d4355d0 parent 6edb3b1a401d95cf36ebfb59e2dcc223d2961a4e Author: William Casarin <jb55@jb55.com> Date: Tue, 16 Jan 2024 16:03:10 -0800 debouncer: add new debounce methods Diffstat:
M | damus/Util/Debouncer.swift | | | 26 | ++++++++++++++++++++++++++ |
1 file changed, 26 insertions(+), 0 deletions(-)
diff --git a/damus/Util/Debouncer.swift b/damus/Util/Debouncer.swift @@ -24,4 +24,30 @@ class Debouncer { workItem = DispatchWorkItem { action() } queue.asyncAfter(deadline: .now() + interval, execute: workItem!) } + + func debounce_immediate(action: @escaping () -> Void) { + guard self.workItem == nil else { + return + } + + self.workItem = DispatchWorkItem(block: { + self.workItem = nil + }) + + action() + queue.asyncAfter(deadline: .now() + interval, execute: self.workItem!) + } + + func debounce_once(action: @escaping () -> Void) { + guard self.workItem == nil else { + return + } + + self.workItem = DispatchWorkItem(block: { + self.workItem = nil + action() + }) + + queue.asyncAfter(deadline: .now() + interval, execute: self.workItem!) + } }