Skip to content

Commit

Permalink
Add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Tunous committed Jul 13, 2024
1 parent 3d5d000 commit 29a60e0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

A SwiftUI onChange and task view modifiers with additional debounce time.

[Documentation](https://swiftpackageindex.com/Tunous/DebouncedOnChange/main/documentation/debouncedonchange)

## Usage

### Basics

```swift
import SwiftUI
import DebouncedOnChange
Expand All @@ -23,7 +27,32 @@ struct ExampleView: View {
}
}
}
```
```

### Manually cancelling debounced actions

```swift
struct Sample: View {
@State private var debouncer = Debouncer() // 1. Store debouncer to control actions
@State private var query = ""

var body: some View {
TextField("Query", text: $query)
.onChange(of: query, debounceTime: .seconds(1), debouncer: $debouncer) { // 2. Pass debouncer to onChange
callApi()
}
.onKeyPress(.return) {
debouncer.cancel() // 3. Call cancel to prevent debounced action from running
callApi()
return .handled
}
}

private func callApi() {
print("Sending query \(query)")
}
}
```

## Installation

Expand Down
2 changes: 1 addition & 1 deletion Sources/DebouncedOnChange/Debouncer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import Foundation
/// ```
public struct Debouncer {
var task: Task<Void, Never>?

/// Creates a new debouncer.
public init() {}

Expand Down
20 changes: 20 additions & 0 deletions Sources/DebouncedOnChange/Documentation.docc/Documentation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# ``DebouncedOnChange``

A SwiftUI onChange and task view modifiers with additional debounce time.

## Topics

### View extensions

- ``SwiftUICore/View/onChange(of:initial:debounceTime:debouncer:_:)-9kwdz``
- ``SwiftUICore/View/onChange(of:initial:debounceTime:debouncer:_:)-113nr``
- ``SwiftUICore/View/task(id:priority:debounceTime:_:)``

### Cancelling actions

- ``Debouncer``

### Depreciated View extensions

- ``SwiftUICore/View/onChange(of:debounceTime:debouncer:perform:)-89k8t``
- ``SwiftUICore/View/onChange(of:debounceTime:debouncer:perform:)-2p8uc``

0 comments on commit 29a60e0

Please sign in to comment.