Skip to content

Commit

Permalink
Tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jan 5, 2025
1 parent a6fc06a commit 3d98a76
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
6 changes: 3 additions & 3 deletions Sources/Defaults/Defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ extension Defaults {

continuation.onTermination = { _ in
// `invalidate()` should be thread-safe, but it is not in practice.
DispatchQueue.main.async {
Task { @MainActor in
observation.invalidate()
}
}
Expand Down Expand Up @@ -334,7 +334,7 @@ extension Defaults {

continuation.onTermination = { _ in
// `invalidate()` should be thread-safe, but it is not in practice.
DispatchQueue.main.async {
Task { @MainActor in
for observation in immutableObservations {
observation.invalidate()
}
Expand Down Expand Up @@ -378,7 +378,7 @@ extension Defaults {

continuation.onTermination = { _ in
// `invalidate()` should be thread-safe, but it is not in practice.
DispatchQueue.main.async {
Task { @MainActor in
for observation in observations {
observation.invalidate()
}
Expand Down
5 changes: 4 additions & 1 deletion Sources/Defaults/SwiftUI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ Access stored values from SwiftUI.

This is similar to `@AppStorage` but it accepts a ``Defaults/Key`` and many more types.
*/
@MainActor
@propertyWrapper
public struct Default<Value: Defaults.Serializable>: DynamicProperty {
public struct Default<Value: Defaults.Serializable>: @preconcurrency DynamicProperty {
@_documentation(visibility: private)
public typealias Publisher = AnyPublisher<Defaults.KeyChange<Value>, Never>

Expand Down Expand Up @@ -254,8 +255,10 @@ extension Defaults.Toggle {
}
}

@MainActor
@propertyWrapper
private struct ViewStorage<Value>: DynamicProperty {
@MainActor
private final class ValueBox {
var value: Value

Expand Down
1 change: 1 addition & 0 deletions Tests/DefaultsTests/DefaultsSwiftUITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ final class DefaultsSwiftUITests {
Defaults.removeAll(suite: suite_)
}

@MainActor
@Test
func testSwiftUIObserve() {
let view = ContentView()
Expand Down

0 comments on commit 3d98a76

Please sign in to comment.