Swiftpack.co - swhitty/Awaiting as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by swhitty.
swhitty/Awaiting 0.5.0
Swift @propertyWrapper that waits asynchronously until the value matches a predicate.
⭐️ 19
🕓 1 year ago
iOS macOS
.package(url: "https://github.com/swhitty/Awaiting.git", from: "0.5.0")

Build Codecov Platforms Swift 5.5 License Twitter

Introduction

@Awaiting is a Swift @propertyWrapper that waits asynchronously until the value matches a predicate.

Usage

Any class can declare a property to be @Awaiting as follows:

@Awaiting var isComplete: Bool = false

You then use its projected value to await until some predicate is met;

// Suspends until isComplete == true
_ = try await $isComplete.first(where: { $0 == true })

Cancellation

CancellationError is thrown if the task is cancelled before the predicate is met.

Optionals

When optionals are wrapped you can wait for the first non nil value:

@Awaiting var name: String?

// Suspends until name != nil
let name = try await $name.some()

Equatables

When equatables are wrapped you can wait for the first value that equals an element:

@Awaiting var name: String?

// Suspends until name == "Fish"
try await $name.equals("Fish")

Collections

When collections are wrapped you can wait for an element to exist at an index:

@Awaiting var names = [String](https://raw.github.com/swhitty/Awaiting/main/)

// Suspends until names[2] exists
let name = try await $names.value(at: 2)

Or wait for at least n elements to exist:

// Suspends until names.count >= 3
let nonEmpty = try await $names.first(withAtLeast: 3)

Credits

@Awaiting is primarily the work of Simon Whitty.

GitHub

link
Stars: 19
Last commit: 13 weeks ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Related Packages

Release Notes

Equatable Waiter
1 year ago

Adds a specialized waiters for Equatable elements:

@Awaiting var name: String?

// Suspends until name == "Fish"
try await $name.equals("Fish")

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics