Ryu0118/swift-composable-architecture-extras

Library to make swift-composable-architecture more useful

22

stars

13

commits

Swift

primary language

Nov 15, 2023

updated

README

Composable Architecture Extras

Language:Swift License:MIT Latest Release Twitter

A companion library to Point-Free's swift-composable-architecture.

TaskResult And VoidSuccess

In the current TCA, TaskResult was not Equatable compliant, so it must be written like this

public struct HogeFeature: Reducer {
  public struct State: Equatable {}
  public enum Action: Equatable {
    case onAppear
    case response(TaskResult<EquatableVoid>)
  }

  public func reduce(into state: inout State, action: Action) -> Effect<Action> {
    switch action {
    case .onAppear:
      return .run { send in
        await send(
          .response(
            TaskResult {
              try await asyncThrowsVoidFunc()
              return VoidSuccess()
            }
          )
        )
      }
    case .response(.success):
      // do something
      return .none

    case .response(.failure(let error)):
      // handle error
      return .none
    }
  }
}

it is not kind to create an VoidSuccess on user just to conform TaskResult to Equatable even though Void is already provided in Swift. So we extended TaskResult so that it could be written like this.

public struct HogeFeature: Reducer {
  public struct State: Equatable {}
  public enum Action: Equatable {
    case onAppear
    case response(TaskResult<VoidSuccess>)
  }

  public func reduce(into state: inout State, action: Action) -> Effect<Action> {
    switch action {
    case .onAppear:
      return .run { send in
        await send(
          .response(
            TaskResult {
              try await asyncThrowsVoidFunc()
            }
          )
        )
      }
    case .response(.success):
      // do something
      return .none

    case .response(.failure(let error)):
      // handle error
      return .none
    }
  }
}

Installation

In the dependencies section, add:

.package(url: "https://github.com/Ryu0118/swift-composable-architecture-extras", from: "1.0.0")

In each module, add:

.target(
  name: "MyModule",
  dependencies: [
    .product(name: "ComposableArchitectureExtras", package: "swift-composable-architecture-extras")
  ]
),

Contributors

Ryu0118

13 commits

Ryu0118/swift-composable-architecture-extras

Library to make swift-composable-architecture more useful

22

stars

13

commits

Swift

primary language

Nov 15, 2023

updated

README

Composable Architecture Extras

Language:Swift License:MIT Latest Release Twitter

A companion library to Point-Free's swift-composable-architecture.

TaskResult And VoidSuccess

In the current TCA, TaskResult was not Equatable compliant, so it must be written like this

public struct HogeFeature: Reducer {
  public struct State: Equatable {}
  public enum Action: Equatable {
    case onAppear
    case response(TaskResult<EquatableVoid>)
  }

  public func reduce(into state: inout State, action: Action) -> Effect<Action> {
    switch action {
    case .onAppear:
      return .run { send in
        await send(
          .response(
            TaskResult {
              try await asyncThrowsVoidFunc()
              return VoidSuccess()
            }
          )
        )
      }
    case .response(.success):
      // do something
      return .none

    case .response(.failure(let error)):
      // handle error
      return .none
    }
  }
}

it is not kind to create an VoidSuccess on user just to conform TaskResult to Equatable even though Void is already provided in Swift. So we extended TaskResult so that it could be written like this.

public struct HogeFeature: Reducer {
  public struct State: Equatable {}
  public enum Action: Equatable {
    case onAppear
    case response(TaskResult<VoidSuccess>)
  }

  public func reduce(into state: inout State, action: Action) -> Effect<Action> {
    switch action {
    case .onAppear:
      return .run { send in
        await send(
          .response(
            TaskResult {
              try await asyncThrowsVoidFunc()
            }
          )
        )
      }
    case .response(.success):
      // do something
      return .none

    case .response(.failure(let error)):
      // handle error
      return .none
    }
  }
}

Installation

In the dependencies section, add:

.package(url: "https://github.com/Ryu0118/swift-composable-architecture-extras", from: "1.0.0")

In each module, add:

.target(
  name: "MyModule",
  dependencies: [
    .product(name: "ComposableArchitectureExtras", package: "swift-composable-architecture-extras")
  ]
),

Contributors

Ryu0118

13 commits

Languages

Swift

100.0%