From cf4f1451373904c95a0e32f2a333c7956a8d4f05 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Tue, 4 Apr 2023 08:56:37 -0500 Subject: [PATCH] refactored debounce for label/button (has label) Signed-off-by: Matt Bruce --- .../Buttons/Button/ButtonBase.swift | 2 +- VDS/Components/Label/Label.swift | 2 +- VDS/Protocols/Handlerable.swift | 19 +++++++++++++------ 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/VDS/Components/Buttons/Button/ButtonBase.swift b/VDS/Components/Buttons/Button/ButtonBase.swift index 41633216..cafd5cd8 100644 --- a/VDS/Components/Buttons/Button/ButtonBase.swift +++ b/VDS/Components/Buttons/Button/ButtonBase.swift @@ -121,7 +121,7 @@ open class ButtonBase: UIButton, Buttonable, Handlerable, ViewProtocol, Resettab translatesAutoresizingMaskIntoConstraints = false accessibilityCustomActions = [] setup() - setupDidChangeEvent() + setupDidChangeEvent(true) updateView() } } diff --git a/VDS/Components/Label/Label.swift b/VDS/Components/Label/Label.swift index 5f347634..db15ce0d 100644 --- a/VDS/Components/Label/Label.swift +++ b/VDS/Components/Label/Label.swift @@ -94,7 +94,7 @@ public class Label: UILabel, Handlerable, ViewProtocol, Resettable, UserInfoable accessibilityCustomActions = [] accessibilityTraits = .staticText setup() - setupDidChangeEvent() + setupDidChangeEvent(true) updateView() } } diff --git a/VDS/Protocols/Handlerable.swift b/VDS/Protocols/Handlerable.swift index 41ef01b5..cefb150f 100644 --- a/VDS/Protocols/Handlerable.swift +++ b/VDS/Protocols/Handlerable.swift @@ -17,15 +17,22 @@ public protocol Handlerable: AnyObject, Initable, Disabling, Surfaceable { extension Handlerable { - public func setupDidChangeEvent() { - handlerPublisher().sink { [weak self] _ in - self?.updateView() + public func setupDidChangeEvent(_ debounce: Bool = false) { + handlerPublisher(debounce) + .sink { [weak self] _ in + self?.updateView() }.store(in: &subscribers) } - public func handlerPublisher() -> AnyPublisher { - subject - .eraseToAnyPublisher() + public func handlerPublisher(_ debounce: Bool = false) -> AnyPublisher { + if debounce { + return subject + .debounce(for: .seconds(Constants.StateDebounce), scheduler: RunLoop.main) + .eraseToAnyPublisher() + } else { + return subject + .eraseToAnyPublisher() + } } }