InFlightValue

1.0.1

Awaits already in-flight calls and their value
sgade/InFlightValue

What's New

1.0.1

2021-12-10T23:27:45Z

Add @Sendable attribute

InFlightValue

Awaits already in-flight calls and their value.

A common application is when concurrent calls require authentication secrets which are expired and should only be refreshed one at a time. This acts as a queue for the result if another thread of execution already started the network request.

Usage

For each call that should only be executed one at a time, create a new instace of InFlightValueProvider:

let value = InFlightValueProvider {
    [...]
    let (data, _) = try await URLSession.default.data(for: request)
    return data
}

let result1 = try await value.get()
// from another queue...
let result2 = try await value.get()

This ensures that subsequent calls to the value while the request is in-flight will return with the same result as the original call. Once the value has been returned to all waiting threads, the provider will do its work once again.

Installation

Using the Swift Package Manager, add this line to your Package.swift as a dependency:

    .package(url: "https://github.com/sgade/InFlightValue", from: "1.0.0"),

Description

  • Swift Tools 5.5.0
View More Packages from this Author

Dependencies

  • None
Last updated: Sat Mar 16 2024 19:55:20 GMT-0900 (Hawaii-Aleutian Daylight Time)