@@ -87,14 +87,14 @@ extension Effect {
87
87
/// Note that you can only deliver a single value to the `callback`. If you send more they will be
88
88
/// discarded:
89
89
///
90
- /// ```swift
90
+ /// ```swift
91
91
/// Effect<Int, Never>.future { callback in
92
92
/// DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
93
93
/// callback(.success(42))
94
94
/// callback(.success(1729)) // Will not be emitted by the effect
95
95
/// }
96
- /// }
97
- /// ```
96
+ /// }
97
+ /// ```
98
98
///
99
99
/// - Parameter attemptToFulfill: A closure that takes a `callback` as an argument which can be
100
100
/// used to feed it `Result<Output, Failure>` values.
@@ -114,36 +114,63 @@ extension Effect {
114
114
}
115
115
}
116
116
117
- /// Turns any publisher into an ``Effect`` that cannot fail by wrapping its output and failure in
117
+ /// Turns any `SignalProducer` into an ``Effect`` that cannot fail by wrapping its output and failure in
118
118
/// a result.
119
119
///
120
120
/// This can be useful when you are working with a failing API but want to deliver its data to an
121
121
/// action that handles both success and failure.
122
122
///
123
- /// ```swift
124
- /// case .buttonTapped:
125
- /// return fetchUser(id: 1)
126
- /// .catchToEffect()
127
- /// .map(ProfileAction.userResponse)
128
- /// ```
123
+ /// ```swift
124
+ /// case .buttonTapped:
125
+ /// return fetchUser(id: 1)
126
+ /// .catchToEffect()
127
+ /// .map(ProfileAction.userResponse)
128
+ /// ```
129
129
///
130
130
/// - Returns: An effect that wraps `self`.
131
131
public func catchToEffect( ) -> Effect < Result < Value , Error > , Never > {
132
132
self . map ( Result< Value, Error> . success)
133
133
. flatMapError { Effect < Result < Value , Error > , Never > ( value: Result . failure ( $0) ) }
134
134
}
135
135
136
- /// Turns any `SignalProducer` into an ``Effect`` for any output and failure type by ignoring all output
136
+ /// Turns any `SignalProducer` into an ``Effect`` that cannot fail by wrapping its output and failure into
137
+ /// result and then applying passed in function to it.
138
+ ///
139
+ /// This is a convenience operator for writing `catchToEffect()` followed by a `map()` .
140
+ ///
141
+ /// ```swift
142
+ /// case .buttonTapped:
143
+ /// return fetchUser(id: 1)
144
+ /// .catchToEffect {
145
+ /// switch $0 {
146
+ /// case let .success(response):
147
+ /// return ProfileAction.updatedUser(response)
148
+ /// case let .failure(error):
149
+ /// return ProfileAction.failedUserUpdate(error)
150
+ /// }
151
+ /// }
152
+ /// ```
153
+ ///
154
+ /// - Parameters:
155
+ /// - f: A mapping function that converts `Result<Output,Failure>` to another type.
156
+ /// - Returns: An effect that wraps `self`.
157
+ public func catchToEffect< T> ( _ f: @escaping ( Result < Value , Error > ) -> T ) -> Effect < T , Never > {
158
+ self
159
+ . catchToEffect ( )
160
+ . map ( f)
161
+ }
162
+
163
+ /// Turns any publisher into an ``Effect`` for any output and failure type by ignoring all output
137
164
/// and any failure.
138
165
///
139
166
/// This is useful for times you want to fire off an effect but don't want to feed any data back
140
167
/// into the system. It can automatically promote an effect to your reducer's domain.
141
168
///
142
- /// ```swift
143
- /// case .buttonTapped:
144
- /// return analyticsClient.track("Button Tapped")
145
- /// .fireAndForget()
146
- /// ```
169
+ /// ```swift
170
+ /// case .buttonTapped:
171
+ /// return analyticsClient.track("Button Tapped")
172
+ /// .fireAndForget()
173
+ /// ```
147
174
///
148
175
/// - Parameters:
149
176
/// - outputType: An output type.
0 commit comments