Skip to content

Commit 858fa4f

Browse files
committed
project: implement Debounce and Throttle + SwiftUI helpers
1 parent 5bf3db8 commit 858fa4f

File tree

19 files changed

+756
-42
lines changed

19 files changed

+756
-42
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
**v0.1.0:**
2+
3+
- Debouncer
4+
- Throttler
5+
- SwiftUI helpers for buttons and bindings

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 AsyncCommunity
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,49 @@
11
# Regulate
22

3-
**Regulate** is a lightweight library that brings the following time-based regulation operations for things that can emit values over times (and are not from reactive programming or `AsyncSequence`).
3+
**Regulate** is a lightweight library that brings the following time-based regulation operations for things that can emit values over times (and are not using reactive programming or `AsyncSequence`).
44

5-
- debounce:
6-
- throttle:
5+
- [Debounce](./Sources/Debouncer.swift) (Outputs elements only after a specified time interval elapses between events)
6+
- [Throttle](./Sources/Throttler.swift) (Outputs either the most-recent or first element pushed by a producer in the specified time interval)
77

88
**Regulate** is entirely backed by Swift concurrency and limits the number of created `Tasks` to the minimum.
99

10+
**Regulate** also provides SwiftUI helpers to regulate buttons and bindings out of the box.
11+
You can give a look at the [Sample app](./Sample).
12+
13+
For a Button, it is as simple as:
14+
15+
```swift
16+
Button {
17+
print("I've been hit (throttled)!")
18+
} label: {
19+
Text("Hit me")
20+
}
21+
.throttle(dueTime: .seconds(1))
22+
```
23+
24+
For a Binding, there is a tiny bit of extra work:
25+
26+
```swift
27+
@State private var text = ""
28+
@StateObject private var debouncer = Debouncer<String>(dueTime: .seconds(1))
29+
...
30+
TextField(
31+
text: self
32+
.$text
33+
.perform(regulator: debouncer) { text in
34+
print("regulated text \(text)") // you can perform any side effect here!
35+
}
36+
) {
37+
Text("prompt")
38+
}
39+
```
40+
41+
## Demo
42+
43+
<kbd>
44+
<img style="border:2px solid black" alt="Demo Application" src="https://raw.githubusercontent.com/sideeffect-io/Regulate/main/Regulate.gif"/>
45+
</kbd>
46+
1047
## Adding Regulate as a Dependency
1148

1249
To use the `Regulate` library in a SwiftPM project,

Regulate.gif

532 KB
Loading

0 commit comments

Comments
 (0)