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

Fix exclusive access problem in preview traits. #306

Merged
merged 2 commits into from
Nov 12, 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
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