diff --git a/Example/Example/ContentView.swift b/Example/Example/ContentView.swift index 563679b..2c18f47 100644 --- a/Example/Example/ContentView.swift +++ b/Example/Example/ContentView.swift @@ -8,10 +8,14 @@ class CounterModel { var isPresentingSheet = false var text = "" func decrementButtonTapped() { - count -= 1 + withAnimation { + count -= 1 + } } func incrementButtonTapped() { - count += 1 + withAnimation { + count += 1 + } } func presentSheetButtonTapped() { isPresentingSheet = true @@ -28,8 +32,10 @@ struct ContentView: View { TextField("Text", text: $model.text) if model.isDisplayingCount { Text(model.count.description) + .font(.largeTitle) } else { Text("Not tracking count") + .font(.largeTitle) } Button("Decrement") { model.decrementButtonTapped() } Button("Increment") { model.incrementButtonTapped() } @@ -45,6 +51,7 @@ struct ContentView: View { WithPerceptionTracking { Form { Text(model.count.description) + .font(.largeTitle) Button("Decrement") { model.decrementButtonTapped() } Button("Increment") { model.incrementButtonTapped() } } diff --git a/Package.swift b/Package.swift index 63ef8c3..074261f 100644 --- a/Package.swift +++ b/Package.swift @@ -47,3 +47,15 @@ let package = Package( ), ] ) + +//for target in package.targets where target.type != .system { +// target.swiftSettings = target.swiftSettings ?? [] +// target.swiftSettings?.append( +// .unsafeFlags([ +// "-c", "release", +// "-emit-module-interface", "-enable-library-evolution", +// "-Xfrontend", "-warn-concurrency", +// "-Xfrontend", "-enable-actor-data-race-checks", +// ]) +// ) +//} diff --git a/Sources/Perception/WithPerceptionTracking.swift b/Sources/Perception/WithPerceptionTracking.swift index b2642fc..e64a338 100644 --- a/Sources/Perception/WithPerceptionTracking.swift +++ b/Sources/Perception/WithPerceptionTracking.swift @@ -42,7 +42,6 @@ import SwiftUI @available(macOS, deprecated: 14, message: "Remove WithPerceptionTracking") @available(tvOS, deprecated: 17, message: "Remove WithPerceptionTracking") @available(watchOS, deprecated: 10, message: "Remove WithPerceptionTracking") -@MainActor public struct WithPerceptionTracking { @State var id = 0 let content: () -> Content @@ -55,10 +54,8 @@ public struct WithPerceptionTracking { let _ = self.id return withPerceptionTracking { self.instrumentedBody() - } onChange: { - Task { @MainActor in - self.id += 1 - } + } onChange: { [_id = UncheckedSendable(self._id)] in + _id.value.wrappedValue += 1 } } } @@ -167,3 +164,10 @@ public enum _PerceptionLocals { @TaskLocal public static var isInPerceptionTracking = false @TaskLocal public static var skipPerceptionChecking = false } + +private struct UncheckedSendable: @unchecked Sendable { + let value: A + init(_ value: A) { + self.value = value + } +}