-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSideEffectDebugging.swift
101 lines (98 loc) · 4.15 KB
/
SideEffectDebugging.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import CasePaths
import CustomDump
import Dispatch
public extension SideEffect {
/// Prints debug messages describing all received actions and state mutations.
///
/// Printing is only done in debug (`#if DEBUG`) builds.
///
/// - Parameters:
/// - prefix: A string with which to prefix all debug messages.
/// - toDebugEnvironment: A function that transforms an environment into a debug environment by
/// describing a print function and a queue to print from. Defaults to a function that ignores
/// the environment and returns a default ``DebugEnvironment`` that uses Swift's `print`
/// function and a background queue.
/// - Returns: A side-effect that prints debug messages for all received actions.
func debug(
_ prefix: String = "",
actionFormat: ActionFormat = .prettyPrint,
environment toDebugEnvironment: @escaping (Environment) -> DebugEnvironment = { _ in
DebugEnvironment()
}
) -> Self {
debug(
prefix,
action: .self,
actionFormat: actionFormat,
environment: toDebugEnvironment
)
}
/// Prints debug messages describing all received actions.
///
/// Printing is only done in debug (`#if DEBUG`) builds.
///
/// - Parameters:
/// - prefix: A string with which to prefix all debug messages.
/// - toDebugEnvironment: A function that transforms an environment into a debug environment by
/// describing a print function and a queue to print from. Defaults to a function that ignores
/// the environment and returns a default ``DebugEnvironment`` that uses Swift's `print`
/// function and a background queue.
/// - Returns: A side-effect that prints debug messages for all received actions.
func debugActions(
_ prefix: String = "",
actionFormat: ActionFormat = .prettyPrint,
environment toDebugEnvironment: @escaping (Environment) -> DebugEnvironment = { _ in
DebugEnvironment()
}
) -> Self {
debug(
prefix,
action: .self,
actionFormat: actionFormat,
environment: toDebugEnvironment
)
}
/// Prints debug messages describing all received local actions and local state mutations.
///
/// Printing is only done in debug (`#if DEBUG`) builds.
///
/// - Parameters:
/// - prefix: A string with which to prefix all debug messages.
/// - toLocalAction: A case path that filters actions that are printed.
/// - toDebugEnvironment: A function that transforms an environment into a debug environment by
/// describing a print function and a queue to print from. Defaults to a function that ignores
/// the environment and returns a default ``DebugEnvironment`` that uses Swift's `print`
/// function and a background queue.
/// - Returns: A side-effect that prints debug messages for all received actions.
func debug<LocalAction>(
_ prefix: String = "",
action toLocalAction: CasePath<SyncAction, LocalAction>,
actionFormat: ActionFormat = .prettyPrint,
environment toDebugEnvironment: @escaping (Environment) -> DebugEnvironment = { _ in
DebugEnvironment()
}
) -> Self {
#if DEBUG
.init { action, environment in
let debugEnvironment = toDebugEnvironment(environment)
debugEnvironment.queue.async {
let debugOutput = debugActionOutput(
received: EitherAction<Never, SyncAction>.sync(action),
produced: [],
asyncAction: .self,
syncAction: toLocalAction,
actionFormat: actionFormat
)
debugEnvironment.printer(
"""
\(prefix.isEmpty.if(true: "", false: "\(prefix): "))side-effect received:\(debugOutput)
"""
)
}
closure(action, environment)
}
#else
self
#endif
}
}