From 4c696dad7f166026cf2dd5a88b0a9a39ba3b8cca Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Fri, 8 Nov 2024 13:20:23 -0500 Subject: [PATCH] Relax preview traits sendability. --- Sources/Dependencies/DependencyValues.swift | 6 +++++- Sources/Dependencies/Traits/PreviewTrait.swift | 12 +++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/Sources/Dependencies/DependencyValues.swift b/Sources/Dependencies/DependencyValues.swift index 52ae81cd..2a63ea76 100644 --- a/Sources/Dependencies/DependencyValues.swift +++ b/Sources/Dependencies/DependencyValues.swift @@ -485,7 +485,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 diff --git a/Sources/Dependencies/Traits/PreviewTrait.swift b/Sources/Dependencies/Traits/PreviewTrait.swift index f8e24aec..ff137ce0 100644 --- a/Sources/Dependencies/Traits/PreviewTrait.swift +++ b/Sources/Dependencies/Traits/PreviewTrait.swift @@ -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( + public static func dependency( _ keyPath: WritableKeyPath & Sendable, _ value: Value ) -> PreviewTrait { @@ -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