ConcurrencyPlus

0.4.1

Utilities for working with Swift Concurrency
ChimeHQ/ConcurrencyPlus

What's New

v0.4.1

2022-12-16T15:24:44Z

Deprecating AsyncSubject and AsyncThrowingSubject. They are cool, but there are way better implementations out there and this just really isn't the right library for this stuff anyways.

Build Status License Platforms Documentation

ConcurrencyPlus

Utilities for working with Swift Concurrency

This is a really small library with some type and extensions that may be useful when working with Swift's concurrency system.

  • A TaskQueue for queuing tasks in FIFO ordering
  • CheckedContinuation extensions for improved ergonomics
  • Task extensions for improved ergonomics when used to bridge to non-async code
  • NSXPCConnection extensions for safe async integration
  • MainActor.runUnsafely to help work around incorrectly- or insufficiently-annotated code not under your control

TaskQueue

let queue = TaskQueue()

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

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

let value = try await task.value
// Without .ordered, the execution order of these tasks is not well-defined.
Task.ordered {
    event1()
}

Task.ordered(priority: .background) {
    event2()
}

Task.ordered {
    event3()
}

Working with XPC

You might be tempted to make your XPC interface functions async. This approach does not handle connection failures and will violate the Structured Concurrency contract, resulting in hangs. See the post "ExtensionKit and XPC" for context.

This little NSXPCConnection extension provides a safe way to get into the async world.

func withContinuation<Service, T>(
    function: String = #function, 
    _ body: (Service, CheckedContinuation<T, Error>) -> Void
) async throws -> T

There are also some extensions on CheckedContinuation to make it easier to use in the context of XPC. These are really handy for resuming from common reply patterns.

Given an XPC service like this in your code:

protocol XPCService {
    func errorMethod(reply: (Error?) -> Void)
    func valueAndErrorMethod(reply: (String?, Error?) -> Void)
    func dataAndErrorMethod(reply: (Data?, Error?) -> Void)
}

The continuation helpers allow bridging like:

try await withContinuation { service, continuation in
    service.errorMethod(reply: continuation.resumingHandler)
}

try await withContinuation { service, continuation in
    service.valueAndErrorMethod(reply: continuation.resumingHandler)
}

// this one will try to use JSONDecoder on the resulting data
try await withContinuation { service, continuation in
    service.dataAndErrorMethod(reply: continuation.resumingHandler)
}

Other Useful Projects

Right now, it's still quite difficult to make use of AsyncSequence. These libraries might be useful and are defintely worth checking out as well.

  • AnyAsyncSequence: super-focused on solving the lack type-erased sequences
  • AsyncAlgorithms: Apple-owned reactive extensions to AsyncSequence
  • AsyncExtensions: Companion to AsyncAlgorithms to add additional reactive features
  • Asynchrone: Extensions to bring reactive features to AsyncSequence

Suggestions or Feedback

We'd love to hear from you! Please open up an issue or pull request.

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

Description

  • Swift Tools 5.5.0
View More Packages from this Author

Dependencies

  • None
Last updated: Sun Jan 29 2023 22:34:56 GMT-0500 (GMT-05:00)