0xfeedface1993/swift-async-conveyor
A lightweight Swift utility that ensures async tasks run in strict FIFO order, providing simple and efficient serial execution for concurrency control across all Apple platforms.
π§ Features
- π Serial execution of async closures
- β Supports structured concurrency and cancellation handling
- π§΅ Lock-free internal implementation using
ManagedCriticalState - π§ͺ Well-suited for unit testing and task coordination scenarios
- π§ No external dependencies except Swift standard concurrency APIs
π¦ Requirements
- Swift 5.9+
- Concurrency enabled (Swift Concurrency runtime)
- Supported Platforms:
- iOS 13+ - macOS 10.15+ - watchOS 6+ - tvOS 13+
π Installation
.package(url: "https://github.com/0xfeedface1993/swift-async-conveyor.git", from: "1.0.0")Then add "AsyncConveyor" to your target dependencies.
π§βπ» Usage
import AsyncConveyor
let conveyor = AsyncConveyor()
Task {
try await conveyor.run {
print("Task A starting")
try await Task.sleep(nanoseconds: 1_000_000_000)
print("Task A finished")
}
}
Task {
try await conveyor.run {
print("Task B starting")
try await Task.sleep(nanoseconds: 500_000_000)
print("Task B finished")
}
}β Output
Task A starting
Task A finished
Task B starting
Task B finishedNote: Even though Task B starts earlier in wall time, it waits for Task A to complete before executing.
π Cancellation Safety
If a task is cancelled while waiting or running, AsyncConveyor ensures:
- The task is properly removed from the internal queue
- The next task resumes correctly
- No memory leaks or deadlocks
π Use Cases
- Serializing file reads/writes
- Managing access to a database or cache
- Enforcing order of operations in state machines
- Coordinating request pipelines
- Preventing overlapping animations or transitions
π§ͺ Testing Tips
- Each .run { } block is awaited and can be tested deterministically.
- Consider injecting AsyncConveyor as a dependency when testing logic that depends on ordering.
π§ Design Philosophy
AsyncConveyor follows a minimal locking, actor-free design by using ManagedCriticalState to efficiently manage internal state. It behaves similarly to a serial DispatchQueue, but with native async/await and structured cancellation.
π· Contributions
Contributions and feedback are welcome! Please open issues or PRs.
Package Metadata
Repository: 0xfeedface1993/swift-async-conveyor
Stars: 0
Forks: 0
Open issues: 0
Default branch: main
Primary language: swift
License: MIT
README: README.md