Make throttle underscored (#296)

# Motivation
During the discussion in https://github.com/apple/swift-async-algorithms/issues/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.
This commit is contained in:
Franz Busch authored and GitHub committed 2023-09-27 08:35:58 -07:00
1 parent 8cfdf03b51
commit cb417003f9
2 files changed
+28 -28

No files matched your search

@@ -12,20 +12,20 @@
extension AsyncSequence {
/// Create a rate-limited `AsyncSequence` by emitting values at most every specified interval.
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
public func throttle<C: Clock, Reduced>(for interval: C.Instant.Duration, clock: C, reducing: @Sendable @escaping (Reduced?, Element) async -> Reduced) -> AsyncThrottleSequence<Self, C, Reduced> {
AsyncThrottleSequence(self, interval: interval, clock: clock, reducing: reducing)
public func _throttle<C: Clock, Reduced>(for interval: C.Instant.Duration, clock: C, reducing: @Sendable @escaping (Reduced?, Element) async -> Reduced) -> _AsyncThrottleSequence<Self, C, Reduced> {
_AsyncThrottleSequence(self, interval: interval, clock: clock, reducing: reducing)
}
/// Create a rate-limited `AsyncSequence` by emitting values at most every specified interval.
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
public func throttle<Reduced>(for interval: Duration, reducing: @Sendable @escaping (Reduced?, Element) async -> Reduced) -> AsyncThrottleSequence<Self, ContinuousClock, Reduced> {
throttle(for: interval, clock: .continuous, reducing: reducing)
public func _throttle<Reduced>(for interval: Duration, reducing: @Sendable @escaping (Reduced?, Element) async -> Reduced) -> _AsyncThrottleSequence<Self, ContinuousClock, Reduced> {
_throttle(for: interval, clock: .continuous, reducing: reducing)
}
/// Create a rate-limited `AsyncSequence` by emitting values at most every specified interval.
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
public func throttle<C: Clock>(for interval: C.Instant.Duration, clock: C, latest: Bool = true) -> AsyncThrottleSequence<Self, C, Element> {
throttle(for: interval, clock: clock) { previous, element in
public func _throttle<C: Clock>(for interval: C.Instant.Duration, clock: C, latest: Bool = true) -> _AsyncThrottleSequence<Self, C, Element> {
_throttle(for: interval, clock: clock) { previous, element in
if latest {
return element
} else {
@@ -36,14 +36,14 @@ extension AsyncSequence {
/// Create a rate-limited `AsyncSequence` by emitting values at most every specified interval.
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
public func throttle(for interval: Duration, latest: Bool = true) -> AsyncThrottleSequence<Self, ContinuousClock, Element> {
throttle(for: interval, clock: .continuous, latest: latest)
public func _throttle(for interval: Duration, latest: Bool = true) -> _AsyncThrottleSequence<Self, ContinuousClock, Element> {
_throttle(for: interval, clock: .continuous, latest: latest)
}
}
/// A rate-limited `AsyncSequence` by emitting values at most every specified interval.
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
public struct AsyncThrottleSequence<Base: AsyncSequence, C: Clock, Reduced> {
public struct _AsyncThrottleSequence<Base: AsyncSequence, C: Clock, Reduced> {
let base: Base
let interval: C.Instant.Duration
let clock: C
@@ -58,7 +58,7 @@ public struct AsyncThrottleSequence<Base: AsyncSequence, C: Clock, Reduced> {
}
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
extension AsyncThrottleSequence: AsyncSequence {
extension _AsyncThrottleSequence: AsyncSequence {
public typealias Element = Reduced
/// The iterator for an `AsyncThrottleSequence` instance.
@@ -110,7 +110,7 @@ extension AsyncThrottleSequence: AsyncSequence {
}
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
extension AsyncThrottleSequence: Sendable where Base: Sendable, Element: Sendable { }
extension _AsyncThrottleSequence: Sendable where Base: Sendable, Element: Sendable { }
@available(*, unavailable)
extension AsyncThrottleSequence.Iterator: Sendable { }
extension _AsyncThrottleSequence.Iterator: Sendable { }
+16 -16
View File
@@ -17,7 +17,7 @@ final class TestThrottle: XCTestCase {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"abcdefghijk|"
$0.inputs[0].throttle(for: .steps(0), clock: $0.clock)
$0.inputs[0]._throttle(for: .steps(0), clock: $0.clock)
"abcdefghijk|"
}
}
@@ -26,7 +26,7 @@ final class TestThrottle: XCTestCase {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"abcdefghijk|"
$0.inputs[0].throttle(for: .steps(0), clock: $0.clock, latest: false)
$0.inputs[0]._throttle(for: .steps(0), clock: $0.clock, latest: false)
"abcdefghijk|"
}
}
@@ -35,7 +35,7 @@ final class TestThrottle: XCTestCase {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"abcdefghijk|"
$0.inputs[0].throttle(for: .steps(1), clock: $0.clock)
$0.inputs[0]._throttle(for: .steps(1), clock: $0.clock)
"abcdefghijk|"
}
}
@@ -44,7 +44,7 @@ final class TestThrottle: XCTestCase {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"abcdefghijk|"
$0.inputs[0].throttle(for: .steps(1), clock: $0.clock, latest: false)
$0.inputs[0]._throttle(for: .steps(1), clock: $0.clock, latest: false)
"abcdefghijk|"
}
}
@@ -53,7 +53,7 @@ final class TestThrottle: XCTestCase {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"abcdefghijk|"
$0.inputs[0].throttle(for: .steps(2), clock: $0.clock)
$0.inputs[0]._throttle(for: .steps(2), clock: $0.clock)
"a-c-e-g-i-k|"
}
}
@@ -62,7 +62,7 @@ final class TestThrottle: XCTestCase {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"abcdefghijk|"
$0.inputs[0].throttle(for: .steps(2), clock: $0.clock, latest: false)
$0.inputs[0]._throttle(for: .steps(2), clock: $0.clock, latest: false)
"a-b-d-f-h-j|"
}
}
@@ -71,7 +71,7 @@ final class TestThrottle: XCTestCase {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"abcdefghijk|"
$0.inputs[0].throttle(for: .steps(3), clock: $0.clock)
$0.inputs[0]._throttle(for: .steps(3), clock: $0.clock)
"a--d--g--j--[k|]"
}
}
@@ -80,7 +80,7 @@ final class TestThrottle: XCTestCase {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"abcdefghijk|"
$0.inputs[0].throttle(for: .steps(3), clock: $0.clock, latest: false)
$0.inputs[0]._throttle(for: .steps(3), clock: $0.clock, latest: false)
"a--b--e--h--[k|]"
}
}
@@ -89,7 +89,7 @@ final class TestThrottle: XCTestCase {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"abcdef^hijk|"
$0.inputs[0].throttle(for: .steps(2), clock: $0.clock)
$0.inputs[0]._throttle(for: .steps(2), clock: $0.clock)
"a-c-e-^"
}
}
@@ -98,7 +98,7 @@ final class TestThrottle: XCTestCase {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"abcdef^hijk|"
$0.inputs[0].throttle(for: .steps(2), clock: $0.clock, latest: false)
$0.inputs[0]._throttle(for: .steps(2), clock: $0.clock, latest: false)
"a-b-d-^"
}
}
@@ -107,7 +107,7 @@ final class TestThrottle: XCTestCase {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"-a-b-c-d-e-f-g-h-i-j-k-|"
$0.inputs[0].throttle(for: .steps(1), clock: $0.clock)
$0.inputs[0]._throttle(for: .steps(1), clock: $0.clock)
"-a-b-c-d-e-f-g-h-i-j-k-|"
}
}
@@ -116,7 +116,7 @@ final class TestThrottle: XCTestCase {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"-a-b-c-d-e-f-g-h-i-j-k-|"
$0.inputs[0].throttle(for: .steps(2), clock: $0.clock)
$0.inputs[0]._throttle(for: .steps(2), clock: $0.clock)
"-a-b-c-d-e-f-g-h-i-j-k-|"
}
}
@@ -125,7 +125,7 @@ final class TestThrottle: XCTestCase {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"--a--b--c--d--e--f--g|"
$0.inputs[0].throttle(for: .steps(2), clock: $0.clock)
$0.inputs[0]._throttle(for: .steps(2), clock: $0.clock)
"--a--b--c--d--e--f--g|"
}
}
@@ -134,7 +134,7 @@ final class TestThrottle: XCTestCase {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"-a-b-c-d-e-f-g-h-i-j-k-|"
$0.inputs[0].throttle(for: .steps(3), clock: $0.clock)
$0.inputs[0]._throttle(for: .steps(3), clock: $0.clock)
"-a---c---e---g---i---k-|"
}
}
@@ -143,7 +143,7 @@ final class TestThrottle: XCTestCase {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"abcdefghijkl|"
$0.inputs[0].throttle(for: .steps(3), clock: $0.clock, latest: false)
$0.inputs[0]._throttle(for: .steps(3), clock: $0.clock, latest: false)
"a--b--e--h--[k|]"
}
}
@@ -152,7 +152,7 @@ final class TestThrottle: XCTestCase {
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
validate {
"abcdefghijkl|"
$0.inputs[0].throttle(for: .steps(3), clock: $0.clock, latest: true)
$0.inputs[0]._throttle(for: .steps(3), clock: $0.clock, latest: true)
"a--d--g--j--[l|]"
}
}