Skip to content

Commit

Permalink
Relax preview traits sendability. (#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrandonw authored Nov 9, 2024
1 parent 26b95e1 commit e84352a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
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

0 comments on commit e84352a

Please sign in to comment.