tca

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

The Composable Architecture (TCA) Guidelines

The Composable Architecture (TCA) 指南

You are an expert in Point-Free's Composable Architecture. When writing or refactoring TCA code, adhere to these rules:
您是Point-Free的Composable Architecture专家。在编写或重构TCA代码时,请遵循以下规则:

1. Reducer Structure

1. Reducer 结构

  • ALWAYS use the
    @Reducer
    macro.
  • State and Action must be nested types within the Reducer struct.
  • Use
    CasePaths
    logic implicitly via the
    Bindable
    and
    Pulsar
    patterns where applicable (though
    CasePath
    is rarely manual now).
  • Actions should be named
    delegate
    ,
    view
    , and
    internal
    to separate concerns:
    • case view(ViewAction)
      : User interactions.
    • case delegate(DelegateAction)
      : Communication with parent reducers.
    • case internal(InternalAction)
      : Side-effect results.
  • 始终使用
    @Reducer
    宏。
  • State和Action必须是Reducer结构体中的嵌套类型。
  • 在适用的情况下,通过
    Bindable
    Pulsar
    模式隐式使用
    CasePaths
    逻辑(不过现在很少需要手动处理
    CasePath
    )。
  • Action应命名为
    delegate
    view
    internal
    以分离关注点:
    • case view(ViewAction)
      :用户交互。
    • case delegate(DelegateAction)
      :与父reducer通信。
    • case internal(InternalAction)
      :副作用结果。

2. Dependencies

2. 依赖项

  • Use the
    @Dependency(\.clientName)
    pattern.
  • NEVER reach out to global singletons.
  • Always define a
    testValue
    and
    previewValue
    for every custom dependency.
  • 使用
    @Dependency(\.clientName)
    模式。
  • 绝对不要直接调用全局单例。
  • 始终为每个自定义依赖项定义
    testValue
    previewValue

3. Effects & Concurrency

3. 副作用与并发

  • Use
    .run { send in ... }
    for asynchronous side effects.
  • Avoid
    Effect.task
    unless strictly necessary for simple bridging.
  • Ensure all loops in effects handle cancellation properly via
    .cancellable(id:)
    .
  • 异步副作用使用
    .run { send in ... }
  • 除非是为了简单桥接的严格必要情况,否则避免使用
    Effect.task
  • 确保副作用中的所有循环都通过
    .cancellable(id:)
    正确处理取消。

4. Testing

4. 测试

  • Use
    TestStore
    for all logic verification.
  • Enforce exhaustivity:
    store.exhaustivity = .on
    .
  • Mock all dependencies in tests using
    withDependencies
    .
  • 使用
    TestStore
    验证所有逻辑。
  • 强制要求穷尽性:
    store.exhaustivity = .on
  • 在测试中使用
    withDependencies
    模拟所有依赖项。

Example

示例

swift
@Reducer
struct Feature {
    @ObservableState
    struct State: Equatable {
        var count = 0
    }
    enum Action {
        case view(ViewAction)
        case internal(InternalAction)

        enum ViewAction {
            case incrementButtonTapped
        }
        enum InternalAction {
            case loadResponse(Int)
        }
    }

    var body: some ReducerOf<Self> {
        Reduce { state, action in
            switch action {
            case .view(.incrementButtonTapped):
                state.count += 1
                return .none
            }
        }
    }
}
swift
@Reducer
struct Feature {
    @ObservableState
    struct State: Equatable {
        var count = 0
    }
    enum Action {
        case view(ViewAction)
        case internal(InternalAction)

        enum ViewAction {
            case incrementButtonTapped
        }
        enum InternalAction {
            case loadResponse(Int)
        }
    }

    var body: some ReducerOf<Self> {
        Reduce { state, action in
            switch action {
            case .view(.incrementButtonTapped):
                state.count += 1
                return .none
            }
        }
    }
}