Contents

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

Use Swift Package Manager:

.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 finished

Note: 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