Skip to content

Commit

Permalink
Fix exclusive access problem in preview traits. (#306)
Browse files Browse the repository at this point in the history
* Fix exclusive access problem in preview traits.

* fix
  • Loading branch information
mbrandonw authored Nov 12, 2024
1 parent a24a0f1 commit 28f8271
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Sources/Dependencies/Traits/PreviewTrait.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@
public static func dependencies(
_ updateValuesForPreview: (inout DependencyValues) -> Void
) -> PreviewTrait {
updateValuesForPreview(&previewValues)
var copy = previewValues
defer { previewValues = copy }
updateValuesForPreview(&copy)
return PreviewTrait()
}
}
Expand Down
21 changes: 21 additions & 0 deletions Tests/DependenciesTests/PreviewTraitsTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#if canImport(Testing) && (os(iOS) || os(macOS) || os(tvOS) || os(watchOS))
import Dependencies
import Testing
import SwiftUI

@Suite
@MainActor
struct PreviewTraitsTests {
@Test
@available(iOS 18, macOS 15, tvOS 18, watchOS 11, visionOS 2, *)
func dependency() {
_ = PreviewTrait.dependency(\.date.now, Date(timeIntervalSince1970: 1_234_567_890))
withDependencies {
$0.context = .preview
} operation: {
@Dependency(\.date.now) var now
#expect(now == Date(timeIntervalSince1970: 1_234_567_890))
}
}
}
#endif

0 comments on commit 28f8271

Please sign in to comment.