1
1
extension 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
-
12
2
/// Enhances a reducer with debug logging of received actions and state mutations for the given
13
3
/// printer.
14
4
///
@@ -17,53 +7,55 @@ extension ReducerProtocol {
17
7
/// - Parameter printer: A printer for printing debug messages.
18
8
/// - Returns: A reducer that prints debug messages for all received actions.
19
9
@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 )
24
14
}
25
15
}
26
16
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
30
19
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
+ }
34
25
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)
43
28
}
44
29
}
45
30
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
+ }
49
42
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
+ }
53
47
}
54
48
}
55
49
56
- public struct _PrintChangesReducer <
57
- Base: ReducerProtocol , Printer: _ReducerPrinter
58
- > : ReducerProtocol {
50
+ public struct _PrintChangesReducer < Base: ReducerProtocol > : ReducerProtocol {
59
51
@usableFromInline
60
52
let base : Base
61
53
62
54
@usableFromInline
63
- let printer : Printer
55
+ let printer : _ReducerPrinter < Base . State , Base . Action > ?
64
56
65
57
@usableFromInline
66
- init ( base: Base , printer: Printer ) {
58
+ init ( base: Base , printer: _ReducerPrinter < Base . State , Base . Action > ? ) {
67
59
self . base = base
68
60
self . printer = printer
69
61
}
@@ -76,12 +68,12 @@ public struct _PrintChangesReducer<
76
68
into state: inout Base . State , action: Base . Action
77
69
) -> Effect < Base . Action , Never > {
78
70
#if DEBUG
79
- if self . context != . test {
71
+ if self . context != . test, let printer = self . printer {
80
72
let oldState = state
81
73
let effects = self . base. reduce ( into: & state, action: action)
82
74
return effects. merge (
83
75
with: . fireAndForget { [ newState = state] in
84
- self . printer. printChange ( receivedAction: action, oldState: oldState, newState: newState)
76
+ printer. printChange ( receivedAction: action, oldState: oldState, newState: newState)
85
77
}
86
78
)
87
79
}
0 commit comments