Skip to content

Create Task API -> 0.7.0 #51

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 8 commits into from
Apr 8, 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
2 changes: 1 addition & 1 deletion AsyncQueue.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'AsyncQueue'
s.version = '0.6.1'
s.version = '0.7.0'
s.license = 'MIT'
s.summary = 'A queue that enables ordered sending of events from synchronous to asynchronous code.'
s.homepage = 'https://github.com/dfed/swift-async-queue'
Expand Down
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ func testFIFOQueueOrdering() async {
actor Counter {
nonisolated
func incrementAndAssertCountEquals(_ expectedCount: Int) {
queue.enqueue {
Task(on: queue) {
await self.increment()
let incrementedCount = await self.count
XCTAssertEqual(incrementedCount, expectedCount) // always succeeds
}
}

func flushQueue() async {
await queue.enqueueAndWait { }
await Task(on: queue) {}.value
}

func increment() {
Expand Down Expand Up @@ -101,14 +101,14 @@ func testActorQueueOrdering() async {

nonisolated
func incrementAndAssertCountEquals(_ expectedCount: Int) {
queue.enqueue { myself in
await Task(on: queue) { myself in
myself.count += 1
XCTAssertEqual(expectedCount, myself.count) // always succeeds
}
}

func flushQueue() async {
await queue.enqueueAndWait { _ in }
await Task(on: queue) {}.value
}

private var count = 0
Expand All @@ -127,25 +127,25 @@ func testActorQueueOrdering() async {

### Sending ordered asynchronous tasks to the `@MainActor` from a nonisolated context

Use a `MainActorQueue` to send ordered asynchronous tasks to the `@MainActor`’s isolated context from nonisolated or synchronous contexts. Tasks sent to this queue type are guaranteed to begin executing in the order in which they are enqueued. Like an `ActorQueue`, execution order is guaranteed only until the first [suspension point](https://docs.swift.org/swift-book/LanguageGuide/Concurrency.html#ID639) within the enqueued task. A `MainActorQueue` executes tasks within its adopted actor’s isolated context, resulting in `MainActorQueue` task execution having the same properties as a `@MainActor`'s' code execution: code between suspension points is executed atomically, and tasks sent to a single `MainActorQueue` can await results from the queue without deadlocking.
Use `MainActor.queue` to send ordered asynchronous tasks to the `@MainActor`’s isolated context from nonisolated or synchronous contexts. Tasks sent to this queue type are guaranteed to begin executing in the order in which they are enqueued. Like an `ActorQueue`, execution order is guaranteed only until the first [suspension point](https://docs.swift.org/swift-book/LanguageGuide/Concurrency.html#ID639) within the enqueued task. A `MainActor.queue` executes tasks within its adopted actor’s isolated context, resulting in `MainActor.queue` task execution having the same properties as a `@MainActor`'s' code execution: code between suspension points is executed atomically, and tasks sent to a single `MainActor.queue` can await results from the queue without deadlocking.

A `MainActorQueue` can easily execute asynchronous tasks from a nonisolated context in FIFO order:
A `MainActor.queue` can easily execute asynchronous tasks from a nonisolated context in FIFO order:
```swift
@MainActor
func testMainActorQueueOrdering() async {
@MainActor
final class Counter {
nonisolated
func incrementAndAssertCountEquals(_ expectedCount: Int) {
MainActorQueue.shared.enqueue {
Task(on: MainActor.queue) {
self.increment()
let incrementedCount = self.count
XCTAssertEqual(incrementedCount, expectedCount) // always succeeds
}
}

func flushQueue() async {
await MainActorQueue.shared.enqueueAndWait { }
await Task(on: MainActor.queue) { }.value
}

func increment() {
Expand Down Expand Up @@ -181,7 +181,7 @@ To install swift-async-queue in your project with [Swift Package Manager](https:

```swift
dependencies: [
.package(url: "https://github.com/dfed/swift-async-queue", from: "0.6.0"),
.package(url: "https://github.com/dfed/swift-async-queue", from: "0.7.0"),
]
```

Expand All @@ -190,7 +190,7 @@ dependencies: [
To install swift-async-queue in your project with [CocoaPods](http://cocoapods.org), add the following to your `Podfile`:

```
pod 'AsyncQueue', '~> 0.6.0'
pod 'AsyncQueue', '~> 0.7.0'
```

## Contributing
Expand Down
Loading