• Stars
    star
    246
  • Rank 164,726 (Top 4 %)
  • Language
    Swift
  • License
    BSD 3-Clause "New...
  • Created over 1 year 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
1,099
star
2

MainOffender

Utilities for transitioning to Swift Concurrency
Swift
131
star
3

XCLint

Xcode project linting
Swift
90
star
4

TextEditingReference

A non-curated collection of tools for working with text on Apple platforms
78
star
5

kernel-tutorial

A step-by-step tutorial for building a simple kernel
C
65
star
6

Empire

A local persistence system for Swift
Swift
65
star
7

nsui

AppKit and UIKit without conditional compilation
Swift
56
star
8

PackageTemplate

A template for Swift packages on GitHub
Swift
54
star
9

Lock

A lock for Swift concurrency
Swift
38
star
10

AppTemplate

A template for setting up targets within Xcode
Swift
28
star
11

Packet

AsyncSequence Data transforms
Swift
26
star
12

swift-passkeys

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

three

Three Programming Language
C++
8
star
14

Nero

A watchOS app to help you care for aquariums
Swift
6
star
15

TaskProgress

A progress mechanism for Swift Concurrency
5
star
16

XCConfig

xcconfig file parsing and evaluation
Swift
4
star
17

rake-file-content

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

swift-keyspaces

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

cosl

Collaborative Open Source License
2
star
20

config

dot files and configuration
Shell
2
star
21

OAuthenticator

Lightweight OAuth 2.0 request authentication in Swift
1
star
22

rake-compile

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

rake-multifile

Rake file task with automatic parallel dependencies
Ruby
1
star
24

vex

Makefile
1
star
25

osin-cassandra-store

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

rake-remote-file

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

vex-generator

Rust
1
star
28

c-dependecies

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