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

Relax preview traits sendability. #299

Merged
merged 2 commits into from
Nov 9, 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: 5 additions & 1 deletion Sources/Dependencies/DependencyValues.swift
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,11 @@ public final class CachedValues: @unchecked Sendable {
case .preview:
if !CachedValues.isAccessingCachedDependencies {
value = CachedValues.$isAccessingCachedDependencies.withValue(true) {
previewValues.withValue { $0[key] }
#if compiler(>=6)
return previewValues[key]
#else
return Key.previewValue
#endif
}
} else {
value = Key.previewValue
Expand Down
12 changes: 5 additions & 7 deletions Sources/Dependencies/Traits/PreviewTrait.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/// - Parameters:
/// - keyPath: A key path to a dependency value.
/// - value: A dependency value to override for the lifetime of the preview.
public static func dependency<Value: Sendable>(
public static func dependency<Value>(
_ keyPath: WritableKeyPath<DependencyValues, Value> & Sendable,
_ value: Value
) -> PreviewTrait {
Expand All @@ -45,14 +45,12 @@
/// - Parameter updateValuesForPreview: A closure for updating the current dependency values for
/// the lifetime of the preview.
public static func dependencies(
_ updateValuesForPreview: @Sendable (inout DependencyValues) -> Void
_ updateValuesForPreview: (inout DependencyValues) -> Void
) -> PreviewTrait {
previewValues.withValue {
updateValuesForPreview(&$0)
}
updateValuesForPreview(&previewValues)
return PreviewTrait()
}
}
#endif

let previewValues = LockIsolated(DependencyValues(context: .preview))
nonisolated(unsafe) var previewValues = DependencyValues(context: .preview)
#endif