Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update messaging for perception warning. #96

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ It's unfortunate to have to wrap your view's content in `WithPerceptionTracking`
forget then you will helpfully get a runtime warning letting you know that observation is not
set up correctly:

> 🟣 Runtime Warning: Perceptible state was accessed but is not being tracked. Track changes to
> state by wrapping your view in a 'WithPerceptionTracking' view.
> 🟣 Runtime Warning: Perceptible state was accessed but is not being tracked. Track changes
> to state by wrapping your view in a 'WithPerceptionTracking' view. This must also be done
> for any escaping, trailing closures, such as 'GeometryReader', `LazyVStack` (and all lazy
> views), navigation APIs ('sheet', 'popover', 'fullScreenCover', etc.), and others.

### Bindable

Expand Down
4 changes: 0 additions & 4 deletions Sources/Perception/Internal/Locals.swift

This file was deleted.

16 changes: 16 additions & 0 deletions Sources/Perception/PerceptionChecking.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
import Foundation

/// Globally enable or disable perception checks.
///
/// The library performs certain runtime checks to make sure that the tools are being used
/// correctly. In particular, view bodies must be wrapped in the ``WithPerceptionTracking`` view
/// in order for observation to be properly tracked. If the library detects state is accessed
/// without being inside ``WithPerceptionTracking``, a runtime warning is triggered to let you
/// know there is something to fix.
///
/// This check only happens in `DEBUG` builds, and so does not affect App Store releases of your
/// app. However, the checks can sometimes be costly and slow down your app in development. If
/// you wish to fully disable the checks, you can set this boolean to `false.`
public var isPerceptionCheckingEnabled: Bool {
get { perceptionChecking.isPerceptionCheckingEnabled }
set { perceptionChecking.isPerceptionCheckingEnabled = newValue }
}

public enum _PerceptionLocals {
@TaskLocal public static var isInPerceptionTracking = false
@TaskLocal public static var skipPerceptionChecking = false
}

private let perceptionChecking = PerceptionChecking()

private class PerceptionChecking: @unchecked Sendable {
Expand Down
4 changes: 3 additions & 1 deletion Sources/Perception/PerceptionRegistrar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,9 @@ extension PerceptionRegistrar: Hashable {
reportIssue(
"""
Perceptible state was accessed but is not being tracked. Track changes to state by \
wrapping your view in a 'WithPerceptionTracking' view.
wrapping your view in a 'WithPerceptionTracking' view. This must also be done for any \
escaping, trailing closures, such as 'GeometryReader', `LazyVStack` (and all lazy \
views), navigation APIs ('sheet', 'popover', 'fullScreenCover', etc.), and others.
"""
)
}
Expand Down
10 changes: 9 additions & 1 deletion Sources/Perception/WithPerceptionTracking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,19 @@
/// }
/// ```
///
/// > Note: Other common escaping closures to be aware of:
/// > * Reader views, such as `GeometryReader`, ScrollViewReader`, etc.
/// > * Lazy views such as `LazyVStack`, `LazyVGrid`, etc.
/// > * Navigation APIs, such as `sheet`, `popover`, `fullScreenCover`, `navigationDestination`,
/// etc.
///
/// If a field of a `@Perceptible` model is accessed in a view while _not_ inside
/// ``WithPerceptionTracking``, then a runtime warning will helpfully be triggered:
///
/// > 🟣 Runtime Warning: Perceptible state was accessed but is not being tracked. Track changes
/// > to state by wrapping your view in a 'WithPerceptionTracking' view.
/// > to state by wrapping your view in a 'WithPerceptionTracking' view. This must also be done
/// > for any escaping, trailing closures, such as 'GeometryReader', `LazyVStack` (and all lazy
/// > views), navigation APIs ('sheet', 'popover', 'fullScreenCover', etc.), and others.
///
/// To debug this, expand the warning in the Issue Navigator of Xcode (cmd+5), and click through
/// the stack frames displayed to find the line in your view where you are accessing state without
Expand Down
4 changes: 3 additions & 1 deletion Tests/PerceptionTests/RuntimeWarningTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,9 @@
XCTExpectFailure(failingBlock: failingBlock) {
$0.compactDescription == """
failed - Perceptible state was accessed but is not being tracked. Track changes to state \
by wrapping your view in a 'WithPerceptionTracking' view.
by wrapping your view in a 'WithPerceptionTracking' view. This must also be done for any \
escaping, trailing closures, such as 'GeometryReader', `LazyVStack` (and all lazy \
views), navigation APIs ('sheet', 'popover', 'fullScreenCover', etc.), and others.
"""
}
}
Expand Down