|
| 1 | +// |
| 2 | +// FromAsync.swift |
| 3 | +// CombineExt |
| 4 | +// |
| 5 | +// Created by Thibault Wittemberg on 2021-06-15. |
| 6 | +// Copyright © 2021 Combine Community. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#if canImport(Combine) |
| 10 | +import Combine |
| 11 | + |
| 12 | +@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *) |
| 13 | +public extension Publishers { |
| 14 | + /// Creates a Combine Publisher from an async function |
| 15 | + /// The Publisher emits a value and then completes when the async function returns its result. |
| 16 | + /// The task that supports the async function is canceled when the publisher's subscription is canceled. |
| 17 | + /// ``` |
| 18 | + /// var value: Int { |
| 19 | + /// get async { |
| 20 | + /// 3 |
| 21 | + /// } |
| 22 | + /// } |
| 23 | + /// |
| 24 | + /// Publishers |
| 25 | + /// .fromAsync { |
| 26 | + /// await value |
| 27 | + /// }.sink { |
| 28 | + /// print($0) |
| 29 | + /// } receiveValue: { |
| 30 | + /// print($0) |
| 31 | + /// } |
| 32 | + /// |
| 33 | + /// // will print: |
| 34 | + /// // 3 |
| 35 | + /// // finished |
| 36 | + /// ``` |
| 37 | + /// - parameter priority: Optional value indicating the priority of the Task supporting the execution of the async function |
| 38 | + /// - Returns: The Combine Publisher wrapping the async function execution |
| 39 | + static func fromAsync<Output>(priority: TaskPriority? = nil, |
| 40 | + _ asyncFunction: @escaping () async -> Output) -> AnyPublisher<Output, Never> { |
| 41 | + AnyPublisher<Output, Never>.create { subscriber in |
| 42 | + let task = Task(priority: priority) { |
| 43 | + let result = await asyncFunction() |
| 44 | + subscriber.send(result) |
| 45 | + subscriber.send(completion: .finished) |
| 46 | + } |
| 47 | + |
| 48 | + return AnyCancellable { |
| 49 | + task.cancel() |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /// Creates a Combine Publisher from a throwing async function |
| 55 | + /// The Publisher emits a value or fail according the the async function execution result. |
| 56 | + /// The task that supports the async function is canceled when the publisher's subscription is canceled. |
| 57 | + /// |
| 58 | + /// ``` |
| 59 | + /// var value: Int { |
| 60 | + /// get async { |
| 61 | + /// 3 |
| 62 | + /// } |
| 63 | + /// } |
| 64 | + /// |
| 65 | + /// Publishers |
| 66 | + /// .fromAsync { |
| 67 | + /// await value |
| 68 | + /// }.sink { |
| 69 | + /// print($0) |
| 70 | + /// } receiveValue: { |
| 71 | + /// print($0) |
| 72 | + /// } |
| 73 | + /// |
| 74 | + /// // will print: |
| 75 | + /// // 3 |
| 76 | + /// // finished |
| 77 | + /// ``` |
| 78 | + /// |
| 79 | + /// Whenever the async function throws an error, the stream will faile: |
| 80 | + /// |
| 81 | + /// ``` |
| 82 | + /// struct MyError: Error, CustomStringConvertible { |
| 83 | + /// var description: String { |
| 84 | + /// "Async Error" |
| 85 | + /// } |
| 86 | + /// } |
| 87 | + /// |
| 88 | + /// Publishers |
| 89 | + /// .fromAsync { () async throws -> String in |
| 90 | + /// throw MyError() |
| 91 | + /// }.sink { |
| 92 | + /// print($0) |
| 93 | + /// } receiveValue: { |
| 94 | + /// print($0) |
| 95 | + /// } |
| 96 | + /// |
| 97 | + /// // will print: |
| 98 | + /// // failure(Async Error) |
| 99 | + ///``` |
| 100 | + /// - parameter priority: Optional value indicating the priority of the Task supporting the execution of the async function |
| 101 | + /// - Returns: The Combine Publisher wrapping the async function execution |
| 102 | + static func fromThrowingAsync<Output>(priority: TaskPriority? = nil, |
| 103 | + _ asyncThrowingFunction: @escaping () async throws -> Output) -> AnyPublisher<Output, Error> { |
| 104 | + AnyPublisher<Output, Error>.create { subscriber in |
| 105 | + let task = Task(priority: priority) { |
| 106 | + do { |
| 107 | + let result = try await asyncThrowingFunction() |
| 108 | + subscriber.send(result) |
| 109 | + subscriber.send(completion: .finished) |
| 110 | + } catch { |
| 111 | + subscriber.send(completion: .failure(error)) |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + return AnyCancellable { |
| 116 | + task.cancel() |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + /// Creates a Combine Publisher from an async sequence. |
| 122 | + /// The Publisher emits values or fail according the the async sequence execution result. |
| 123 | + /// |
| 124 | + /// ``` |
| 125 | + /// let sequence = [1, 2, 3].publisher.values |
| 126 | + /// |
| 127 | + /// Publishers |
| 128 | + /// .fromAsyncSequence(sequence).sink { |
| 129 | + /// print($0) |
| 130 | + /// } receiveValue: { |
| 131 | + /// print($0) |
| 132 | + /// } |
| 133 | + /// |
| 134 | + /// // will print: |
| 135 | + /// // 1 |
| 136 | + /// // 2 |
| 137 | + /// // 3 |
| 138 | + /// // finished |
| 139 | + /// ``` |
| 140 | + /// |
| 141 | + /// If the asyncSequence faild: |
| 142 | + /// |
| 143 | + /// ``` |
| 144 | + /// struct MyError: Error, CustomStringConvertible { |
| 145 | + /// var description: String { |
| 146 | + /// "Async Error" |
| 147 | + /// } |
| 148 | + /// } |
| 149 | + /// |
| 150 | + /// let sequence = AsyncThrowingStream(Int.self) { continuation in |
| 151 | + /// continuation.yield(1) |
| 152 | + /// continuation.yield(2) |
| 153 | + /// continuation.finish(throwing: MockError(value: Int.random(in: 1...100))) |
| 154 | + /// } |
| 155 | + /// |
| 156 | + /// Publishers |
| 157 | + /// .fromAsyncSequence(sequence).sink { |
| 158 | + /// print($0) |
| 159 | + /// } receiveValue: { |
| 160 | + /// print($0) |
| 161 | + /// } |
| 162 | + /// |
| 163 | + /// // will print: |
| 164 | + /// // 1 |
| 165 | + /// // 2 |
| 166 | + /// // failure(Async Error) |
| 167 | + ///``` |
| 168 | + /// - parameter priority: Optional value indicating the priority of the Task supporting the async sequence execution |
| 169 | + /// - Returns: The Combine Publisher wrapping the async sequence iteration |
| 170 | + static func fromAsyncSequence<Output, AsyncSequenceType>(priority: TaskPriority? = nil, |
| 171 | + _ asyncSequence: AsyncSequenceType) -> AnyPublisher<Output, Error> |
| 172 | + where AsyncSequenceType: AsyncSequence, AsyncSequenceType.Element == Output { |
| 173 | + AnyPublisher<Output, Error>.create { subscriber in |
| 174 | + let task = Task(priority: priority) { |
| 175 | + do { |
| 176 | + for try await result in asyncSequence { |
| 177 | + subscriber.send(result) |
| 178 | + } |
| 179 | + subscriber.send(completion: .finished) |
| 180 | + } catch { |
| 181 | + subscriber.send(completion: .failure(error)) |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + return AnyCancellable { |
| 186 | + task.cancel() |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | +} |
| 191 | +#endif |
0 commit comments