Skip to content

Commit 41581ed

Browse files
authored
Make perception checks configurable (pointfreeco#16)
Perception checking can be expensive, so let's give folks to the option to toggle it off when developing.
1 parent 915fc78 commit 41581ed

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import Foundation
2+
3+
public var isPerceptionCheckingEnabled: Bool {
4+
get { perceptionChecking.isPerceptionCheckingEnabled }
5+
set { perceptionChecking.isPerceptionCheckingEnabled = newValue }
6+
}
7+
8+
private let perceptionChecking = PerceptionChecking()
9+
10+
private class PerceptionChecking: @unchecked Sendable {
11+
var isPerceptionCheckingEnabled: Bool {
12+
get {
13+
lock.lock()
14+
defer { lock.unlock() }
15+
return _isPerceptionCheckingEnabled
16+
}
17+
set {
18+
lock.lock()
19+
defer { lock.unlock() }
20+
_isPerceptionCheckingEnabled = newValue
21+
}
22+
}
23+
let lock = NSLock()
24+
#if DEBUG
25+
var _isPerceptionCheckingEnabled = true
26+
#else
27+
var _isPerceptionCheckingEnabled = false
28+
#endif
29+
}

Sources/Perception/PerceptionRegistrar.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ extension PerceptionRegistrar: Hashable {
196196
#if DEBUG
197197
private func perceptionCheck() {
198198
if
199+
isPerceptionCheckingEnabled,
199200
!_PerceptionLocals.isInPerceptionTracking,
200201
!_PerceptionLocals.skipPerceptionChecking,
201202
isInSwiftUIBody()

Sources/Perception/WithPerceptionTracking.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,11 @@ extension WithPerceptionTracking: TableColumnContent where Content: TableColumnC
118118
self.content = content
119119
}
120120

121-
public var tableColumnBody: Never {
121+
nonisolated public var tableColumnBody: Never {
122122
fatalError()
123123
}
124124

125-
public static func _makeContent(
125+
nonisolated public static func _makeContent(
126126
content: _GraphValue<WithPerceptionTracking<Content>>, inputs: _TableColumnInputs
127127
) -> _TableColumnOutputs {
128128
Content._makeContent(content: content[\.body], inputs: inputs)
@@ -141,7 +141,7 @@ extension WithPerceptionTracking: TableRowContent where Content: TableRowContent
141141
self.content = content
142142
}
143143

144-
public var tableRowBody: Never {
144+
nonisolated public var tableRowBody: Never {
145145
fatalError()
146146
}
147147
}

0 commit comments

Comments
 (0)