ITADN
swift-foundations/swift-async
swift-foundations/swift-async · 文件
文件最后提交记录最后更新时间
README.md

swift-async

Development Status

Composable asynchronous streams and isolation-preserving AsyncSequence operators for Swift, built around a concrete Async.Stream type that composes through reactive combinators without type erasure.


Key Features

  • Concrete Async.Stream<Element> — a single Sendable AsyncSequence type that can be stored, passed around, and composed without any AsyncSequence erasure or generic type explosion.
  • Reactive combinatorsmerge, zip, combine.latest, debounce, throttle, sample, scan, delay, and timeout over the concrete stream type.
  • Multicastshare() and replay give multiple consumers a single upstream subscription instead of one per for await.
  • Isolation-preserving sequence operatorsmap, filter, compactMap, and flatMap overloads that accept synchronous closures and run inline on the caller's actor rather than hopping to the cooperative pool.
  • Nested-accessor API — variant transforms live under accessors (stream.map.compact, stream.map.flat.latest, stream.zip(other)) instead of compound method names.
  • Stream constructors — build a stream from a sequence, an interval, a timer, or any existing AsyncSequence via .from, .interval, .just, .empty, and .never.

Quick Start

A stream assembled from an interval and a stream built from a literal sequence are the same concrete type — Async.Stream<String> — so merge composes them uniformly. Without a concrete stream type, combining heterogeneously-constructed async sequences forces any AsyncSequence erasure or a bespoke merging AsyncIteratorProtocol:

import Async

let ticks = Async.Stream.interval(.seconds(1))
    .map { "tick #\($0)" }                  // Async.Stream<String>

let messages = Async.Stream.from(["ping", "pong"])  // Async.Stream<String>

let merged = Async.Stream.merge(ticks, messages)    // Async.Stream<String>

for await line in merged {
    print(line)
}

Installation

dependencies: [
    .package(url: "https://github.com/swift-foundations/swift-async.git", branch: "main")
]
.target(
    name: "YourTarget",
    dependencies: [
        .product(name: "Async", package: "swift-async")
    ]
)

Requires Swift 6.3.1 and macOS 26 / iOS 26 / tvOS 26 / watchOS 26 / visionOS 26.


Architecture

The Async umbrella product re-exports the stream type, the sequence operators, and the underlying async coordination primitives. Narrower products are available when only one surface is needed.

ProductImportWhen to import
Asyncimport AsyncThe umbrella — the concrete stream type, the AsyncSequence operators, and the coordination primitives in one import.
Async Sequenceimport Async_SequenceOnly the isolation-preserving lazy operators (map / filter / compactMap / flatMap) over any AsyncSequence.
Async Streamimport Async_StreamOnly the concrete Async.Stream type and its reactive combinators.
Async Test Supportimport Async_Test_SupportTest targets exercising async streams.

Community

Discussion thread will be created at first public release.

License

Apache 2.0. See LICENSE.