fix(sysproxy): read the live verge config in the toggle drain loop

The drain loop captured the render-time verge snapshot, so a config
revalidation landing mid-loop left later iterations deciding connection
closing and rollback with stale values. The confirmed-state seed and
the in-loop auto-close check now read through a latest ref.
This commit is contained in:
Tunglies
2026-11-19 22:58:48 +08:00
parent cc202fa312
commit df4d09167d
+6 -2
View File
@@ -48,10 +48,14 @@ export const useSystemProxyState = () => {
// Coalesce rapid clicks so only the latest requested state is applied.
const pendingRef = useRef<boolean | null>(null)
const busyRef = useRef(false)
// The drain loop outlives the render it started in; reading the config
// through a ref keeps mid-loop revalidations visible to later iterations.
const vergeRef = useRef(verge)
vergeRef.current = verge
const toggleSystemProxy = async (enabled: boolean) => {
// Roll failed optimistic writes back to the latest confirmed state.
let confirmed = verge?.enable_system_proxy ?? false
let confirmed = vergeRef.current?.enable_system_proxy ?? false
mutateVerge((prev) =>
prev ? { ...prev, enable_system_proxy: enabled } : prev,
)
@@ -69,7 +73,7 @@ export const useSystemProxyState = () => {
errorNotice: false,
})
confirmed = target
if (!target && verge?.auto_close_connection) {
if (!target && vergeRef.current?.auto_close_connection) {
await mutate(() => closeAllConnections(), {
id: 'close-all-connections',
errorNotice: false,