From ba4e011fc9132aa17cc6c4e8e74a6310a308291e Mon Sep 17 00:00:00 2001 From: Lucas Di Pentima Date: Mon, 15 Feb 2021 17:44:13 -0300 Subject: [PATCH] 16848: Improved event handler installation/removal. Arvados-DCO-1.1-Signed-off-by: Lucas Di Pentima --- src/store/auth/auth-action.test.ts | 2 +- .../auto-logout/auto-logout.tsx | 36 +++++++++++-------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/store/auth/auth-action.test.ts b/src/store/auth/auth-action.test.ts index 13575d44..616f2d2c 100644 --- a/src/store/auth/auth-action.test.ts +++ b/src/store/auth/auth-action.test.ts @@ -9,7 +9,7 @@ import 'jest-localstorage-mock'; import { ServiceRepository, createServices } from "~/services/services"; import { configureStore, RootStore } from "../store"; import { createBrowserHistory } from "history"; -import { mockConfig, DISCOVERY_DOC_PATH, } from '~/common/config'; +import { mockConfig } from '~/common/config'; import { ApiActions } from "~/services/api/api-actions"; import { ACCOUNT_LINK_STATUS_KEY } from '~/services/link-account-service/link-account-service'; import Axios from "axios"; diff --git a/src/views-components/auto-logout/auto-logout.tsx b/src/views-components/auto-logout/auto-logout.tsx index 9ccf79a5..54411926 100644 --- a/src/views-components/auto-logout/auto-logout.tsx +++ b/src/views-components/auto-logout/auto-logout.tsx @@ -24,12 +24,10 @@ interface AutoLogoutActionProps { doCloseWarn: () => void; } -const mapStateToProps = (state: RootState, ownProps: any): AutoLogoutDataProps => { - return { - sessionIdleTimeout: parse(state.auth.config.clusterConfig.Workbench.IdleTimeout, 's') || 0, - lastWarningDuration: ownProps.lastWarningDuration || 60, - }; -}; +const mapStateToProps = (state: RootState, ownProps: any): AutoLogoutDataProps => ({ + sessionIdleTimeout: parse(state.auth.config.clusterConfig.Workbench.IdleTimeout, 's') || 0, + lastWarningDuration: ownProps.lastWarningDuration || 60, +}); const mapDispatchToProps = (dispatch: Dispatch): AutoLogoutActionProps => ({ doLogout: () => dispatch(logout(true)), @@ -57,15 +55,25 @@ const LAST_ACTIVE_TIMESTAMP = 'lastActiveTimestamp'; export const AutoLogoutComponent = (props: AutoLogoutProps) => { let logoutTimer: NodeJS.Timer; const lastWarningDuration = min([props.lastWarningDuration, props.sessionIdleTimeout])! * 1000; - const handleOtherTabActivity = debounce(500, () => { - handleOnActive(); - reset(); - }); - window.addEventListener('storage', (e: StorageEvent) => { - // Other tab activity detected by a localStorage change event. - if (e.key === LAST_ACTIVE_TIMESTAMP) { handleOtherTabActivity(); } - }); + // Runs once after render + React.useEffect(() => { + window.addEventListener('storage', handleStorageEvents); + // Component cleanup + return () => { + window.removeEventListener('storage', handleStorageEvents); + }; + }, []); + + const handleStorageEvents = (e: StorageEvent) => { + if (e.key === LAST_ACTIVE_TIMESTAMP) { + // Other tab activity detected by a localStorage change event. + debounce(500, () => { + handleOnActive(); + reset(); + })(); + } + }; const handleOnIdle = () => { logoutTimer = setTimeout( -- 2.30.2