Skip to content

Commit

Permalink
Rename certain Effect APIs (pointfreeco#92)
Browse files Browse the repository at this point in the history
* Rename certain Effect APIs

* Move deprecations

* Fix
  • Loading branch information
stephencelis committed May 13, 2020
1 parent 940207c commit 0807f9a
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extension DownloadClient {
}
},
download: { id, url in
Effect.async { subscriber in
.run { subscriber in
let task = URLSession.shared.dataTask(with: url) { data, _, error in
switch (data, error) {
case let (.some(data), _):
Expand Down Expand Up @@ -54,7 +54,8 @@ extension DownloadClient {
dependencies[id] = nil
}
}
})
}
)
}

private struct Dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import CoreMotion
extension MotionClient {
static let live = MotionClient(
create: { id in
Effect.async { subscriber in
.run { subscriber in
let manager = MotionManager(
manager: CMMotionManager(),
handler: { motion, error in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extension SpeechClient {
}
},
recognitionTask: { id, request in
Effect.async { subscriber in
Effect.run { subscriber in
let cancellable = AnyCancellable {
dependencies[id]?.cancel()
dependencies[id] = nil
Expand Down
8 changes: 4 additions & 4 deletions Sources/ComposableArchitecture/Effect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public struct Effect<Output, Failure: Error>: Publisher {
/// sending the current status immediately, and then if the current status is `notDetermined` it
/// can request authorization, and once a status is received it can send that back to the effect:
///
/// Effect.async { subscriber in
/// Effect.run { subscriber in
/// subscriber.send(MPMediaLibrary.authorizationStatus())
///
/// guard MPMediaLibrary.authorizationStatus() == .notDetermined else {
Expand All @@ -161,7 +161,7 @@ public struct Effect<Output, Failure: Error>: Publisher {
/// - Parameter work: A closure that accepts a `Subscriber` value and returns a cancellable. When
/// the `Effect` is completed, the cancellable will be used to clean up any resources created
/// when the effect was started.
public static func async(
public static func run(
_ work: @escaping (Effect.Subscriber<Output, Failure>) -> Cancellable
) -> Self {
AnyPublisher.create(work).eraseToEffect()
Expand Down Expand Up @@ -243,7 +243,7 @@ extension Effect where Failure == Swift.Error {
///
/// For example, to load a user from some JSON on the disk, one can wrap that work in an effect:
///
/// Effect<User, Error>.sync {
/// Effect<User, Error>.catching {
/// let fileUrl = URL(
/// fileURLWithPath: NSSearchPathForDirectoriesInDomains(
/// .documentDirectory, .userDomainMask, true
Expand All @@ -257,7 +257,7 @@ extension Effect where Failure == Swift.Error {
///
/// - Parameter work: A closure encapsulating some work to execute in the real world.
/// - Returns: An effect.
public static func sync(_ work: @escaping () throws -> Output) -> Self {
public static func catching(_ work: @escaping () throws -> Output) -> Self {
.future { $0(Result { try work() }) }
}
}
Expand Down
19 changes: 19 additions & 0 deletions Sources/ComposableArchitecture/Internal/Deprecations.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Combine

// NB: Deprecated after 0.1.3:

extension Effect {
@available(*, deprecated, renamed: "run")
public static func async(
_ work: @escaping (Effect.Subscriber<Output, Failure>) -> Cancellable
) -> Self {
self.run(work)
}
}

extension Effect where Failure == Swift.Error {
@available(*, deprecated, renamed: "catching")
public static func sync(_ work: @escaping () throws -> Output) -> Self {
self.catching(work)
}
}
4 changes: 2 additions & 2 deletions Tests/ComposableArchitectureTests/EffectTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ final class EffectTests: XCTestCase {
}

func testEffectSubscriberInitializer() {
let effect = Effect<Int, Never>.async { subscriber in
let effect = Effect<Int, Never>.run { subscriber in
subscriber.send(1)
subscriber.send(2)
self.scheduler.schedule(after: self.scheduler.now.advanced(by: .seconds(1))) {
Expand Down Expand Up @@ -130,7 +130,7 @@ final class EffectTests: XCTestCase {
func testEffectSubscriberInitializer_WithCancellation() {
struct CancelId: Hashable {}

let effect = Effect<Int, Never>.async { subscriber in
let effect = Effect<Int, Never>.run { subscriber in
subscriber.send(1)
self.scheduler.schedule(after: self.scheduler.now.advanced(by: .seconds(1))) {
subscriber.send(2)
Expand Down

0 comments on commit 0807f9a

Please sign in to comment.