-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathThunkInstrumentation.swift
70 lines (66 loc) · 2.19 KB
/
ThunkInstrumentation.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
import Combine
import os.signpost
public extension Thunk {
func signpost(
_ prefix: String = "",
log: OSLog = OSLog(
subsystem: "com.reswift.recombine",
category: "Thunk Instrumentation"
)
) -> Self {
guard log.signpostsEnabled else { return self }
// NB: Prevent rendering as "N/A" in Instruments
let zeroWidthSpace = "\u{200B}"
return .init {
transform($0, $1, $2).thunkSignpost(
prefix.isEmpty.if(
true: zeroWidthSpace,
false: "[\(prefix)] "
),
log: log,
actionOutput: debugCaseOutput($1)
)
}
}
}
extension Publisher where Failure == Never {
func thunkSignpost(
_ prefix: String,
log: OSLog,
actionOutput: String
) -> Publishers.HandleEvents<Self> {
let sid = OSSignpostID(log: log)
let token = DebugToken().description
let printPrefix = "\(prefix)-\(token)"
return handleEvents(
receiveSubscription: { _ in
if log.signpostsEnabled {
os_signpost(
.begin, log: log, name: "Thunk", signpostID: sid, "%sStarted from %s", printPrefix,
actionOutput
)
}
},
receiveOutput: {
if log.signpostsEnabled {
os_signpost(
.event, log: log, name: "Thunk Output", "%sOutput from %s: %s", printPrefix, actionOutput, debugCaseOutput($0)
)
}
},
receiveCompletion: { completion in
switch completion {
case .finished:
if log.signpostsEnabled {
os_signpost(.end, log: log, name: "Thunk", signpostID: sid, "%sFinished", printPrefix)
}
}
},
receiveCancel: {
if log.signpostsEnabled {
os_signpost(.end, log: log, name: "Thunk", signpostID: sid, "%sCancelled", printPrefix)
}
}
)
}
}