• Stars
    star
    187
  • Rank 199,727 (Top 5 %)
  • Language
    Swift
  • License
    BSD 3-Clause "New...
  • Created 11 months ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A queue for Swift concurrency

Build Status Platforms Documentation

Queue

A queue for Swift concurrency

This package exposes a single type: AsyncQueue. Conceptually, AsyncQueue is very similar to a DispatchQueue or OperationQueue. However, unlike these an AsyncQueue can accept async blocks. This exists to more easily enforce ordering across unstructured tasks without requiring explicit dependencies between them.

I've found this helpful when interfacing stateful asynchronous systems with synchronous code.

Integration

dependencies: [
    .package(url: "https://github.com/mattmassicotte/Queue", from: "0.1.4")
]

Usage

let queue = AsyncQueue()

queue.addOperation {
    await asyncFunction()
    await anotherAsyncFunction()
}

// This can can also return the underlying Task,
// so you can cancel, or await a value
let task = queue.addOperation {
    return await makeValue()
}

let value = try await task.value

By default, AsyncQueue will only run one operation at a time. But, it can be configured as a concurrent queue.

let queue = AsyncQueue(attributes: [.concurrent])

// these two may run concurrently
queue.addOperation { await asyncFunction() }
queue.addOperation { await asyncFunction() }

// This will only run once existing operations are complete, and will
// prevent new operations from starting until done
queue.addBarrierOperation {
    await asyncFunction()
}

The AsyncQueue type has an errorSequence property, which can be used to detect uncaught errors out of band.

let queue = AsyncQueue(attributes: [.publishErrors])

Task {
    for await error in queue.errorSequence {
        print(error)
    }
}

This package was inspired by Semaphore, which is another concurrency-related synchronization system that I've found very useful.

Alternatives

Contributing and Collaboration

I'd love to hear from you! Get in touch via mastodon, an issue, or a pull request.

I prefer collaboration, and would love to find ways to work together if you have a similar project.

I prefer indentation with tabs for improved accessibility. But, I'd rather you use the system you want and make a PR than hesitate because of whitespace.

By participating in this project you agree to abide by the Contributor Code of Conduct.

More Repositories

1

ConcurrencyRecipes

Practical solutions to problems with Swift Concurrency
Swift
699
star
2

MainOffender

Utilities for transitioning to Swift Concurrency
Swift
92
star
3

XCLint

Xcode project linting
Swift
75
star
4

kernel-tutorial

A step-by-step tutorial for building a simple kernel
C
59
star
5

nsui

AppKit and UIKit without conditional compilation
Swift
42
star
6

TextEditingReference

A non-curated collection of tools for working with text on Apple platforms
42
star
7

PackageTemplate

A template for Swift packages on GitHub
Swift
38
star
8

AppTemplate

A template for setting up targets within Xcode
Swift
26
star
9

swift-passkeys

An experiment building passkey-based auth with Swift and AWS
Swift
17
star
10

three

Three Programming Language
C++
6
star
11

XCConfig

xcconfig file parsing and evaluation
Swift
4
star
12

rake-file-content

Ruby rake file tasks with content-based dependencies
Ruby
3
star
13

swift-keyspaces

An experiment connecting Swift to AWS Keyspaces (Cassandra)
Swift
2
star
14

cosl

Collaborative Open Source License
2
star
15

config

dot files and configuration
Shell
2
star
16

OAuthenticator

Lightweight OAuth 2.0 request authentication in Swift
1
star
17

rake-multifile

Rake file task with automatic parallel dependencies
Ruby
1
star
18

rake-compile

A set of Rake DSL extensions for compiling native code
Ruby
1
star
19

vex

Makefile
1
star
20

osin-cassandra-store

Cassandra storage for the OSIN Go Oauth library
Go
1
star
21

rake-remote-file

Ruby Rake extension to support remote file task
Ruby
1
star
22

vex-generator

Rust
1
star
23

c-dependecies

Manage C/C++ file dependencies with Ruby
Ruby
1
star
24

Packet

AsyncSequence Data transforms
Swift
1
star