Swiftpack.co - KKLater/OpenCombineAsync as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by KKLater.
KKLater/OpenCombineAsync 0.0.1
async and await api base on openCombine
⭐️ 0
🕓 3 years ago
iOS macOS watchOS tvOS
.package(url: "https://github.com/KKLater/OpenCombineAsync.git", from: "0.0.1")

OpenCombineAsync

Release Install Platform

简体中文

OpenCombineAsync 'is an encapsulation based on OpenCombine Future. It encapsulates Future and provides async and await API to handle asynchronous events gracefully.

Requirements

  • macOS 10.10 + / iOS 9.0 + / tvOS 9.0 + / watchOS 2.0 +
  • Swift 5.0+

If your project is based on the system Combine framework, and the project environment is macOS 10.15 + / iOS 10.0 + / tvOS 10.0 + / watchOS 6.0 + , then you can choose to use [CombineAsync](https://raw.github.com/KKLater/OpenCombineAsync/main/ https://github.com/KKLater/CombineAsync ) framework. If your project uses the CombineX framework, you can choose CombineXAsync framework.

Installation

Swift Package Manager (SPM)

Swift Package Manager is a tool for managing the distribution of Swift code. It’s integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies.

Xcode 11+ is required to build OpenCombineAsync using Swift Package Manager.

To integrate OpenCombineAsync into your Xcode project using Swift Package Manager, add it to the dependencies value of your Package.swift:

dependencies: [
    .package(url: "https://github.com/KKLater/CombineXAsync.git", .upToNextMajor(from: "0.0.1"))
]

Usage

Asynchronous operations need to be wrapped as Future objects of OpenCombine.

func background1() -> Future<Int, Error> {
    return Future<Int, Error> { promise in
        let i:Int = Int(arc4random() % 10)
        DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + DispatchTimeInterval.microseconds(i)) {
            promise(.success(1))
        }
    }
}

func background2(c: Int) -> Future<Int, Error> {
    return Future<Int, Error> { promise in
        let i:Int = Int(arc4random() % 10)
        DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + DispatchTimeInterval.microseconds(i)) {
            promise(.success(c + 10))
        }
    }
}

func background3(c: Int) -> Future<Int, Error> {
    return Future<Int, Error> { promise in
        let i:Int = Int(arc4random() % 10)
        DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + DispatchTimeInterval.microseconds(i)) {
            promise(.success(c + 100))
        }
    }
}

Before use async and await api:

self.background1().sink { (error) in
    print(error)
} receiveValue: { (a) in
    self.background2(c: a).sink { (error) in
        print(error)
    } receiveValue: { (b) in
        self.background3(c: b).sink { (error) in
            print(error)
        } receiveValue: { (c) in
            print(c) // 111
        }.store(in: &self.cancels)

    }.store(in: &self.cancels)
}.store(in: &cancels)

After using the async and await APIs:

async {
    do {
        let a = try await(self.background1())
        let b = try await(self.background2(c: a))
        let c = try await(self.background3(c: b))
        main {
            print(c) // 111
        }
    } catch {
        throw error
    }
}

License

OpenCombineAsync is released under the MIT license. See LICENSE for details.

GitHub

link
Stars: 0
Last commit: 3 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

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