杨锴
2025-04-16 09a372bc45fde16fd42257ab6f78b8deeecf720b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//
//  PrimitiveSequence+Concurrency.swift
//  RxSwift
//
//  Created by Shai Mishali on 22/09/2021.
//  Copyright © 2021 Krunoslav Zaher. All rights reserved.
//
 
import Foundation
 
#if swift(>=5.6) && canImport(_Concurrency) && !os(Linux)
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
public extension PrimitiveSequenceType where Trait == SingleTrait {
    /**
     Creates a `Single` from the result of an asynchronous operation
 
     - seealso: [create operator on reactivex.io](http://reactivex.io/documentation/operators/create.html)
 
     - parameter work: An `async` closure expected to return an element of type `Element`
 
     - returns: A `Single` of the `async` closure's element type
     */
    @_disfavoredOverload
    static func create(
        detached: Bool = false,
        priority: TaskPriority? = nil,
        work: @Sendable @escaping () async throws -> Element
    ) -> PrimitiveSequence<Trait, Element> {
        .create { single in
            let operation: () async throws -> Void = {
                await single(
                    Result { try await work() }
                )
            }
 
            let task = if detached {
                Task.detached(priority: priority, operation: operation)
            } else {
                Task(priority: priority, operation: operation)
            }
 
            return Disposables.create { task.cancel() }
        }
    }
    
    /// Allows awaiting the success or failure of this `Single`
    /// asynchronously via Swift's concurrency features (`async/await`)
    ///
    /// A sample usage would look like so:
    ///
    /// ```swift
    /// do {
    ///     let value = try await single.value
    /// } catch {
    ///     // Handle error
    /// }
    /// ```
    var value: Element {
        get async throws {
            let disposable = SingleAssignmentDisposable()
            return try await withTaskCancellationHandler(
                operation: {
                    try await withCheckedThrowingContinuation { continuation in
                        var didResume = false
                        disposable.setDisposable(
                            self.subscribe(
                                onSuccess: {
                                    didResume = true
                                    continuation.resume(returning: $0)
                                },
                                onFailure: {
                                    didResume = true
                                    continuation.resume(throwing: $0)
                                },
                                onDisposed: {
                                    guard !didResume else { return }
                                    continuation.resume(throwing: CancellationError())
                                }
                            )
                        )
                    }
                },
                onCancel: { [disposable] in
                    disposable.dispose()
                }
            )
        }
    }
}
 
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
public extension PrimitiveSequenceType where Trait == MaybeTrait {
    /// Allows awaiting the success or failure of this `Maybe`
    /// asynchronously via Swift's concurrency features (`async/await`)
    ///
    /// If the `Maybe` completes without emitting a value, it would return
    /// a `nil` value to indicate so.
    ///
    /// A sample usage would look like so:
    ///
    /// ```swift
    /// do {
    ///     let value = try await maybe.value // Element?
    /// } catch {
    ///     // Handle error
    /// }
    /// ```
    var value: Element? {
        get async throws {
            let disposable = SingleAssignmentDisposable()
            return try await withTaskCancellationHandler(
                operation: {
                    try await withCheckedThrowingContinuation { continuation in
                        var didEmit = false
                        var didResume = false
                        disposable.setDisposable(
                            self.subscribe(
                                onSuccess: { value in
                                    didEmit = true
                                    didResume = true
                                    continuation.resume(returning: value)
                                },
                                onError: { error in
                                    didResume = true
                                    continuation.resume(throwing: error)
                                },
                                onCompleted: {
                                    guard !didEmit else { return }
                                    didResume = true
                                    continuation.resume(returning: nil)
                                },
                                onDisposed: {
                                    guard !didResume else { return }
                                    continuation.resume(throwing: CancellationError())
                                }
                            )
                        )
                    }
                },
                onCancel: { [disposable] in
                    disposable.dispose()
                }
            )
        }
    }
}
 
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
public extension PrimitiveSequenceType where Trait == CompletableTrait, Element == Never {
    /// Allows awaiting the success or failure of this `Completable`
    /// asynchronously via Swift's concurrency features (`async/await`)
    ///
    /// Upon completion, a `Void` will be returned
    ///
    /// A sample usage would look like so:
    ///
    /// ```swift
    /// do {
    ///     let value = try await completable.value // Void
    /// } catch {
    ///     // Handle error
    /// }
    /// ```
    var value: Void {
        get async throws {
            let disposable = SingleAssignmentDisposable()
            return try await withTaskCancellationHandler(
                operation: {
                    try await withCheckedThrowingContinuation { continuation in
                        var didResume = false
                        disposable.setDisposable(
                            self.subscribe(
                                onCompleted: {
                                    didResume = true
                                    continuation.resume()
                                },
                                onError: { error in
                                    didResume = true
                                    continuation.resume(throwing: error)
                                },
                                onDisposed: {
                                    guard !didResume else { return }
                                    continuation.resume(throwing: CancellationError())
                                }
                            )
                        )
                    }
                },
                onCancel: { [disposable] in
                    disposable.dispose()
                }
            )
        }
    }
}
 
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
extension Result where Failure == Swift.Error {
    @_disfavoredOverload
    init(catching body: () async throws -> Success) async {
        do {
            self = try await .success(body())
        } catch {
            self = .failure(error)
        }
    }
}
#endif