Skip to content

Commit 3ab80c0

Browse files
committed
Make throttle underscored
# Motivation During the discussion in #248 it became clear that the semantics of `throttle` are not 100% figured out yet. # Modification This PR is making `throttle` an underscored API for the upcoming 1.0.0 release. This gives us more time to investigate what exact semantics we want to have.
1 parent 6360ca0 commit 3ab80c0

File tree

2 files changed

+28
-28
lines changed

2 files changed

+28
-28
lines changed

Sources/AsyncAlgorithms/AsyncThrottleSequence.swift

+12-12
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@
1212
extension AsyncSequence {
1313
/// Create a rate-limited `AsyncSequence` by emitting values at most every specified interval.
1414
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
15-
public func throttle<C: Clock, Reduced>(for interval: C.Instant.Duration, clock: C, reducing: @Sendable @escaping (Reduced?, Element) async -> Reduced) -> AsyncThrottleSequence<Self, C, Reduced> {
16-
AsyncThrottleSequence(self, interval: interval, clock: clock, reducing: reducing)
15+
public func _throttle<C: Clock, Reduced>(for interval: C.Instant.Duration, clock: C, reducing: @Sendable @escaping (Reduced?, Element) async -> Reduced) -> _AsyncThrottleSequence<Self, C, Reduced> {
16+
_AsyncThrottleSequence(self, interval: interval, clock: clock, reducing: reducing)
1717
}
1818

1919
/// Create a rate-limited `AsyncSequence` by emitting values at most every specified interval.
2020
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
21-
public func throttle<Reduced>(for interval: Duration, reducing: @Sendable @escaping (Reduced?, Element) async -> Reduced) -> AsyncThrottleSequence<Self, ContinuousClock, Reduced> {
22-
throttle(for: interval, clock: .continuous, reducing: reducing)
21+
public func _throttle<Reduced>(for interval: Duration, reducing: @Sendable @escaping (Reduced?, Element) async -> Reduced) -> _AsyncThrottleSequence<Self, ContinuousClock, Reduced> {
22+
_throttle(for: interval, clock: .continuous, reducing: reducing)
2323
}
2424

2525
/// Create a rate-limited `AsyncSequence` by emitting values at most every specified interval.
2626
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
27-
public func throttle<C: Clock>(for interval: C.Instant.Duration, clock: C, latest: Bool = true) -> AsyncThrottleSequence<Self, C, Element> {
28-
throttle(for: interval, clock: clock) { previous, element in
27+
public func _throttle<C: Clock>(for interval: C.Instant.Duration, clock: C, latest: Bool = true) -> _AsyncThrottleSequence<Self, C, Element> {
28+
_throttle(for: interval, clock: clock) { previous, element in
2929
if latest {
3030
return element
3131
} else {
@@ -36,14 +36,14 @@ extension AsyncSequence {
3636

3737
/// Create a rate-limited `AsyncSequence` by emitting values at most every specified interval.
3838
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
39-
public func throttle(for interval: Duration, latest: Bool = true) -> AsyncThrottleSequence<Self, ContinuousClock, Element> {
40-
throttle(for: interval, clock: .continuous, latest: latest)
39+
public func _throttle(for interval: Duration, latest: Bool = true) -> _AsyncThrottleSequence<Self, ContinuousClock, Element> {
40+
_throttle(for: interval, clock: .continuous, latest: latest)
4141
}
4242
}
4343

4444
/// A rate-limited `AsyncSequence` by emitting values at most every specified interval.
4545
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
46-
public struct AsyncThrottleSequence<Base: AsyncSequence, C: Clock, Reduced> {
46+
public struct _AsyncThrottleSequence<Base: AsyncSequence, C: Clock, Reduced> {
4747
let base: Base
4848
let interval: C.Instant.Duration
4949
let clock: C
@@ -58,7 +58,7 @@ public struct AsyncThrottleSequence<Base: AsyncSequence, C: Clock, Reduced> {
5858
}
5959

6060
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
61-
extension AsyncThrottleSequence: AsyncSequence {
61+
extension _AsyncThrottleSequence: AsyncSequence {
6262
public typealias Element = Reduced
6363

6464
/// The iterator for an `AsyncThrottleSequence` instance.
@@ -110,7 +110,7 @@ extension AsyncThrottleSequence: AsyncSequence {
110110
}
111111

112112
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
113-
extension AsyncThrottleSequence: Sendable where Base: Sendable, Element: Sendable { }
113+
extension _AsyncThrottleSequence: Sendable where Base: Sendable, Element: Sendable { }
114114

115115
@available(*, unavailable)
116-
extension AsyncThrottleSequence.Iterator: Sendable { }
116+
extension _AsyncThrottleSequence.Iterator: Sendable { }

Tests/AsyncAlgorithmsTests/TestThrottle.swift

+16-16
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ final class TestThrottle: XCTestCase {
1717
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
1818
validate {
1919
"abcdefghijk|"
20-
$0.inputs[0].throttle(for: .steps(0), clock: $0.clock)
20+
$0.inputs[0]._throttle(for: .steps(0), clock: $0.clock)
2121
"abcdefghijk|"
2222
}
2323
}
@@ -26,7 +26,7 @@ final class TestThrottle: XCTestCase {
2626
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
2727
validate {
2828
"abcdefghijk|"
29-
$0.inputs[0].throttle(for: .steps(0), clock: $0.clock, latest: false)
29+
$0.inputs[0]._throttle(for: .steps(0), clock: $0.clock, latest: false)
3030
"abcdefghijk|"
3131
}
3232
}
@@ -35,7 +35,7 @@ final class TestThrottle: XCTestCase {
3535
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
3636
validate {
3737
"abcdefghijk|"
38-
$0.inputs[0].throttle(for: .steps(1), clock: $0.clock)
38+
$0.inputs[0]._throttle(for: .steps(1), clock: $0.clock)
3939
"abcdefghijk|"
4040
}
4141
}
@@ -44,7 +44,7 @@ final class TestThrottle: XCTestCase {
4444
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
4545
validate {
4646
"abcdefghijk|"
47-
$0.inputs[0].throttle(for: .steps(1), clock: $0.clock, latest: false)
47+
$0.inputs[0]._throttle(for: .steps(1), clock: $0.clock, latest: false)
4848
"abcdefghijk|"
4949
}
5050
}
@@ -53,7 +53,7 @@ final class TestThrottle: XCTestCase {
5353
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
5454
validate {
5555
"abcdefghijk|"
56-
$0.inputs[0].throttle(for: .steps(2), clock: $0.clock)
56+
$0.inputs[0]._throttle(for: .steps(2), clock: $0.clock)
5757
"a-c-e-g-i-k|"
5858
}
5959
}
@@ -62,7 +62,7 @@ final class TestThrottle: XCTestCase {
6262
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
6363
validate {
6464
"abcdefghijk|"
65-
$0.inputs[0].throttle(for: .steps(2), clock: $0.clock, latest: false)
65+
$0.inputs[0]._throttle(for: .steps(2), clock: $0.clock, latest: false)
6666
"a-b-d-f-h-j|"
6767
}
6868
}
@@ -71,7 +71,7 @@ final class TestThrottle: XCTestCase {
7171
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
7272
validate {
7373
"abcdefghijk|"
74-
$0.inputs[0].throttle(for: .steps(3), clock: $0.clock)
74+
$0.inputs[0]._throttle(for: .steps(3), clock: $0.clock)
7575
"a--d--g--j--[k|]"
7676
}
7777
}
@@ -80,7 +80,7 @@ final class TestThrottle: XCTestCase {
8080
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
8181
validate {
8282
"abcdefghijk|"
83-
$0.inputs[0].throttle(for: .steps(3), clock: $0.clock, latest: false)
83+
$0.inputs[0]._throttle(for: .steps(3), clock: $0.clock, latest: false)
8484
"a--b--e--h--[k|]"
8585
}
8686
}
@@ -89,7 +89,7 @@ final class TestThrottle: XCTestCase {
8989
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
9090
validate {
9191
"abcdef^hijk|"
92-
$0.inputs[0].throttle(for: .steps(2), clock: $0.clock)
92+
$0.inputs[0]._throttle(for: .steps(2), clock: $0.clock)
9393
"a-c-e-^"
9494
}
9595
}
@@ -98,7 +98,7 @@ final class TestThrottle: XCTestCase {
9898
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
9999
validate {
100100
"abcdef^hijk|"
101-
$0.inputs[0].throttle(for: .steps(2), clock: $0.clock, latest: false)
101+
$0.inputs[0]._throttle(for: .steps(2), clock: $0.clock, latest: false)
102102
"a-b-d-^"
103103
}
104104
}
@@ -107,7 +107,7 @@ final class TestThrottle: XCTestCase {
107107
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
108108
validate {
109109
"-a-b-c-d-e-f-g-h-i-j-k-|"
110-
$0.inputs[0].throttle(for: .steps(1), clock: $0.clock)
110+
$0.inputs[0]._throttle(for: .steps(1), clock: $0.clock)
111111
"-a-b-c-d-e-f-g-h-i-j-k-|"
112112
}
113113
}
@@ -116,7 +116,7 @@ final class TestThrottle: XCTestCase {
116116
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
117117
validate {
118118
"-a-b-c-d-e-f-g-h-i-j-k-|"
119-
$0.inputs[0].throttle(for: .steps(2), clock: $0.clock)
119+
$0.inputs[0]._throttle(for: .steps(2), clock: $0.clock)
120120
"-a-b-c-d-e-f-g-h-i-j-k-|"
121121
}
122122
}
@@ -125,7 +125,7 @@ final class TestThrottle: XCTestCase {
125125
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
126126
validate {
127127
"--a--b--c--d--e--f--g|"
128-
$0.inputs[0].throttle(for: .steps(2), clock: $0.clock)
128+
$0.inputs[0]._throttle(for: .steps(2), clock: $0.clock)
129129
"--a--b--c--d--e--f--g|"
130130
}
131131
}
@@ -134,7 +134,7 @@ final class TestThrottle: XCTestCase {
134134
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
135135
validate {
136136
"-a-b-c-d-e-f-g-h-i-j-k-|"
137-
$0.inputs[0].throttle(for: .steps(3), clock: $0.clock)
137+
$0.inputs[0]._throttle(for: .steps(3), clock: $0.clock)
138138
"-a---c---e---g---i---k-|"
139139
}
140140
}
@@ -143,7 +143,7 @@ final class TestThrottle: XCTestCase {
143143
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
144144
validate {
145145
"abcdefghijkl|"
146-
$0.inputs[0].throttle(for: .steps(3), clock: $0.clock, latest: false)
146+
$0.inputs[0]._throttle(for: .steps(3), clock: $0.clock, latest: false)
147147
"a--b--e--h--[k|]"
148148
}
149149
}
@@ -152,7 +152,7 @@ final class TestThrottle: XCTestCase {
152152
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
153153
validate {
154154
"abcdefghijkl|"
155-
$0.inputs[0].throttle(for: .steps(3), clock: $0.clock, latest: true)
155+
$0.inputs[0]._throttle(for: .steps(3), clock: $0.clock, latest: true)
156156
"a--d--g--j--[l|]"
157157
}
158158
}

0 commit comments

Comments
 (0)