mirror of
https://github.com/shadowsocks/ShadowsocksX-NG.git
synced 2026-11-24 15:00:40 +00:00
fix(route): make same-destination navigation a no-op
The sidebar List selection binding can be written back during view updates (macOS List/NavigationSplitView selection re-sync on row content changes); publishing from the setter then triggered the "Publishing changes from within view updates" runtime warning. Guard navigate(to:) against same-value writes and pin the no-publish invariant with a regression test.
This commit is contained in:
@@ -28,7 +28,11 @@ final class WorkspaceRoute: ObservableObject {
|
||||
@Published private(set) var destination: WorkspaceDestination = .home
|
||||
|
||||
/// workspace 内部导航只改变 route destination;开窗不由 route 驱动。
|
||||
/// 同值导航必须是无操作:macOS 的 List/NavigationSplitView 会在视图更新
|
||||
/// 期间回写 selection Binding(选择再同步),此时 publish 会触发
|
||||
/// "Publishing changes from within view updates" 运行时警告。
|
||||
func navigate(to destination: WorkspaceDestination) {
|
||||
guard destination != self.destination else { return }
|
||||
self.destination = destination
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import Combine
|
||||
import Foundation
|
||||
import XCTest
|
||||
|
||||
@@ -9,6 +10,25 @@ final class WorkspaceRouteTests: XCTestCase {
|
||||
XCTAssertEqual(WorkspaceRoute().destination, .home)
|
||||
}
|
||||
|
||||
/// 侧栏 List/NavigationSplitView 会在视图更新期间回写 selection Binding;
|
||||
/// 同值导航不得 publish(否则触发 view-update 期间发布警告)。
|
||||
func testNavigateToCurrentDestinationDoesNotPublish() {
|
||||
let route = WorkspaceRoute()
|
||||
var changeCount = 0
|
||||
let cancellable = route.objectWillChange.sink { _ in changeCount += 1 }
|
||||
|
||||
route.navigate(to: .home)
|
||||
XCTAssertEqual(changeCount, 0)
|
||||
XCTAssertEqual(route.destination, .home)
|
||||
|
||||
route.navigate(to: .servers)
|
||||
XCTAssertEqual(changeCount, 1)
|
||||
XCTAssertEqual(route.destination, .servers)
|
||||
|
||||
route.navigate(to: .servers)
|
||||
XCTAssertEqual(changeCount, 1)
|
||||
}
|
||||
|
||||
func testDestinationVocabularyIsFiniteAndStable() {
|
||||
XCTAssertEqual(
|
||||
WorkspaceDestination.allCases,
|
||||
|
||||
Reference in New Issue
Block a user