Skip to content

Commit 3dbaf34

Browse files
authoredOct 13, 2022
Merge pull request #1 from sideeffect-io/feature/task-extensions
project: add task extensions
2 parents 8ae6142 + baba499 commit 3dbaf34

7 files changed

+52
-7
lines changed
 

‎CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
**v0.2.0:**
2+
3+
- add static factory methods on `Task`
4+
15
**v0.1.0:**
26

37
- Debouncer

‎README.md

+11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@
77

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

10+
```swift
11+
let regulator = Task.debounce(dueTime: .milliseconds(200)) { (value: Int) in
12+
print(value)
13+
}
14+
15+
// the created `regulator` can be used across `Tasks` and each call to `regulator.push(x)`
16+
// will feed the regulation system
17+
18+
// the execution of the provided closure will be debounced and executed 200ms after the last call to `push(x)`
19+
```
20+
1021
**Regulate** also provides SwiftUI helpers to regulate buttons and bindings out of the box.
1122
You can give a look at the [Sample app](./Sample).
1223

‎Sources/Debouncer.swift

+15-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@
77

88
import Foundation
99

10+
public extension Task where Failure == Never {
11+
/// Creates a `Regulator` that executes an output only after a specified time interval elapses between events
12+
/// - Parameters:
13+
/// - dueTime: the time the Debouncer should wait before executing the output
14+
/// - output: the block to execute once the regulation is done
15+
/// - Returns: the debounced regulator
16+
static func debounce(
17+
dueTime: DispatchTimeInterval,
18+
output: @Sendable @escaping (Success) async -> Void
19+
) -> some Regulator<Success> {
20+
Debouncer(dueTime: dueTime, output: output)
21+
}
22+
}
23+
1024
/// Executes an output only after a specified time interval elapses between events
1125
///
1226
/// ```swift
@@ -79,7 +93,7 @@ public final class Debouncer<Value>: @unchecked Sendable, ObservableObject, Regu
7993
/// A Regulator that executes the output only after a specified time interval elapses between events
8094
/// - Parameters:
8195
/// - dueTime: the time the Debouncer should wait before executing the output
82-
/// - output: the block to execute once the regulationis done
96+
/// - output: the block to execute once the regulation is done
8397
public init(
8498
dueTime: DispatchTimeInterval,
8599
output: (@Sendable (Value) async -> Void)? = nil

‎Sources/SwiftUI/RegulatedButtonStyle.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public struct RegulatedButtonStyle<R: Regulator<Void>>: PrimitiveButtonStyle {
2020
regulator.dueTime = self.dueTime
2121
regulator.output = { _ in configuration.trigger() }
2222

23-
if #available(iOS 15.0, *) {
23+
if #available(iOS 15.0, macOS 12.0, *) {
2424
return Button(role: configuration.role) {
2525
regulator.push(())
2626
} label: {

‎Sources/Throttler.swift

+17-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,22 @@
77

88
import Foundation
99

10+
public extension Task where Failure == Never {
11+
/// Creates a `Regulator` that executes the output with either the most-recent or first element
12+
/// pushed in the Throttler in the specified time interval
13+
/// - dueTime: the interval at which to find and emit either the most recent or the first element
14+
/// - latest: true if output should be called with the most-recent element, false otherwise
15+
/// - output: the block to execute once the regulation is done
16+
/// - Returns: the throttled regulator
17+
static func throttle(
18+
dueTime: DispatchTimeInterval,
19+
latest: Bool = true,
20+
output: @Sendable @escaping (Success) async -> Void
21+
) -> some Regulator<Success> {
22+
Throttler(dueTime: dueTime, latest: latest, output: output)
23+
}
24+
}
25+
1026
/// Executes the output with either the most-recent or first element pushed in the Throttler in the specified time interval
1127
///
1228
/// ```swift
@@ -84,7 +100,7 @@ public final class Throttler<Value>: @unchecked Sendable, ObservableObject, Regu
84100
/// - Parameters:
85101
/// - dueTime: the interval at which to find and emit either the most recent or the first element
86102
/// - latest: true if output should be called with the most-recent element, false otherwise
87-
/// - output: the block to execute once the regulationis done
103+
/// - output: the block to execute once the regulation is done
88104
public init(
89105
dueTime: DispatchTimeInterval,
90106
latest: Bool = true,

‎Tests/DebouncerTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ final class DebouncerTests: XCTestCase {
1313
let hasDebounced = expectation(description: "Has debounced a value")
1414
let spy = Spy<Int>()
1515

16-
let sut = Debouncer<Int>(dueTime: .milliseconds(200)) { value in
16+
let sut = Task.debounce(dueTime: .milliseconds(200)) { value in
1717
await spy.push(value)
1818
hasDebounced.fulfill()
1919
}

‎Tests/ThrottlerTests.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ final class ThrottlerTests: XCTestCase {
1515

1616
let spy = Spy<Int>()
1717

18-
let sut = Throttler<Int>(dueTime: .milliseconds(100), latest: false) { value in
18+
let sut = Task.throttle(dueTime: .milliseconds(100), latest: false) { value in
1919
await spy.push(value)
2020
hasThrottledTwoValues.fulfill()
2121
}
@@ -39,7 +39,7 @@ final class ThrottlerTests: XCTestCase {
3939

4040
let spy = Spy<Int>()
4141

42-
let sut = Throttler<Int>(dueTime: .milliseconds(100), latest: true) { value in
42+
let sut = Task.throttle(dueTime: .milliseconds(100), latest: true) { value in
4343
await spy.push(value)
4444
hasThrottledTwoValues.fulfill()
4545
}
@@ -63,7 +63,7 @@ final class ThrottlerTests: XCTestCase {
6363

6464
let spy = Spy<Int>()
6565

66-
let sut = Throttler<Int>(dueTime: .milliseconds(100), latest: true) { value in
66+
let sut = Task.throttle(dueTime: .milliseconds(100), latest: true) { value in
6767
await spy.push(value)
6868
hasThrottledTwoValues.fulfill()
6969
}

0 commit comments

Comments
 (0)