Skip to content

fix documentation errors #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
.DS_Store
.swiftpm
.vscode
.build
Sources/**/*.html
/.DS_Store
/.swiftpm
/.vscode
/.build
/.build.ssgc
/.ssgc
/Sources/**/*.html
/.build.ssgc
/.index-build
docc
.build.ssgc
.index-build
142 changes: 98 additions & 44 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0"),
.package(url: "https://github.com/hummingbird-project/hummingbird-auth.git", from: "2.0.2"),
.package(url: "https://github.com/hummingbird-project/hummingbird-websocket.git", from: "2.0.0"),
.package(url: "https://github.com/hummingbird-project/swift-mustache.git", from: "2.0.0-beta.3"),
.package(url: "https://github.com/hummingbird-project/swift-jobs.git", from: "1.0.0-beta.6"),
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.21.1"),
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.5.0"),
.package(url: "https://github.com/apple/swift-nio.git", from: "2.65.0"),
Expand All @@ -32,10 +34,12 @@ let package = Package(
.product(name: "HummingbirdRouter", package: "hummingbird"),
.product(name: "AsyncHTTPClient", package: "async-http-client"),
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "Jobs", package: "swift-jobs"),
.product(name: "NIOCore", package: "swift-nio"),
.product(name: "NIOPosix", package: "swift-nio"),
.product(name: "Logging", package: "swift-log"),
.product(name: "ServiceLifecycle", package: "swift-service-lifecycle"),
.product(name: "HummingbirdAuth", package: "hummingbird-auth"),
.product(name: "HummingbirdWebSocket", package: "hummingbird-websocket"),
.product(name: "HummingbirdWSCompression", package: "hummingbird-websocket"),
.product(name: "Mustache", package: "swift-mustache"),
Expand Down
2 changes: 1 addition & 1 deletion Snippets/advanced-async-sequences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ extension DelayedElementEmitter {
}
}
// snippet.end
// snipet.delayedprint
// snippet.delayedprint
let delayedEmitter = DelayedElementEmitter(
elements: [1, 2, 3, 4, 5],
delay: .seconds(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ When you're using a `for .. in` loop to iterate elements, you're always leveragi

### IteratorProtocol

Sequence implementations only need to provide one function, the ``Sequence/makeIterator`` function. This function creates an ``IteratorProtocol`` that you implement as well.
Sequence implementations only need to provide one function, the ``Sequence/makeIterator [requirement]`` function. This function creates an ``IteratorProtocol`` that you implement as well.

_IteratorProtocol_ should be implemented as a `struct`, and has a single function called ``IteratorProtocol/next()``. This _mutating_ function returns the next element in the sequence.

Expand Down Expand Up @@ -113,9 +113,9 @@ The `downloadState` property is set to `downloading` before parallelising work.

## Implementing Custom AsyncSequence

You can implement your own ``AsyncSequence`` by implementing the ``AsyncSequence`` protocol. This protocol has two associated types: ``Element`` and ``AsyncIterator``. You'll need to implement the ``AsyncSequence/makeAsyncIterator()`` function and the ``AsyncIteratorProtocol``.
You can implement your own ``AsyncSequence`` by implementing the ``AsyncSequence`` protocol. This protocol has two associated types: ``AsyncSequence/Element`` and ``AsyncSequence/AsyncIterator``. You'll need to implement the ``AsyncSequence/makeAsyncIterator()`` function and the ``AsyncIteratorProtocol``.

First we'll define our custom ``DelayedElementEmitter`` struct. This struct will emit elements from an array with a delay between each element.
First we'll define our custom `DelayedElementEmitter` struct. This struct will emit elements from an array with a delay between each element.

@Snippet(path: "site/Snippets/advanced-async-sequences", slice: "delayedelementemitter")

Expand All @@ -129,7 +129,7 @@ You can now use this custom ``AsyncSequence`` in your code. The following exampl

@Snippet(path: "site/Snippets/advanced-async-sequences", slice: "delayedprint")

Note that our custom ``DelayedElementEmitter`` cannot be iterated upon without a `try` keyword to handle errors. This is because the ``AsyncIteratorProtocol/next()`` function can throw errors due to being marked as `throws`. We can also handle the ``CancellationError``s that ``Task.sleep(for:tolerance:clock:)`` throws in the `next()` function.
Note that our custom `DelayedElementEmitter` cannot be iterated upon without a `try` keyword to handle errors. This is because the ``AsyncIteratorProtocol/next()`` function can throw errors due to being marked as `throws`. We can also handle the ``CancellationError``s that ``Task.sleep(for:tolerance:clock:)`` throws in the `next()` function.

```swift
mutating func next() async -> Element? {
Expand All @@ -155,7 +155,7 @@ The ``AsyncIteratorProtocol/next()`` function can be cancelled by the consumer.

It is expected that Async Sequences handle cancellation gracefully as appropriate. In Networking, this could mean cancelling a network request. In a generator, this could mean stopping the generation of new elements through ``AsyncStream.Continuation.finish()``.

To detect a cancellation signal, use ``withTaskCancellationHandler(operation:onCancel:)``. Or when using Swift Service Lifecycle, use ``withTaskCancellationOrGracefulShutdownHandler(operation:onCancelOrGracefulShutdown:)``.
To detect a cancellation signal, use ``withTaskCancellationHandler(operation:onCancel:isolation:)``. Or when using Swift Service Lifecycle, use ``withTaskCancellationOrGracefulShutdownHandler(isolation:operation:onCancelOrGracefulShutdown:)``.

## Changes in Swift 6

Expand Down
Loading
Loading