Skip to content

Commit

Permalink
Make perception checks configurable
Browse files Browse the repository at this point in the history
Perception checking can be expensive, so let's give folks to the option
to toggle it off when developing.
  • Loading branch information
stephencelis committed Jan 12, 2024
1 parent 915fc78 commit 6d3c1e2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
29 changes: 29 additions & 0 deletions Sources/Perception/PerceptionChecking.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Foundation

public var isPerceptionCheckingEnabled: Bool {
get { perceptionChecking.isPerceptionCheckingEnabled }
set { perceptionChecking.isPerceptionCheckingEnabled = newValue }
}

private let perceptionChecking = PerceptionChecking()

private class PerceptionChecking: @unchecked Sendable {
var isPerceptionCheckingEnabled: Bool {
get {
lock.lock()
defer { lock.unlock() }
return _isPerceptionCheckingEnabled
}
set {
lock.lock()
defer { lock.unlock() }
_isPerceptionCheckingEnabled = newValue
}
}
let lock = NSLock()
#if DEBUG
var _isPerceptionCheckingEnabled = true
#else
var _isPerceptionCheckingEnabled = false
#endif
}
1 change: 1 addition & 0 deletions Sources/Perception/PerceptionRegistrar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ extension PerceptionRegistrar: Hashable {
#if DEBUG
private func perceptionCheck() {
if
isPerceptionCheckingEnabled,
!_PerceptionLocals.isInPerceptionTracking,
!_PerceptionLocals.skipPerceptionChecking,
isInSwiftUIBody()
Expand Down
6 changes: 3 additions & 3 deletions Sources/Perception/WithPerceptionTracking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ extension WithPerceptionTracking: TableColumnContent where Content: TableColumnC
self.content = content
}

public var tableColumnBody: Never {
nonisolated public var tableColumnBody: Never {
fatalError()
}

public static func _makeContent(
nonisolated public static func _makeContent(
content: _GraphValue<WithPerceptionTracking<Content>>, inputs: _TableColumnInputs
) -> _TableColumnOutputs {
Content._makeContent(content: content[\.body], inputs: inputs)
Expand All @@ -141,7 +141,7 @@ extension WithPerceptionTracking: TableRowContent where Content: TableRowContent
self.content = content
}

public var tableRowBody: Never {
nonisolated public var tableRowBody: Never {
fatalError()
}
}
Expand Down

0 comments on commit 6d3c1e2

Please sign in to comment.