-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add debouncer with function to manually cancel debounced tasks (#4)
- Loading branch information
Showing
2 changed files
with
80 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// | ||
// Debouncer.swift | ||
// | ||
// | ||
// Created by Łukasz Rutkowski on 13/07/2024. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Object which allows manual control of debounced operations. | ||
/// | ||
/// You can create an instance of this object and pass it to one of debounced `onChange` functions to be able to | ||
/// manually cancel their execution. | ||
/// | ||
/// - Note: A single debouncer should be passed to only one onChange function. If you want to control multiple | ||
/// operations create a separate debouncer for each of them. | ||
/// | ||
/// # Example | ||
/// | ||
/// Here a debouncer is used to cancel debounced api call and instead trigger it immediately when keyboard return | ||
/// key is pressed. | ||
/// | ||
/// ```swift | ||
/// struct Sample: View { | ||
/// @State private var debouncer = Debouncer() | ||
/// @State private var query = "" | ||
/// | ||
/// var body: some View { | ||
/// TextField("Query", text: $query) | ||
/// .onKeyPress(.return) { | ||
/// debouncer.cancel() | ||
/// callApi() | ||
/// return .handled | ||
/// } | ||
/// .onChange(of: query, debounceTime: .seconds(1), debouncer: $debouncer) { | ||
/// callApi() | ||
/// } | ||
/// } | ||
/// | ||
/// private func callApi() { | ||
/// print("Sending query \(query)") | ||
/// } | ||
/// } | ||
/// ``` | ||
public struct Debouncer { | ||
var task: Task<Void, Never>? | ||
|
||
/// Creates a new debouncer. | ||
public init() {} | ||
|
||
/// Cancels an operation that is currently being debounced. | ||
public func cancel() { | ||
task?.cancel() | ||
} | ||
} |