Skip to content

Commit

Permalink
Update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
gh123man committed Jul 18, 2024
1 parent d81c6fc commit 6ea9612
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,28 @@ Performant channels for Swift concurrency.
>
> \- Rob Pike
If you are familiar with golang and the go ecosystem, you can skip to the [go comparisons section.](/GolangVsSwift.md)

Channels are a typed conduit through which you can send and receive values - usually across threads or in this case, Swift async tasks. This library is modeled after go's channel behaviors.

If you are familiar with golang and the go ecosystem, you can skip to the [go comparisons section.](/GolangVsSwift.md)

## Example

```swift
let msg = Channel<String>(capacity: 3)
let done = Channel<Bool>()

await msg <- "Swift"
await msg <- "❤️"
await msg <- "Channels"
msg.close()

Task {
for await message in msg {
print(message)
}
await done <- true
}

await msg <- "Swift"
await msg <- "❤️"
await msg <- "Channels"

msg.close()
await <-done
```

Expand Down Expand Up @@ -214,6 +215,8 @@ await <-done
## Advanced Usage
This library also includes some extra features that are made possible by the flexibility of Swift's `resultBuilder`.

### Examples

Multiplexing `n:1` channels using select `any`
```swift
let channels = (0..<100).map { _ in Channel<Bool>() }
Expand Down Expand Up @@ -246,6 +249,25 @@ for await _ in collected {
}
```

Conditional cases
```swift
let a = Channel<String>()
let b = Channel<String>()

Task {
await a <- "foo"
}

var enableRecieve = true
await select {
if enableRecieve {
receive(a) { await result <- $0! }
}
send("b", to: b)
}

```

## Code Samples

See the [Examples](/Examples/) folder for real world usage.
Expand Down

0 comments on commit 6ea9612

Please sign in to comment.