New TaskUtilities library for Swift

Today, I published a new Swift library called TaskUtilities, containing code that was originally in my WebSocketActors library. This library serves two purposes:

Understanding task structure

In complex asynchronous code, it is sometimes difficult to know what task is currently running and where it originated. TaskPath lets you name a task and then retrieve the current task name from an arbitrary point in your code.

Task.detached {
    TaskPath.with(name: "Fetch image") {
        ...
        TaskPath.with(name: "Constructing request") {
            ...
        }
    }
}

// In other code called from the inner task:
print(TaskPath.current) // Prints "{Task Fetch image > Constructing request}"

Making synchronous code async-safe

If synchronous code like a class has mutable state, it is not safe to call that code from asynchronous tasks. The preferred way to solve this problem is to change the class into an actor, but there are situations where that may not be possible because the code must be called from a synchronous context.

In these situations, RecursiveTaskLock and LockedValue provide a way to ensure that the mutable state is only accessed from one task at a time.

TaskUtilities

If these features sound helpful, please give my TaskUtilities project a try. I look forward to your feedback!

Stuart Malone @samalone