Skip to content

debounce ignores animation parameter of send #3586

Open
@nkristek

Description

@nkristek

Description

When adding debounce to an effect any actions that are emitted from that effect are not animated even though the animation parameter is set when calling send.

Checklist

  • I have determined whether this bug is also reproducible in a vanilla SwiftUI project.
  • If possible, I've reproduced the issue using the main branch of this package.
  • This issue hasn't been addressed in an existing GitHub issue or discussion.

Expected behavior

In the sample code, all buttons should animate the square.

Actual behavior

Only the first and last buttons animate the square.

Reproducing project

import ComposableArchitecture
import SwiftUI

@Reducer
struct DebounceAnimationBugFeature {
    @ObservableState
    struct State {
        var toggle = false
    }

    enum Action {
        case toggleWithoutDebounceTapped
        case toggleWithDebounceTapped
        case toggleWithDebounceAndAnimationModifierTapped
        case toggleState
    }

    private enum TaskId {
        case debounce
    }

    @Dependency(\.mainRunLoop) private var mainRunLoop

    var body: some ReducerOf<Self> {
        Reduce<State, Action> { state, action in
            switch action {
            case .toggleWithoutDebounceTapped:
                return .run { send in
                    await send(.toggleState, animation: .default)
                }
            case .toggleWithDebounceTapped:
                return .run { send in
                    await send(.toggleState, animation: .default)
                }
                .debounce(id: TaskId.debounce, for: 0.1, scheduler: mainRunLoop)
            case .toggleWithDebounceAndAnimationModifierTapped:
                return .run { send in
                    await send(.toggleState)
                }
                .debounce(id: TaskId.debounce, for: 0.1, scheduler: mainRunLoop)
                .animation(.default)
            case .toggleState:
                state.toggle.toggle()
                return .none
            }
        }
    }
}

struct DebounceAnimationBugView: View {
    @Bindable var store: StoreOf<DebounceAnimationBugFeature>

    var body: some View {
        VStack {
            Button("Toggle without debounce") {
                store.send(.toggleWithoutDebounceTapped)
            }

            Button("Toggle with debounce") {
                store.send(.toggleWithDebounceTapped)
            }

            Button("Toggle with debounce and animation modifier") {
                store.send(.toggleWithDebounceAndAnimationModifierTapped)
            }

            Rectangle()
                .fill(Color.blue)
                .frame(width: 50, height: 50)
                .frame(maxWidth: .infinity, alignment: store.toggle ? .leading : .trailing)
        }
    }
}

#Preview {
    DebounceAnimationBugView(store: Store(initialState: .init(), reducer: {
        DebounceAnimationBugFeature()
    }))
}

The Composable Architecture version information

1.17.1

Destination operating system

iOS 18.2

Xcode version information

16.2

Swift Compiler version information

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working due to a bug in the library.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions