/// A result builder for combining reducers into a single reducer by running each, one after the /// other, and merging their effects. /// /// It is most common to encounter a reducer builder context when conforming a type to ``Reducer`` /// and implementing its ``Reducer/body-swift.property`` property. /// /// See ``CombineReducers`` for an entry point into a reducer builder context. @resultBuilder public enum ReducerBuilder { @inlinable public static func buildArray( _ reducers: [some Reducer] ) -> some Reducer { _SequenceMany(reducers: reducers) } @inlinable public static func buildBlock() -> some Reducer { EmptyReducer() } @inlinable public static func buildBlock>(_ reducer: R) -> R { reducer } @inlinable public static func buildEither, R1: Reducer>( first reducer: R0 ) -> _Conditional { .first(reducer) } @inlinable public static func buildEither, R1: Reducer>( second reducer: R1 ) -> _Conditional { .second(reducer) } @inlinable public static func buildExpression>(_ expression: R) -> R { expression } @inlinable @_disfavoredOverload public static func buildExpression( _ expression: any Reducer ) -> Reduce { Reduce(expression) } @inlinable public static func buildFinalResult>(_ reducer: R) -> R { reducer } @inlinable public static func buildLimitedAvailability( _ wrapped: some Reducer ) -> Reduce { Reduce(wrapped) } @inlinable public static func buildOptional>(_ wrapped: R?) -> R? { wrapped } @inlinable public static func buildPartialBlock>(first: R) -> R { first } @inlinable public static func buildPartialBlock, R1: Reducer>( accumulated: R0, next: R1 ) -> _Sequence { _Sequence(accumulated, next) } public enum _Conditional>: Reducer { case first(First) case second(Second) @inlinable public func reduce(into state: inout First.State, action: First.Action) -> Effect< First.Action > { switch self { case let .first(first): return first.reduce(into: &state, action: action) case let .second(second): return second.reduce(into: &state, action: action) } } } public struct _Sequence>: Reducer { @usableFromInline let r0: R0 @usableFromInline let r1: R1 @usableFromInline init(_ r0: R0, _ r1: R1) { self.r0 = r0 self.r1 = r1 } @inlinable public func reduce(into state: inout R0.State, action: R0.Action) -> Effect { self.r0.reduce(into: &state, action: action) .merge(with: self.r1.reduce(into: &state, action: action)) } } public struct _SequenceMany: Reducer { @usableFromInline let reducers: [Element] @usableFromInline init(reducers: [Element]) { self.reducers = reducers } @inlinable public func reduce( into state: inout Element.State, action: Element.Action ) -> Effect { self.reducers.reduce(.none) { $0.merge(with: $1.reduce(into: &state, action: action)) } } } }