From db6250d8d187a888bb3421cbe225e6c4a4ff381e Mon Sep 17 00:00:00 2001 From: Kyle Date: Sat, 4 Oct 2025 02:49:08 +0800 Subject: [PATCH] Fix nested State update issue --- .../Data/State/StoredLocation.swift | 2 +- .../Data/State/StateCompatibilityTests.swift | 38 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/Sources/OpenSwiftUICore/Data/State/StoredLocation.swift b/Sources/OpenSwiftUICore/Data/State/StoredLocation.swift index 79d7e248b..4ece458c9 100644 --- a/Sources/OpenSwiftUICore/Data/State/StoredLocation.swift +++ b/Sources/OpenSwiftUICore/Data/State/StoredLocation.swift @@ -35,7 +35,7 @@ package class StoredLocationBase: AnyLocation, Location, @unchecke return false } box.$data.access { data in - _ = data.savedValue.removeFirst() + _ = data.savedValue.removeLast() } return true } diff --git a/Tests/OpenSwiftUICompatibilityTests/Data/State/StateCompatibilityTests.swift b/Tests/OpenSwiftUICompatibilityTests/Data/State/StateCompatibilityTests.swift index 6f5d1bc1f..07a700d7e 100644 --- a/Tests/OpenSwiftUICompatibilityTests/Data/State/StateCompatibilityTests.swift +++ b/Tests/OpenSwiftUICompatibilityTests/Data/State/StateCompatibilityTests.swift @@ -5,6 +5,7 @@ import Testing import OpenSwiftUITestsSupport +@MainActor struct StateCompatibilityTests { @Test func appear() async throws { @@ -32,4 +33,41 @@ struct StateCompatibilityTests { ) } } + + @Test("StoredLocationBase.BeginUpdate.removeLast verify") + func nestedStateUpdate() async throws { + @MainActor + enum Helper { + static var values: [Int] = [] + static var valueChanges: [[Int]] = [] + } + + struct ContentView: View { + @State private var value = 0 + + var body: some View { + let _ = Helper.values.append(value) + Color.white + .onAppear { + value = 1 + value = 2 + withAnimation { + value = 3 + value = 4 + } + } + .onChange(of: value) { oldValue, newValue in + Helper.valueChanges.append([oldValue, newValue]) + } + } + } + + try await triggerLayoutWithWindow(expectedCount: 0) { _ in + PlatformHostingController( + rootView: ContentView() + ) + } + #expect(Helper.values == [0, 2, 4]) + #expect(Helper.valueChanges == [[0, 2], [2, 4]]) + } }