@@ -1651,51 +1651,6 @@ extension SignalProtocol {
16511651 }
16521652 }
16531653
1654- /// Apply `operation` to values from `self` with `success`ful results
1655- /// forwarded on the returned signal and `failure`s sent as failed events.
1656- ///
1657- /// - parameters:
1658- /// - operation: A closure that accepts a value and returns a `Result`.
1659- ///
1660- /// - returns: A signal that receives `success`ful `Result` as `value` event
1661- /// and `failure` as failed event.
1662- public func attempt( _ operation: @escaping ( Value ) -> Result < ( ) , Error > ) -> Signal < Value , Error > {
1663- return attemptMap { value in
1664- return operation ( value) . map {
1665- return value
1666- }
1667- }
1668- }
1669-
1670- /// Apply `operation` to values from `self` with `success`ful results mapped
1671- /// on the returned signal and `failure`s sent as failed events.
1672- ///
1673- /// - parameters:
1674- /// - operation: A closure that accepts a value and returns a result of
1675- /// a mapped value as `success`.
1676- ///
1677- /// - returns: A signal that sends mapped values from `self` if returned
1678- /// `Result` is `success`ful, `failed` events otherwise.
1679- public func attemptMap< U> ( _ operation: @escaping ( Value ) -> Result < U , Error > ) -> Signal < U , Error > {
1680- return Signal { observer in
1681- self . observe { event in
1682- switch event {
1683- case let . value( value) :
1684- operation ( value) . analysis (
1685- ifSuccess: observer. send ( value: ) ,
1686- ifFailure: observer. send ( error: )
1687- )
1688- case let . failed( error) :
1689- observer. send ( error: error)
1690- case . completed:
1691- observer. sendCompleted ( )
1692- case . interrupted:
1693- observer. sendInterrupted ( )
1694- }
1695- }
1696- }
1697- }
1698-
16991654 /// Throttle values sent by the receiver, so that at least `interval`
17001655 /// seconds pass between each, then forwards them on the given scheduler.
17011656 ///
@@ -2230,3 +2185,119 @@ extension SignalProtocol where Error == NoError {
22302185 . timeout ( after: interval, raising: error, on: scheduler)
22312186 }
22322187}
2188+
2189+ extension SignalProtocol {
2190+ /// Apply `operation` to values from `self` with `success`ful results
2191+ /// forwarded on the returned signal and `failure`s sent as failed events.
2192+ ///
2193+ /// - parameters:
2194+ /// - operation: A closure that accepts a value and returns a `Result`.
2195+ ///
2196+ /// - returns: A signal that receives `success`ful `Result` as `value` event
2197+ /// and `failure` as failed event.
2198+ public func attempt( _ operation: @escaping ( Value ) -> Result < ( ) , Error > ) -> Signal < Value , Error > {
2199+ return attemptMap { value in
2200+ return operation ( value) . map {
2201+ return value
2202+ }
2203+ }
2204+ }
2205+
2206+ /// Apply `operation` to values from `self` with `success`ful results mapped
2207+ /// on the returned signal and `failure`s sent as failed events.
2208+ ///
2209+ /// - parameters:
2210+ /// - operation: A closure that accepts a value and returns a result of
2211+ /// a mapped value as `success`.
2212+ ///
2213+ /// - returns: A signal that sends mapped values from `self` if returned
2214+ /// `Result` is `success`ful, `failed` events otherwise.
2215+ public func attemptMap< U> ( _ operation: @escaping ( Value ) -> Result < U , Error > ) -> Signal < U , Error > {
2216+ return Signal { observer in
2217+ self . observe { event in
2218+ switch event {
2219+ case let . value( value) :
2220+ operation ( value) . analysis (
2221+ ifSuccess: observer. send ( value: ) ,
2222+ ifFailure: observer. send ( error: )
2223+ )
2224+ case let . failed( error) :
2225+ observer. send ( error: error)
2226+ case . completed:
2227+ observer. sendCompleted ( )
2228+ case . interrupted:
2229+ observer. sendInterrupted ( )
2230+ }
2231+ }
2232+ }
2233+ }
2234+ }
2235+
2236+ extension SignalProtocol where Error == NoError {
2237+ /// Apply a failable `operation` to values from `self` with successful
2238+ /// results forwarded on the returned signal and thrown errors sent as
2239+ /// failed events.
2240+ ///
2241+ /// - parameters:
2242+ /// - operation: A failable closure that accepts a value.
2243+ ///
2244+ /// - returns: A signal that forwards successes as `value` events and thrown
2245+ /// errors as `failed` events.
2246+ public func attempt( _ operation: @escaping ( Value ) throws -> Void ) -> Signal < Value , AnyError > {
2247+ return self
2248+ . promoteErrors ( AnyError . self)
2249+ . attempt ( operation)
2250+ }
2251+
2252+ /// Apply a failable `operation` to values from `self` with successful
2253+ /// results mapped on the returned signal and thrown errors sent as
2254+ /// failed events.
2255+ ///
2256+ /// - parameters:
2257+ /// - operation: A failable closure that accepts a value and attempts to
2258+ /// transform it.
2259+ ///
2260+ /// - returns: A signal that sends successfully mapped values from `self`, or
2261+ /// thrown errors as `failed` events.
2262+ public func attemptMap< U> ( _ operation: @escaping ( Value ) throws -> U ) -> Signal < U , AnyError > {
2263+ return self
2264+ . promoteErrors ( AnyError . self)
2265+ . attemptMap ( operation)
2266+ }
2267+ }
2268+
2269+ extension SignalProtocol where Error == AnyError {
2270+ /// Apply a failable `operation` to values from `self` with successful
2271+ /// results forwarded on the returned signal and thrown errors sent as
2272+ /// failed events.
2273+ ///
2274+ /// - parameters:
2275+ /// - operation: A failable closure that accepts a value.
2276+ ///
2277+ /// - returns: A signal that forwards successes as `value` events and thrown
2278+ /// errors as `failed` events.
2279+ public func attempt( _ operation: @escaping ( Value ) throws -> Void ) -> Signal < Value , AnyError > {
2280+ return attemptMap { value in
2281+ try operation ( value)
2282+ return value
2283+ }
2284+ }
2285+
2286+ /// Apply a failable `operation` to values from `self` with successful
2287+ /// results mapped on the returned signal and thrown errors sent as
2288+ /// failed events.
2289+ ///
2290+ /// - parameters:
2291+ /// - operation: A failable closure that accepts a value and attempts to
2292+ /// transform it.
2293+ ///
2294+ /// - returns: A signal that sends successfully mapped values from `self`, or
2295+ /// thrown errors as `failed` events.
2296+ public func attemptMap< U> ( _ operation: @escaping ( Value ) throws -> U ) -> Signal < U , AnyError > {
2297+ return attemptMap { value in
2298+ ReactiveSwift . materialize {
2299+ try operation ( value)
2300+ }
2301+ }
2302+ }
2303+ }
0 commit comments