Skip to content

Commit 06a2d53

Browse files
committed
Group attempt(_:) and attemptMap(_:) operators
1 parent 97c1cc1 commit 06a2d53

File tree

2 files changed

+75
-69
lines changed

2 files changed

+75
-69
lines changed

Sources/Signal.swift

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -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
///
@@ -2229,7 +2184,56 @@ extension SignalProtocol where Error == NoError {
22292184
.promoteErrors(NewError.self)
22302185
.timeout(after: interval, raising: error, on: scheduler)
22312186
}
2187+
}
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+
}
22322205

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 {
22332237
/// Apply a failable `operation` to values from `self` with successful
22342238
/// results forwarded on the returned signal and thrown errors sent as
22352239
/// failed events.

Sources/SignalProducer.swift

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -138,30 +138,6 @@ public struct SignalProducer<Value, Error: Swift.Error> {
138138
return self.init { _ in return }
139139
}
140140

141-
/// Create a `SignalProducer` that will attempt the given operation once for
142-
/// each invocation of `start()`.
143-
///
144-
/// Upon success, the started signal will send the resulting value then
145-
/// complete. Upon failure, the started signal will fail with the error that
146-
/// occurred.
147-
///
148-
/// - parameters:
149-
/// - operation: A closure that returns instance of `Result`.
150-
///
151-
/// - returns: A `SignalProducer` that will forward `success`ful `result` as
152-
/// `value` event and then complete or `failed` event if `result`
153-
/// is a `failure`.
154-
public static func attempt(_ operation: @escaping () -> Result<Value, Error>) -> SignalProducer {
155-
return self.init { observer, disposable in
156-
operation().analysis(ifSuccess: { value in
157-
observer.send(value: value)
158-
observer.sendCompleted()
159-
}, ifFailure: { error in
160-
observer.send(error: error)
161-
})
162-
}
163-
}
164-
165141
/// Create a Signal from the producer, pass it into the given closure,
166142
/// then start sending events on the Signal when the closure has returned.
167143
///
@@ -1256,6 +1232,32 @@ extension SignalProducerProtocol where Error == NoError {
12561232
}
12571233
}
12581234

1235+
extension SignalProducer {
1236+
/// Create a `SignalProducer` that will attempt the given operation once for
1237+
/// each invocation of `start()`.
1238+
///
1239+
/// Upon success, the started signal will send the resulting value then
1240+
/// complete. Upon failure, the started signal will fail with the error that
1241+
/// occurred.
1242+
///
1243+
/// - parameters:
1244+
/// - operation: A closure that returns instance of `Result`.
1245+
///
1246+
/// - returns: A `SignalProducer` that will forward `success`ful `result` as
1247+
/// `value` event and then complete or `failed` event if `result`
1248+
/// is a `failure`.
1249+
public static func attempt(_ operation: @escaping () -> Result<Value, Error>) -> SignalProducer {
1250+
return self.init { observer, disposable in
1251+
operation().analysis(ifSuccess: { value in
1252+
observer.send(value: value)
1253+
observer.sendCompleted()
1254+
}, ifFailure: { error in
1255+
observer.send(error: error)
1256+
})
1257+
}
1258+
}
1259+
}
1260+
12591261
extension SignalProducerProtocol where Error == AnyError {
12601262
/// Create a `SignalProducer` that will attempt the given failable operation once for
12611263
/// each invocation of `start()`.

0 commit comments

Comments
 (0)