11extension ReducerProtocol {
2- /// Enhances a reducer with debug logging of received actions and state mutations.
3- ///
4- /// > Note: Printing is only done in `DEBUG` configurations.
5- ///
6- /// - Returns: A reducer that prints debug messages for all received actions.
7- @inlinable
8- public func _printChanges( ) -> _PrintChangesReducer < Self , _CustomDumpPrinter > {
9- _PrintChangesReducer ( base: self , printer: . customDump)
10- }
11-
122 /// Enhances a reducer with debug logging of received actions and state mutations for the given
133 /// printer.
144 ///
@@ -17,53 +7,55 @@ extension ReducerProtocol {
177 /// - Parameter printer: A printer for printing debug messages.
188 /// - Returns: A reducer that prints debug messages for all received actions.
199 @inlinable
20- public func _printChanges< Printer : _ReducerPrinter > (
21- _ printer: Printer ?
22- ) -> ReducerBuilder < State , Action > . _Conditional < _PrintChangesReducer < Self , Printer > , Self > {
23- printer . map { . first ( _PrintChangesReducer ( base: self , printer: $0 ) ) } ?? . second ( self )
10+ public func _printChanges(
11+ _ printer: _ReducerPrinter < State , Action > ? = . customDump
12+ ) -> _PrintChangesReducer < Self > {
13+ _PrintChangesReducer < Self > ( base: self , printer: printer )
2414 }
2515}
2616
27- public protocol _ReducerPrinter {
28- func printChange< Action, State> ( receivedAction: Action , oldState: State , newState: State )
29- }
17+ public struct _ReducerPrinter < State, Action> {
18+ private let _printChange : ( _ receivedAction: Action , _ oldState: State , _ newState: State ) -> Void
3019
31- extension _ReducerPrinter where Self == _CustomDumpPrinter {
32- public static var customDump : Self { Self ( ) }
33- }
20+ public init (
21+ printChange: @escaping ( _ receivedAction: Action , _ oldState: State , _ newState: State ) -> Void
22+ ) {
23+ self . _printChange = printChange
24+ }
3425
35- public struct _CustomDumpPrinter : _ReducerPrinter {
36- public func printChange< Action, State> ( receivedAction: Action , oldState: State , newState: State ) {
37- var target = " "
38- target. write ( " received action: \n " )
39- CustomDump . customDump ( receivedAction, to: & target, indent: 2 )
40- target. write ( " \n " )
41- target. write ( diff ( oldState, newState) . map { " \( $0) \n " } ?? " (No state changes) \n " )
42- print ( target)
26+ public func printChange( receivedAction: Action , oldState: State , newState: State ) {
27+ self . _printChange ( receivedAction, oldState, newState)
4328 }
4429}
4530
46- extension _ReducerPrinter where Self == _ActionLabelsPrinter {
47- public static var actionLabels : Self { Self ( ) }
48- }
31+ extension _ReducerPrinter {
32+ public static var customDump : Self {
33+ Self { receivedAction, oldState, newState in
34+ var target = " "
35+ target. write ( " received action: \n " )
36+ CustomDump . customDump ( receivedAction, to: & target, indent: 2 )
37+ target. write ( " \n " )
38+ target. write ( diff ( oldState, newState) . map { " \( $0) \n " } ?? " (No state changes) \n " )
39+ print ( target)
40+ }
41+ }
4942
50- public struct _ActionLabelsPrinter : _ReducerPrinter {
51- public func printChange< Action, State> ( receivedAction: Action , oldState: State , newState: State ) {
52- print ( " received action: \( debugCaseOutput ( receivedAction) ) " )
43+ public static var actionLabels : Self {
44+ Self { receivedAction, _, _ in
45+ print ( " received action: \( debugCaseOutput ( receivedAction) ) " )
46+ }
5347 }
5448}
5549
56- public struct _PrintChangesReducer <
57- Base: ReducerProtocol , Printer: _ReducerPrinter
58- > : ReducerProtocol {
50+ public struct _PrintChangesReducer < Base: ReducerProtocol > : ReducerProtocol {
5951 @usableFromInline
6052 let base : Base
6153
6254 @usableFromInline
63- let printer : Printer
55+ let printer : _ReducerPrinter < Base . State , Base . Action > ?
6456
6557 @usableFromInline
66- init ( base: Base , printer: Printer ) {
58+ init ( base: Base , printer: _ReducerPrinter < Base . State , Base . Action > ? ) {
6759 self . base = base
6860 self . printer = printer
6961 }
@@ -76,12 +68,12 @@ public struct _PrintChangesReducer<
7668 into state: inout Base . State , action: Base . Action
7769 ) -> Effect < Base . Action , Never > {
7870 #if DEBUG
79- if self . context != . test {
71+ if self . context != . test, let printer = self . printer {
8072 let oldState = state
8173 let effects = self . base. reduce ( into: & state, action: action)
8274 return effects. merge (
8375 with: . fireAndForget { [ newState = state] in
84- self . printer. printChange ( receivedAction: action, oldState: oldState, newState: newState)
76+ printer. printChange ( receivedAction: action, oldState: oldState, newState: newState)
8577 }
8678 )
8779 }
0 commit comments