Skip to content

rickymohk/SwiftTaskQueue

Repository files navigation

SwiftTaskQueue

A library to serialize async Task. Also support AsyncThrowingStream.

Swift Version Build Status License

This library helps organizing async tasks into a queue, making sure multiple tasks are executed one by one. This could be handy when multiple tasks are being fired at arbitrary moment but must not run concurrently.

Installation

Add this project on your Package.swift

import PackageDescription

let package = Package(
    dependencies: [
        .package(url: "https://github.com/rickymohk/SwiftTaskQueue.git", .branch("main"))
    ]
)

Usage example

import SwiftTaskQueue

let taskQueue = TaskQueue()

Task {

    // Fire and forget — enqueue without waiting for result
    taskQueue.dispatch {
        // your async code here
    }

    // Enqueue and await the result
    let result = try await taskQueue.dispatchAndWait {
        // your async code here
        return "result"
    }

    // Enqueue and get a Task handle — await the result later
    let deferred = taskQueue.dispatchTask {
        // your async code here
        return "result"
    }
    // ... do other work ...
    let deferredResult = try await deferred.value

    // Create an AsyncThrowingStream in the queue
    let stream = taskQueue.dispatchStream { continuation in
        // your stream builder here
        continuation.yield("result")
        // remember to call finish when done, otherwise the queue will be blocked
        continuation.finish()
    }

    do {
        for try await result in stream {
            // use the result from the stream
        }
    }
    catch {
        print("stream error \(error)")
    }
}

Migration guide — 2.0.0 breaking changes

Method renames

1.x 2.0 Notes
dispatch { } (non-async, void) dispatch { } Fire-and-forget, no result (unchanged)
try await dispatch { … } (returns T) try await dispatchAndWait { … } Enqueue and suspend until result is ready
(new) dispatchTask { … } Enqueue and return a Task<T, Error> handle immediately; await .value whenever needed

dispatchStream is unchanged.

About

A library to serialize async `Task`. Also support `AsyncThrowingStream`.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages