diff --git a/Sources/AsyncAlgorithms/AsyncThrottleSequence.swift b/Sources/AsyncAlgorithms/AsyncThrottleSequence.swift new file mode 100644 index 0000000..34cbc88 --- /dev/null +++ b/Sources/AsyncAlgorithms/AsyncThrottleSequence.swift @@ -0,0 +1,92 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Async Algorithms open source project +// +// Copyright (c) 2021 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// +//===----------------------------------------------------------------------===// + +extension AsyncSequence { + public func throttle(for interval: C.Instant.Duration, clock: C, reducing: @Sendable @escaping (Reduced?, Element) async -> Reduced) -> AsyncThrottleSequence { + AsyncThrottleSequence(self, interval: interval, clock: clock, reducing: reducing) + } + + /* + public func throttle(for interval: Duration, reducing: @Sendable @escaping (Reduced?, Element) async -> Reduced) -> AsyncThrottleSequence { + throttle(for: interval, clock: .continuous, reducing: reducing) + } + */ + + public func throttle(for interval: C.Instant.Duration, clock: C, latest: Bool = true) -> AsyncThrottleSequence { + throttle(for: interval, clock: clock) { previous, element in + if latest { + return element + } else { + return previous ?? element + } + } + } + + /* + public func throttle(for interval: Duration, latest: Bool = true) -> AsyncThrottleSequence { + throttle(for: interval, clock: .continuous, latest: latest) + } + */ +} + +public struct AsyncThrottleSequence { + let base: Base + let interval: C.Instant.Duration + let clock: C + let reducing: @Sendable (Reduced?, Base.Element) async -> Reduced + + init(_ base: Base, interval: C.Instant.Duration, clock: C, reducing: @Sendable @escaping (Reduced?, Base.Element) async -> Reduced) { + self.base = base + self.interval = interval + self.clock = clock + self.reducing = reducing + } +} + +extension AsyncThrottleSequence: Sendable where Base: Sendable, Element: Sendable { } + +extension AsyncThrottleSequence: AsyncSequence { + public typealias Element = Reduced + + public struct Iterator: AsyncIteratorProtocol { + var base: Base.AsyncIterator + let interval: C.Instant.Duration + let clock: C + let reducing: @Sendable (Reduced?, Base.Element) async -> Reduced + + init(_ base: Base.AsyncIterator, interval: C.Instant.Duration, clock: C, reducing: @Sendable @escaping (Reduced?, Base.Element) async -> Reduced) { + self.base = base + self.interval = interval + self.clock = clock + self.reducing = reducing + } + + public mutating func next() async rethrows -> Reduced? { + var reduced: Reduced? + let start = clock.now + repeat { + guard let element = try await base.next() else { + return nil + } + let reduction = await reducing(reduced, element) + if clock.now - start >= interval { + return reduction + } else { + reduced = reduction + } + } while true + } + } + + public func makeAsyncIterator() -> Iterator { + Iterator(base.makeAsyncIterator(), interval: interval, clock: clock, reducing: reducing) + } +} diff --git a/Tests/AsyncAlgorithmsTests/TestThrottle.swift b/Tests/AsyncAlgorithmsTests/TestThrottle.swift new file mode 100644 index 0000000..dad8467 --- /dev/null +++ b/Tests/AsyncAlgorithmsTests/TestThrottle.swift @@ -0,0 +1,127 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Async Algorithms open source project +// +// Copyright (c) 2022 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// +//===----------------------------------------------------------------------===// + +import XCTest +import AsyncAlgorithms + +final class TestThrottle: XCTestCase { + func test_rate_0() { + marbleDiagram { + "abcdefghijk|" + $0.inputs[0].throttle(for: .steps(0), clock: $0.clock) + "abcdefghijk|" + } + } + + func test_rate_0_leading_edge() { + marbleDiagram { + "abcdefghijk|" + $0.inputs[0].throttle(for: .steps(0), clock: $0.clock, latest: false) + "abcdefghijk|" + } + } + + func test_rate_1() { + marbleDiagram { + "abcdefghijk|" + $0.inputs[0].throttle(for: .steps(1), clock: $0.clock) + "abcdefghijk|" + } + } + + func test_rate_1_leading_edge() { + marbleDiagram { + "abcdefghijk|" + $0.inputs[0].throttle(for: .steps(1), clock: $0.clock, latest: false) + "abcdefghijk|" + } + } + + func test_rate_2() { + marbleDiagram { + "abcdefghijk|" + $0.inputs[0].throttle(for: .steps(2), clock: $0.clock) + "-b-d-f-h-j-|" + } + } + + func test_rate_2_leading_edge() { + marbleDiagram { + "abcdefghijk|" + $0.inputs[0].throttle(for: .steps(2), clock: $0.clock, latest: false) + "-a-c-e-g-i-|" + } + } + + func test_rate_3() { + marbleDiagram { + "abcdefghijk|" + $0.inputs[0].throttle(for: .steps(3), clock: $0.clock) + "--c--f--i--|" + } + } + + func test_rate_3_leading_edge() { + marbleDiagram { + "abcdefghijk|" + $0.inputs[0].throttle(for: .steps(3), clock: $0.clock, latest: false) + "--a--d--g--|" + } + } + + func test_throwing() { + marbleDiagram { + "abcdef^hijk|" + $0.inputs[0].throttle(for: .steps(2), clock: $0.clock) + "-b-d-f^" + } + } + + func test_throwing_leading_edge() { + marbleDiagram { + "abcdef^hijk|" + $0.inputs[0].throttle(for: .steps(2), clock: $0.clock, latest: false) + "-a-c-e^" + } + } + + func test_emission_2_rate_1() { + marbleDiagram { + "-a-b-c-d-e-f-g-h-i-j-k-|" + $0.inputs[0].throttle(for: .steps(1), clock: $0.clock) + "-a-b-c-d-e-f-g-h-i-j-k-|" + } + } + + func test_emission_2_rate_2() { + marbleDiagram { + "-a-b-c-d-e-f-g-h-i-j-k-|" + $0.inputs[0].throttle(for: .steps(2), clock: $0.clock) + "-a-b-c-d-e-f-g-h-i-j-k-|" + } + } + + func test_emission_3_rate_2() { + marbleDiagram { + "--a--b--c--d--e--f--g|" + $0.inputs[0].throttle(for: .steps(2), clock: $0.clock) + "--a--b--c--d--e--f--g|" + } + } + + func test_emission_2_rate_3() { + marbleDiagram { + "-a-b-c-d-e-f-g-h-i-j-k-|" + $0.inputs[0].throttle(for: .steps(3), clock: $0.clock) + "---b---d---f---h---j---|" + } + } +}