Skip to content

Commit

Permalink
Make throttle underscored (#296)
Browse files Browse the repository at this point in the history
# 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.
  • Loading branch information
FranzBusch authored Sep 27, 2023
1 parent 8cfdf03 commit cb41700
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
24 changes: 12 additions & 12 deletions Sources/AsyncAlgorithms/AsyncThrottleSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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 { }
32 changes: 16 additions & 16 deletions Tests/AsyncAlgorithmsTests/TestThrottle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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|"
}
}
Expand All @@ -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|"
}
}
Expand All @@ -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|"
}
}
Expand All @@ -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|"
}
}
Expand All @@ -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|"
}
}
Expand All @@ -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|"
}
}
Expand All @@ -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|]"
}
}
Expand All @@ -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|]"
}
}
Expand All @@ -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-^"
}
}
Expand All @@ -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-^"
}
}
Expand All @@ -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-|"
}
}
Expand All @@ -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-|"
}
}
Expand All @@ -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|"
}
}
Expand All @@ -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-|"
}
}
Expand All @@ -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|]"
}
}
Expand All @@ -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|]"
}
}
Expand Down

0 comments on commit cb41700

Please sign in to comment.