mirror of
https://github.com/apple/swift-async-algorithms
synced 2026-11-23 04:14:23 +00:00
Compare commits
10
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cb417003f9 | ||
|
|
8cfdf03b51 | ||
|
|
6360ca0344 | ||
|
|
0a3866daec | ||
|
|
260e198221 | ||
|
|
f56556930f | ||
|
|
220f86f792 | ||
|
|
c889832c64 | ||
|
|
6dfbfd5e49 | ||
|
|
281e27c5d0 |
No files matched your search
@@ -0,0 +1,5 @@
|
||||
version: 1
|
||||
builder:
|
||||
configs:
|
||||
- documentation_targets: [AsyncAlgorithms]
|
||||
swift_version: 5.8
|
||||
@@ -0,0 +1,86 @@
|
||||
# Channel
|
||||
|
||||
* Author(s): [Philippe Hausler](https://github.com/phausler)
|
||||
|
||||
[
|
||||
[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncChannel.swift),
|
||||
[Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncThrowingChannel.swift) |
|
||||
[Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestChannel.swift)
|
||||
]
|
||||
|
||||
## Introduction
|
||||
|
||||
`AsyncStream` introduced a mechanism to send buffered elements from a context that doesn't use Swift concurrency into one that does. That design only addressed a portion of the potential use cases; the missing portion was the back pressure excerpted across two concurrency domains.
|
||||
|
||||
## Proposed Solution
|
||||
|
||||
To achieve a system that supports back pressure and allows for the communication of more than one value from one task to another we are introducing a new type, the _channel_. The channel will be a reference-type asynchronous sequence with an asynchronous sending capability that awaits the consumption of iteration. Each value sent by the channel will await the consumption of that value by iteration. That awaiting behavior will allow for the affordance of back pressure applied from the consumption site to be transmitted to the production site. This means that the rate of production cannot exceed the rate of consumption, and that the rate of consumption cannot exceed the rate of production. Sending a terminal event to the channel will instantly resume all pending operations for every producers and consumers.
|
||||
|
||||
## Detailed Design
|
||||
|
||||
Similar to the `AsyncStream` and `AsyncThrowingStream` types, the type for sending elements via back pressure will come in two versions. These two versions will account for the throwing nature or non-throwing nature of the elements being produced.
|
||||
|
||||
Each type will have functions to send elements and to send terminal events.
|
||||
|
||||
```swift
|
||||
public final class AsyncChannel<Element: Sendable>: AsyncSequence, Sendable {
|
||||
public struct Iterator: AsyncIteratorProtocol, Sendable {
|
||||
public mutating func next() async -> Element?
|
||||
}
|
||||
|
||||
public init(element elementType: Element.Type = Element.self)
|
||||
|
||||
public func send(_ element: Element) async
|
||||
public func finish()
|
||||
|
||||
public func makeAsyncIterator() -> Iterator
|
||||
}
|
||||
|
||||
public final class AsyncThrowingChannel<Element: Sendable, Failure: Error>: AsyncSequence, Sendable {
|
||||
public struct Iterator: AsyncIteratorProtocol, Sendable {
|
||||
public mutating func next() async throws -> Element?
|
||||
}
|
||||
|
||||
public init(element elementType: Element.Type = Element.self, failure failureType: Failure.Type = Failure.self)
|
||||
|
||||
public func send(_ element: Element) async
|
||||
public func fail(_ error: Error) where Failure == Error
|
||||
public func finish()
|
||||
|
||||
public func makeAsyncIterator() -> Iterator
|
||||
}
|
||||
```
|
||||
|
||||
Channels are intended to be used as communication types between tasks. Particularly when one task produces values and another task consumes said values. On the one hand, the back pressure applied by `send(_:)` via the suspension/resume ensures that the production of values does not exceed the consumption of values from iteration. This method suspends after enqueuing the event and is resumed when the next call to `next()` on the `Iterator` is made. On the other hand, the call to `finish()` or `fail(_:)` immediately resumes all the pending operations for every producers and consumers. Thus, every suspended `send(_:)` operations instantly resume, so as every suspended `next()` operations by producing a nil value, or by throwing an error, indicating the termination of the iterations. Further calls to `send(_:)` will immediately resume. The calls to `send(:)` and `next()` will immediately resume when their supporting task is cancelled, other operations from other tasks will remain active.
|
||||
|
||||
```swift
|
||||
let channel = AsyncChannel<String>()
|
||||
Task {
|
||||
while let resultOfLongCalculation = doLongCalculations() {
|
||||
await channel.send(resultOfLongCalculation)
|
||||
}
|
||||
channel.finish()
|
||||
}
|
||||
|
||||
for await calculationResult in channel {
|
||||
print(calculationResult)
|
||||
}
|
||||
```
|
||||
|
||||
The example above uses a task to perform intense calculations; each of which are sent to the other task via the `send(_:)` method. That call to `send(_:)` returns when the next iteration of the channel is invoked.
|
||||
|
||||
## Alternatives Considered
|
||||
|
||||
The use of the name "subject" was considered, due to its heritage as a name for a sync-to-async adapter type.
|
||||
|
||||
It was considered to make `AsyncChannel` and `AsyncThrowingChannel` actors, however due to the cancellation internals it would imply that these types would need to create new tasks to handle cancel events. The advantages of an actor in this particular case did not outweigh the impact of adjusting the implementations to be actors.
|
||||
|
||||
## Future Directions
|
||||
|
||||
`AsyncChannel` and `AsyncThrowingChannel` are just the prominent members of the channel-like behavior algorithms. It is reasonable to have as its own distinct type a buffering channel that provides more [tuned back pressure per a given buffer of elements](https://forums.swift.org/t/asyncchannel-should-we-allow-to-buffer/60876). These other members of the same category of algorithm should be considered on their own as distinct proposals.
|
||||
|
||||
## Credits/Inspiration
|
||||
|
||||
`AsyncChannel` and `AsyncThrowingChannel` was heavily inspired from `Subject` but with the key difference that it uses Swift concurrency to apply back pressure.
|
||||
|
||||
https://developer.apple.com/documentation/combine/subject/
|
||||
+4
-16
@@ -13,7 +13,10 @@ let package = Package(
|
||||
products: [
|
||||
.library(name: "AsyncAlgorithms", targets: ["AsyncAlgorithms"]),
|
||||
],
|
||||
dependencies: [.package(url: "https://github.com/apple/swift-collections.git", .upToNextMajor(from: "1.0.4"))],
|
||||
dependencies: [
|
||||
.package(url: "https://github.com/apple/swift-collections.git", from: "1.0.4"),
|
||||
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
|
||||
],
|
||||
targets: [
|
||||
.target(
|
||||
name: "AsyncAlgorithms",
|
||||
@@ -46,18 +49,3 @@ let package = Package(
|
||||
),
|
||||
]
|
||||
)
|
||||
|
||||
#if canImport(Darwin)
|
||||
import Darwin
|
||||
let buildingDocs = getenv("BUILDING_FOR_DOCUMENTATION_GENERATION") != nil
|
||||
#elseif canImport(Glibc)
|
||||
import Glibc
|
||||
let buildingDocs = getenv("BUILDING_FOR_DOCUMENTATION_GENERATION") != nil
|
||||
#else
|
||||
let buildingDocs = false
|
||||
#endif
|
||||
|
||||
// Only require the docc plugin when building documentation
|
||||
package.dependencies += buildingDocs ? [
|
||||
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
|
||||
] : []
|
||||
+4
-16
@@ -16,7 +16,10 @@ let package = Package(
|
||||
.library(name: "_CAsyncSequenceValidationSupport", type: .static, targets: ["AsyncSequenceValidation"]),
|
||||
.library(name: "AsyncAlgorithms_XCTest", targets: ["AsyncAlgorithms_XCTest"]),
|
||||
],
|
||||
dependencies: [.package(url: "https://github.com/apple/swift-collections.git", .upToNextMajor(from: "1.0.4"))],
|
||||
dependencies: [
|
||||
.package(url: "https://github.com/apple/swift-collections.git", from: "1.0.4"),
|
||||
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
|
||||
],
|
||||
targets: [
|
||||
.target(
|
||||
name: "AsyncAlgorithms",
|
||||
@@ -34,18 +37,3 @@ let package = Package(
|
||||
dependencies: ["AsyncAlgorithms", "AsyncSequenceValidation", "AsyncAlgorithms_XCTest"]),
|
||||
]
|
||||
)
|
||||
|
||||
#if canImport(Darwin)
|
||||
import Darwin
|
||||
let buildingDocs = getenv("BUILDING_FOR_DOCUMENTATION_GENERATION") != nil
|
||||
#elseif canImport(Glibc)
|
||||
import Glibc
|
||||
let buildingDocs = getenv("BUILDING_FOR_DOCUMENTATION_GENERATION") != nil
|
||||
#else
|
||||
let buildingDocs = false
|
||||
#endif
|
||||
|
||||
// Only require the docc plugin when building documentation
|
||||
package.dependencies += buildingDocs ? [
|
||||
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
|
||||
] : []
|
||||
@@ -49,6 +49,10 @@ public struct AsyncChunksOfCountSequence<Base: AsyncSequence, Collected: RangeRe
|
||||
return nil
|
||||
}
|
||||
|
||||
if count == 1 {
|
||||
return Collected(CollectionOfOne(first))
|
||||
}
|
||||
|
||||
var result: Collected = .init()
|
||||
result.append(first)
|
||||
|
||||
|
||||
@@ -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.
|
||||
@@ -81,7 +81,16 @@ extension AsyncThrottleSequence: AsyncSequence {
|
||||
let start = last ?? clock.now
|
||||
repeat {
|
||||
guard let element = try await base.next() else {
|
||||
return nil
|
||||
if reduced != nil, let last {
|
||||
// ensure the rate of elements never exceeds the given interval
|
||||
let amount = interval - last.duration(to: clock.now)
|
||||
if amount > .zero {
|
||||
try? await clock.sleep(until: clock.now.advanced(by: amount), tolerance: nil)
|
||||
}
|
||||
}
|
||||
// the last value is unable to have any subsequent
|
||||
// values so always return the last reduction
|
||||
return reduced
|
||||
}
|
||||
let reduction = await reducing(reduced, element)
|
||||
let now = clock.now
|
||||
@@ -101,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 { }
|
||||
@@ -13,59 +13,49 @@
|
||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||
public struct AsyncTimerSequence<C: Clock>: AsyncSequence {
|
||||
public typealias Element = C.Instant
|
||||
|
||||
|
||||
/// The iterator for an `AsyncTimerSequence` instance.
|
||||
public struct Iterator: AsyncIteratorProtocol {
|
||||
var clock: C?
|
||||
let interval: C.Instant.Duration
|
||||
let tolerance: C.Instant.Duration?
|
||||
var last: C.Instant?
|
||||
|
||||
|
||||
init(interval: C.Instant.Duration, tolerance: C.Instant.Duration?, clock: C) {
|
||||
self.clock = clock
|
||||
self.interval = interval
|
||||
self.tolerance = tolerance
|
||||
}
|
||||
|
||||
func nextDeadline(_ clock: C) -> C.Instant {
|
||||
let now = clock.now
|
||||
let last = self.last ?? now
|
||||
let next = last.advanced(by: interval)
|
||||
if next < now {
|
||||
return last.advanced(by: interval * Int(((next.duration(to: now)) / interval).rounded(.up)))
|
||||
} else {
|
||||
return next
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public mutating func next() async -> C.Instant? {
|
||||
guard let clock = clock else {
|
||||
guard let clock = self.clock else {
|
||||
return nil
|
||||
}
|
||||
let next = nextDeadline(clock)
|
||||
|
||||
let next = (self.last ?? clock.now).advanced(by: self.interval)
|
||||
do {
|
||||
try await clock.sleep(until: next, tolerance: tolerance)
|
||||
try await clock.sleep(until: next, tolerance: self.tolerance)
|
||||
} catch {
|
||||
self.clock = nil
|
||||
return nil
|
||||
}
|
||||
let now = clock.now
|
||||
last = next
|
||||
self.last = next
|
||||
return now
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
let clock: C
|
||||
let interval: C.Instant.Duration
|
||||
let tolerance: C.Instant.Duration?
|
||||
|
||||
|
||||
/// Create an `AsyncTimerSequence` with a given repeating interval.
|
||||
public init(interval: C.Instant.Duration, tolerance: C.Instant.Duration? = nil, clock: C) {
|
||||
self.clock = clock
|
||||
self.interval = interval
|
||||
self.tolerance = tolerance
|
||||
}
|
||||
|
||||
|
||||
public func makeAsyncIterator() -> Iterator {
|
||||
Iterator(interval: interval, tolerance: tolerance, clock: clock)
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
@_implementationOnly import DequeModule
|
||||
import DequeModule
|
||||
|
||||
struct BoundedBufferStateMachine<Base: AsyncSequence> {
|
||||
typealias Element = Base.Element
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
@_implementationOnly import DequeModule
|
||||
import DequeModule
|
||||
|
||||
struct UnboundedBufferStateMachine<Base: AsyncSequence> {
|
||||
typealias Element = Base.Element
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
/// on the `Iterator` is made, or when `finish()` is called from another Task.
|
||||
/// As `finish()` induces a terminal state, there is no more need for a back pressure management.
|
||||
/// This function does not suspend and will finish all the pending iterations.
|
||||
public final class AsyncChannel<Element: Sendable>: AsyncSequence, @unchecked Sendable {
|
||||
public final class AsyncChannel<Element: Sendable>: AsyncSequence, Sendable {
|
||||
public typealias Element = Element
|
||||
public typealias AsyncIterator = Iterator
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
/// and is resumed when the next call to `next()` on the `Iterator` is made, or when `finish()`/`fail(_:)` is called
|
||||
/// from another Task. As `finish()` and `fail(_:)` induce a terminal state, there is no more need for a back pressure management.
|
||||
/// Those functions do not suspend and will finish all the pending iterations.
|
||||
public final class AsyncThrowingChannel<Element: Sendable, Failure: Error>: AsyncSequence, @unchecked Sendable {
|
||||
public final class AsyncThrowingChannel<Element: Sendable, Failure: Error>: AsyncSequence, Sendable {
|
||||
public typealias Element = Element
|
||||
public typealias AsyncIterator = Iterator
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
@_implementationOnly import OrderedCollections
|
||||
import OrderedCollections
|
||||
|
||||
// NOTE: this is only marked as unchecked since the swift-collections tag is before auditing for Sendable
|
||||
extension OrderedSet: @unchecked Sendable where Element: Sendable { }
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
@_implementationOnly import DequeModule
|
||||
import DequeModule
|
||||
|
||||
/// State machine for combine latest
|
||||
struct CombineLatestStateMachine<
|
||||
|
||||
@@ -13,14 +13,14 @@ extension AsyncSequence {
|
||||
/// Creates an asynchronous sequence that emits the latest element after a given quiescence period
|
||||
/// has elapsed by using a specified Clock.
|
||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||
public func debounce<C: Clock>(for interval: C.Instant.Duration, tolerance: C.Instant.Duration? = nil, clock: C) -> AsyncDebounceSequence<Self, C> where Self: Sendable {
|
||||
public func debounce<C: Clock>(for interval: C.Instant.Duration, tolerance: C.Instant.Duration? = nil, clock: C) -> AsyncDebounceSequence<Self, C> where Self: Sendable, Self.Element: Sendable {
|
||||
AsyncDebounceSequence(self, interval: interval, tolerance: tolerance, clock: clock)
|
||||
}
|
||||
|
||||
/// Creates an asynchronous sequence that emits the latest element after a given quiescence period
|
||||
/// has elapsed.
|
||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||
public func debounce(for interval: Duration, tolerance: Duration? = nil) -> AsyncDebounceSequence<Self, ContinuousClock> where Self: Sendable {
|
||||
public func debounce(for interval: Duration, tolerance: Duration? = nil) -> AsyncDebounceSequence<Self, ContinuousClock> where Self: Sendable, Self.Element: Sendable {
|
||||
self.debounce(for: interval, tolerance: tolerance, clock: .continuous)
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,7 @@ extension AsyncSequence {
|
||||
/// An `AsyncSequence` that emits the latest element after a given quiescence period
|
||||
/// has elapsed.
|
||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||
public struct AsyncDebounceSequence<Base: AsyncSequence, C: Clock>: Sendable where Base: Sendable {
|
||||
public struct AsyncDebounceSequence<Base: AsyncSequence & Sendable, C: Clock>: Sendable where Base.Element: Sendable {
|
||||
private let base: Base
|
||||
private let clock: C
|
||||
private let interval: C.Instant.Duration
|
||||
|
||||
@@ -10,10 +10,10 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||
struct DebounceStateMachine<Base: AsyncSequence, C: Clock> {
|
||||
struct DebounceStateMachine<Base: AsyncSequence & Sendable, C: Clock>: Sendable where Base.Element: Sendable {
|
||||
typealias Element = Base.Element
|
||||
|
||||
private enum State {
|
||||
private enum State: Sendable {
|
||||
/// The initial state before a call to `next` happened.
|
||||
case initial(base: Base)
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
|
||||
final class DebounceStorage<Base: AsyncSequence, C: Clock>: @unchecked Sendable where Base: Sendable {
|
||||
final class DebounceStorage<Base: AsyncSequence & Sendable, C: Clock>: Sendable where Base.Element: Sendable {
|
||||
typealias Element = Base.Element
|
||||
|
||||
/// The state machine protected with a lock.
|
||||
|
||||
@@ -10,11 +10,11 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if canImport(Darwin)
|
||||
@_implementationOnly import Darwin
|
||||
import Darwin
|
||||
#elseif canImport(Glibc)
|
||||
@_implementationOnly import Glibc
|
||||
import Glibc
|
||||
#elseif canImport(WinSDK)
|
||||
@_implementationOnly import WinSDK
|
||||
import WinSDK
|
||||
#endif
|
||||
|
||||
internal struct Lock {
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
@_implementationOnly import DequeModule
|
||||
import DequeModule
|
||||
|
||||
/// Creates an asynchronous sequence of elements from two underlying asynchronous sequences
|
||||
public func merge<Base1: AsyncSequence, Base2: AsyncSequence>(_ base1: Base1, _ base2: Base2) -> AsyncMerge2Sequence<Base1, Base2>
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
@_implementationOnly import DequeModule
|
||||
import DequeModule
|
||||
|
||||
/// Creates an asynchronous sequence of elements from two underlying asynchronous sequences
|
||||
public func merge<
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
@_implementationOnly import DequeModule
|
||||
import DequeModule
|
||||
|
||||
/// The state machine for any of the `merge` operator.
|
||||
///
|
||||
|
||||
@@ -12,9 +12,9 @@
|
||||
import _CAsyncSequenceValidationSupport
|
||||
|
||||
#if canImport(Darwin)
|
||||
@_implementationOnly import Darwin
|
||||
import Darwin
|
||||
#elseif canImport(Glibc)
|
||||
@_implementationOnly import Glibc
|
||||
import Glibc
|
||||
#elseif canImport(WinSDK)
|
||||
#error("TODO: Port TaskDriver threading to windows")
|
||||
#endif
|
||||
|
||||
@@ -24,6 +24,14 @@ func concatCharacters(_ array: [String]) -> String {
|
||||
}
|
||||
|
||||
final class TestChunk: XCTestCase {
|
||||
func test_count_one() {
|
||||
validate {
|
||||
"ABCDE|"
|
||||
$0.inputs[0].chunks(ofCount: 1).map(concatCharacters)
|
||||
"ABCDE|"
|
||||
}
|
||||
}
|
||||
|
||||
func test_signal_equalChunks() {
|
||||
validate {
|
||||
"ABC- DEF- GHI- |"
|
||||
|
||||
@@ -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,8 +71,8 @@ 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)
|
||||
"a--d--g--j-|"
|
||||
$0.inputs[0]._throttle(for: .steps(3), clock: $0.clock)
|
||||
"a--d--g--j--[k|]"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,8 +80,8 @@ 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)
|
||||
"a--b--e--h-|"
|
||||
$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,8 +134,26 @@ 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-|"
|
||||
}
|
||||
}
|
||||
|
||||
func test_trailing_delay_without_latest() throws {
|
||||
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)
|
||||
"a--b--e--h--[k|]"
|
||||
}
|
||||
}
|
||||
|
||||
func test_trailing_delay_with_latest() throws {
|
||||
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)
|
||||
"a--d--g--j--[l|]"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# This source file is part of the Swift.org 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
|
||||
# See https://swift.org/CONTRIBUTORS.txt for Swift project authors
|
||||
#
|
||||
# Updates the GitHub Pages documentation site thats published from the 'docs'
|
||||
# subdirectory in the 'gh-pages' branch of this repository.
|
||||
#
|
||||
# This script should be run by someone with commit access to the 'gh-pages' branch
|
||||
# at a regular frequency so that the documentation content on the GitHub Pages site
|
||||
# is up-to-date with the content in this repo.
|
||||
#
|
||||
|
||||
export BUILDING_FOR_DOCUMENTATION_GENERATION=1
|
||||
|
||||
set -eu
|
||||
|
||||
# A `realpath` alternative using the default C implementation.
|
||||
filepath() {
|
||||
[[ $1 = /* ]] && echo "$1" || echo "$PWD/${1#./}"
|
||||
}
|
||||
|
||||
SWIFT_ASYNC_ALGORITHMS_ROOT="$(dirname $(dirname $(filepath $0)))"
|
||||
|
||||
ASYNC_ALGORITHMS_BUILD_DIR="$SWIFT_ASYNC_ALGORITHMS_ROOT"/.build/async-algorithms-gh-pages-build
|
||||
|
||||
# Set current directory to the repository root
|
||||
cd "$SWIFT_ASYNC_ALGORITHMS_ROOT"
|
||||
|
||||
# Use git worktree to checkout the gh-pages branch of this repository in a gh-pages sub-directory
|
||||
git fetch
|
||||
git worktree add --checkout gh-pages origin/gh-pages
|
||||
|
||||
# Pretty print DocC JSON output so that it can be consistently diffed between commits
|
||||
export DOCC_JSON_PRETTYPRINT="YES"
|
||||
|
||||
# Generate documentation for the 'AsyncAlgorithms' target and output it
|
||||
# to the /docs subdirectory in the gh-pages worktree directory.
|
||||
swift package \
|
||||
--allow-writing-to-directory "$SWIFT_ASYNC_ALGORITHMS_ROOT/gh-pages/docs" \
|
||||
generate-documentation \
|
||||
--target AsyncAlgorithms \
|
||||
--disable-indexing \
|
||||
--transform-for-static-hosting \
|
||||
--hosting-base-path swift-async-algorithms \
|
||||
--output-path "$SWIFT_ASYNC_ALGORITHMS_ROOT/gh-pages/docs"
|
||||
|
||||
# Save the current commit we've just built documentation from in a variable
|
||||
CURRENT_COMMIT_HASH=`git rev-parse --short HEAD`
|
||||
|
||||
# Commit and push our changes to the gh-pages branch
|
||||
cd gh-pages
|
||||
git add docs
|
||||
|
||||
if [ -n "$(git status --porcelain)" ]; then
|
||||
echo "Documentation changes found. Commiting the changes to the 'gh-pages' branch and pushing to origin."
|
||||
git commit -m "Update GitHub Pages documentation site to $CURRENT_COMMIT_HASH"
|
||||
git push origin HEAD:gh-pages
|
||||
else
|
||||
# No changes found, nothing to commit.
|
||||
echo "No documentation changes found."
|
||||
fi
|
||||
|
||||
# Delete the git worktree we created
|
||||
cd ..
|
||||
git worktree remove gh-pages
|
||||
Reference in new issue
Block a user