@@ -30,6 +30,7 @@ import Combine
30
30
/// - Precondition: The `Request` body must contain **exactly one** `Url`
31
31
public typealias Request = AnyRequest < Data >
32
32
33
+ // TODO: Fix EXC_BAD_ACCESS instead of workaround with `struct`
33
34
/// Tha base class of `Request` to be used with a `Codable` `ResponseType` when using the `onObject` callback
34
35
///
35
36
/// *Example*:
@@ -40,8 +41,7 @@ public typealias Request = AnyRequest<Data>
40
41
/// .onObject { myCodableStructs in
41
42
/// ...
42
43
/// }
43
- public class AnyRequest < ResponseType> : ObservableObject , Identifiable where ResponseType: Decodable {
44
- public var willChange = PassthroughSubject < AnyRequest , Never > ( )
44
+ public struct AnyRequest < ResponseType> /*: ObservableObject, Identifiable*/ where ResponseType: Decodable {
45
45
46
46
private var params : CombinedParams
47
47
@@ -51,7 +51,7 @@ public class AnyRequest<ResponseType>: ObservableObject, Identifiable where Resp
51
51
private var onObject : ( ( ResponseType ) -> Void ) ?
52
52
private var onError : ( ( RequestError ) -> Void ) ?
53
53
54
- @Published public var response : Response = Response ( )
54
+ /* @Published*/ public var response : Response = Response ( )
55
55
56
56
public init ( @RequestBuilder builder: ( ) -> RequestParam ) {
57
57
let params = builder ( )
@@ -68,34 +68,43 @@ public class AnyRequest<ResponseType>: ObservableObject, Identifiable where Resp
68
68
self . response = Response ( )
69
69
}
70
70
71
+ internal init ( params: CombinedParams ,
72
+ onData: ( ( Data ) -> Void ) ? ,
73
+ onString: ( ( String ) -> Void ) ? ,
74
+ onJson: ( ( Json ) -> Void ) ? ,
75
+ onObject: ( ( ResponseType ) -> Void ) ? ,
76
+ onError: ( ( RequestError ) -> Void ) ? ) {
77
+ self . params = params
78
+ self . onData = onData
79
+ self . onString = onString
80
+ self . onJson = onJson
81
+ self . onObject = onObject
82
+ self . onError = onError
83
+ }
84
+
71
85
/// Sets the `onData` callback to be run whenever `Data` is retrieved
72
86
public func onData( _ callback: @escaping ( Data ) -> Void ) -> Self {
73
- self . onData = callback
74
- return self
87
+ Self . init ( params: params, onData: callback, onString: onString, onJson: onJson, onObject: onObject, onError: onError)
75
88
}
76
89
77
90
/// Sets the `onString` callback to be run whenever a `String` is retrieved
78
91
public func onString( _ callback: @escaping ( String ) -> Void ) -> Self {
79
- self . onString = callback
80
- return self
92
+ Self . init ( params: params, onData: onData, onString: callback, onJson: onJson, onObject: onObject, onError: onError)
81
93
}
82
94
83
95
/// Sets the `onData` callback to be run whenever `Json` is retrieved
84
96
public func onJson( _ callback: @escaping ( Json ) -> Void ) -> Self {
85
- self . onJson = callback
86
- return self
97
+ Self . init ( params: params, onData: onData, onString: onString, onJson: callback, onObject: onObject, onError: onError)
87
98
}
88
99
89
100
/// Sets the `onObject` callback to be run whenever `Data` is retrieved
90
101
public func onObject( _ callback: @escaping ( ResponseType ) -> Void ) -> Self {
91
- self . onObject = callback
92
- return self
102
+ Self . init ( params: params, onData: onData, onString: onString, onJson: onJson, onObject: callback, onError: onError)
93
103
}
94
104
95
105
/// Handle any `RequestError`s thrown by the `Request`
96
106
public func onError( _ callback: @escaping ( RequestError ) -> Void ) -> Self {
97
- self . onError = callback
98
- return self
107
+ Self . init ( params: params, onData: onData, onString: onString, onJson: onJson, onObject: onObject, onError: callback)
99
108
}
100
109
101
110
/// Performs the `Request`, and calls the `onData`, `onString`, `onJson`, and `onError` callbacks when appropriate.
0 commit comments