11import Foundation
2+ #if !COCOAPODS
3+ import ApolloCore
4+ #endif
25
36public class RequestChain : Cancellable {
47
@@ -9,21 +12,26 @@ public class RequestChain: Cancellable {
912
1013 private let interceptors : [ ApolloInterceptor ]
1114 private var currentIndex : Int
12- public private( set) var isCancelled : Bool = false
15+ public private( set) var callbackQueue : DispatchQueue
16+ public private( set) var isCancelled = Atomic < Bool > ( false )
1317
1418 /// Helper var for readability in guard statements
1519 public var isNotCancelled : Bool {
16- !self . isCancelled
20+ !self . isCancelled. value
1721 }
1822
1923 /// Something which allows additional error handling to occur when some kind of error has happened.
2024 public var additionalErrorHandler : ApolloErrorInterceptor ?
2125
2226 /// Creates a chain with the given interceptor array.
2327 ///
24- /// - Parameter interceptors: The interceptors to use.
25- public init ( interceptors: [ ApolloInterceptor ] ) {
28+ /// - Parameters:
29+ /// - interceptors: The array of interceptors to use.
30+ /// - callbackQueue: The `DispatchQueue` to call back on when an error or result occurs. Defauls to `.main`.
31+ public init ( interceptors: [ ApolloInterceptor ] ,
32+ callbackQueue: DispatchQueue = . main) {
2633 self . interceptors = interceptors
34+ self . callbackQueue = callbackQueue
2735 self . currentIndex = 0
2836 }
2937
@@ -86,7 +94,7 @@ public class RequestChain: Cancellable {
8694
8795 /// Cancels the entire chain of interceptors.
8896 public func cancel( ) {
89- self . isCancelled = true
97+ self . isCancelled. value = true
9098
9199 // If an interceptor adheres to `Cancellable`, it should have its in-flight work cancelled as well.
92100 for interceptor in self . interceptors {
@@ -132,7 +140,9 @@ public class RequestChain: Cancellable {
132140 }
133141
134142 guard let additionalHandler = self . additionalErrorHandler else {
135- completion ( . failure( error) )
143+ self . callbackQueue. async {
144+ completion ( . failure( error) )
145+ }
136146 return
137147 }
138148
@@ -143,4 +153,17 @@ public class RequestChain: Cancellable {
143153 response: response,
144154 completion: completion)
145155 }
156+
157+ public func returnValueAsync< ParsedValue: Parseable > (
158+ value: ParsedValue ,
159+ completion: @escaping ( Result < ParsedValue , Error > ) -> Void ) {
160+
161+ guard self . isNotCancelled else {
162+ return
163+ }
164+
165+ self . callbackQueue. async {
166+ completion ( . success( value) )
167+ }
168+ }
146169}
0 commit comments