From 96371d2bd47c2ac8cbf0ce848233d9104401c37c Mon Sep 17 00:00:00 2001 From: WofWca Date: Sat, 14 Jun 2025 00:25:45 +0400 Subject: [PATCH 1/2] refactor: remove unnecessary `useEffect` --- .../components/dialogs/ProxyConfiguration/index.tsx | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/packages/frontend/src/components/dialogs/ProxyConfiguration/index.tsx b/packages/frontend/src/components/dialogs/ProxyConfiguration/index.tsx index 194eaf1de0..10794b2ead 100644 --- a/packages/frontend/src/components/dialogs/ProxyConfiguration/index.tsx +++ b/packages/frontend/src/components/dialogs/ProxyConfiguration/index.tsx @@ -57,7 +57,6 @@ export default function ProxyConfiguration( const [newProxyUrl, setNewProxyUrl] = useState('') const [showNewProxyForm, setShowNewProxyForm] = useState(false) - const [showEnableSwitch, setShowEnableSwitch] = useState(false) // updated on connectivity change const [connectivityStatus, setConnectivityStatus] = useState( @@ -209,15 +208,6 @@ export default function ProxyConfiguration( [proxyState.activeProxy, proxyState.proxies, updateProxyState] ) - // show/hide the enable switch - useEffect(() => { - if (proxyState.enabled) { - setShowEnableSwitch(proxyState.proxies.length > 0) - } else { - setShowEnableSwitch(proxyState.proxies.length > 0) - } - }, [showEnableSwitch, proxyState.enabled, proxyState.proxies]) - useEffect(() => { let removeConnectivityListener = () => {} const checkConnectivity = async () => { @@ -344,7 +334,7 @@ export default function ProxyConfiguration( />
- {showEnableSwitch && ( + {proxyState.proxies.length > 0 && ( Date: Sat, 14 Jun 2025 00:50:26 +0400 Subject: [PATCH 2/2] fix: resource leak and debounce in ProxyConfig Debouncing didn't work, and the DC event listener would hang around forever sometimes. See https://github.com/deltachat/deltachat-desktop/pull/5052#discussion_r2066144385. --- .../dialogs/ProxyConfiguration/index.tsx | 45 ++++++++++--------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/packages/frontend/src/components/dialogs/ProxyConfiguration/index.tsx b/packages/frontend/src/components/dialogs/ProxyConfiguration/index.tsx index 10794b2ead..5f8a382b71 100644 --- a/packages/frontend/src/components/dialogs/ProxyConfiguration/index.tsx +++ b/packages/frontend/src/components/dialogs/ProxyConfiguration/index.tsx @@ -18,7 +18,7 @@ import Button from '../../Button' import styles from './styles.module.scss' import { Proxy } from '../../Settings/DefaultCredentials' -import { debounceWithInit } from '../../chat/ChatListHelpers' +import { debounce } from 'debounce' import { getLogger } from '@deltachat-desktop/shared/logger' import { unknownErrorToString } from '../../helpers/unknownErrorToString' @@ -209,27 +209,32 @@ export default function ProxyConfiguration( ) useEffect(() => { - let removeConnectivityListener = () => {} - const checkConnectivity = async () => { - if (configured) { - const connectivity = await BackendRemote.rpc.getConnectivity(accountId) - setConnectivityStatus(connectivity) - removeConnectivityListener = onDCEvent( - accountId, - 'ConnectivityChanged', - () => - debounceWithInit(async () => { - const connectivity = - await BackendRemote.rpc.getConnectivity(accountId) - setConnectivityStatus(connectivity) - }, 300)() - ) - } + let outdated = false + + if (!configured) { + return } - checkConnectivity() - return () => { - removeConnectivityListener() + + const update = async () => { + const connectivity = await BackendRemote.rpc.getConnectivity(accountId) + + if (outdated) { + return + } + + setConnectivityStatus(connectivity) } + + const debouncedUpdate = debounce(update, 300) + debouncedUpdate() + debouncedUpdate.flush() + + const cleanup = [ + onDCEvent(accountId, 'ConnectivityChanged', debouncedUpdate), + () => debouncedUpdate.clear(), + () => (outdated = true), + ] + return () => cleanup.forEach(off => off()) }, [accountId, configured]) /**