Skip to content

Commit 65b9f7b

Browse files
authored
Catch to effect updates (#707)
* Update catchToEffect docs. * clean up
1 parent e4a480c commit 65b9f7b

File tree

2 files changed

+14
-18
lines changed

2 files changed

+14
-18
lines changed

Sources/ComposableArchitecture/Effect.swift

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ extension Publisher {
328328
///
329329
/// ```swift
330330
/// case .buttonTapped:
331-
/// return fetchUser(id: 1)
331+
/// return environment.fetchUser(id: 1)
332332
/// .catchToEffect()
333333
/// .map(ProfileAction.userResponse)
334334
/// ```
@@ -340,31 +340,27 @@ extension Publisher {
340340
.eraseToEffect()
341341
}
342342

343-
/// Turns any publisher into an ``Effect`` that cannot fail by wrapping its output and failure into
344-
/// result and then applying passed in function to it.
343+
/// Turns any publisher into an ``Effect`` that cannot fail by wrapping its output and failure
344+
/// into a result and then applying passed in function to it.
345345
///
346-
/// This is a convenience operator for writing `catchToEffect()` followed by a `map()` .
346+
/// This is a convenience operator for writing ``Effect/catchToEffect()`` followed by a
347+
/// ``Effect/map(_:)-28ghh``.
347348
///
348349
/// ```swift
349350
/// case .buttonTapped:
350-
/// return fetchUser(id: 1)
351-
/// .catchToEffect {
352-
/// switch $0 {
353-
/// case let .success(response):
354-
/// return ProfileAction.updatedUser(response)
355-
/// case let .failure(error):
356-
/// return ProfileAction.failedUserUpdate(error)
357-
/// }
358-
/// }
351+
/// return environment.fetchUser(id: 1)
352+
/// .catchToEffect(ProfileAction.userResponse)
359353
/// ```
360354
///
361355
/// - Parameters:
362-
/// - f: A mapping function that converts `Result<Output,Failure>` to another type.
356+
/// - transform: A mapping function that converts `Result<Output,Failure>` to another type.
363357
/// - Returns: An effect that wraps `self`.
364-
public func catchToEffect<T>(_ f: @escaping (Result<Output, Failure>) -> T) -> Effect<T, Never> {
358+
public func catchToEffect<T>(
359+
_ transform: @escaping (Result<Output,Failure>) -> T
360+
) -> Effect<T, Never> {
365361
self
366-
.catchToEffect()
367-
.map(f)
362+
.map { transform(.success($0)) }
363+
.catch { Just(transform(.failure($0))) }
368364
.eraseToEffect()
369365
}
370366

Tests/ComposableArchitectureTests/EffectTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ final class EffectTests: XCTestCase {
77
var cancellables: Set<AnyCancellable> = []
88
let scheduler = DispatchQueue.test
99

10-
func testEraseToEffectWithError() {
10+
func testCatchToEffect() {
1111
struct Error: Swift.Error, Equatable {}
1212

1313
Future<Int, Error> { $0(.success(42)) }

0 commit comments

Comments
 (0)