From c8330d871357802b389b38203e6867ad7dbf086a Mon Sep 17 00:00:00 2001 From: Abhinav Date: Thu, 13 Apr 2023 21:28:43 +0530 Subject: [PATCH] deregister eventListener and clearInterval of syncWithRemote on logout --- src/pages/gallery/index.tsx | 30 +++++++++++++++++++++--------- src/services/userService.ts | 9 +++------ 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/pages/gallery/index.tsx b/src/pages/gallery/index.tsx index 6cccbc7ef..018b62bf5 100644 --- a/src/pages/gallery/index.tsx +++ b/src/pages/gallery/index.tsx @@ -109,6 +109,7 @@ import { checkConnectivity } from 'utils/common'; import { SYNC_INTERVAL_IN_MICROSECONDS } from 'constants/gallery'; import ElectronService from 'services/electron/common'; import uploadManager from 'services/upload/uploadManager'; +import { getToken } from 'utils/common/key'; export const DeadCenter = styled('div')` flex: 1; @@ -188,7 +189,8 @@ export default function Gallery() { const [searchResultSummary, setSetSearchResultSummary] = useState(null); const syncInProgress = useRef(true); - const resync = useRef(false); + const syncInterval = useRef(); + const resync = useRef<{ force: boolean; silent: boolean }>(); const [deletedFileIds, setDeletedFileIds] = useState>( new Set() ); @@ -265,14 +267,18 @@ export default function Gallery() { setIsFirstLoad(false); setJustSignedUp(false); setIsFirstFetch(false); - setInterval(() => { + syncInterval.current = setInterval(() => { syncWithRemote(false, true); }, SYNC_INTERVAL_IN_MICROSECONDS); - ElectronService.registerForegroundEventListener(() => - syncWithRemote(false, true) - ); + ElectronService.registerForegroundEventListener(() => { + syncWithRemote(false, true); + }); }; main(); + return () => { + clearInterval(syncInterval.current); + ElectronService.registerForegroundEventListener(() => {}); + }; }, []); useEffect(() => { @@ -341,13 +347,18 @@ export default function Gallery() { const syncWithRemote = async (force = false, silent = false) => { if (syncInProgress.current && !force) { - resync.current = true; + resync.current = { force, silent }; return; } syncInProgress.current = true; try { checkConnectivity(); - if (!(await isTokenValid())) { + const token = getToken(); + if (!token) { + return; + } + const tokenValid = await isTokenValid(token); + if (!tokenValid) { throw new Error(ServerErrorCodes.SESSION_EXPIRED); } !silent && startLoading(); @@ -375,8 +386,9 @@ export default function Gallery() { } syncInProgress.current = false; if (resync.current) { - resync.current = false; - setTimeout(() => syncWithRemote(), 0); + const { force, silent } = resync.current; + setTimeout(() => syncWithRemote(force, silent), 0); + resync.current = null; } }; diff --git a/src/services/userService.ts b/src/services/userService.ts index 4611070d7..2602bb9ec 100644 --- a/src/services/userService.ts +++ b/src/services/userService.ts @@ -167,16 +167,13 @@ export const clearFiles = async () => { await localForage.clear(); }; -export const isTokenValid = async () => { +export const isTokenValid = async (token: string) => { try { - if (!getToken()) { - return false; - } const resp = await HTTPService.get( `${ENDPOINT}/users/session-validity/v2`, null, { - 'X-Auth-Token': getToken(), + 'X-Auth-Token': token, } ); try { @@ -186,7 +183,7 @@ export const isTokenValid = async () => { if (!resp.data['hasSetKeys']) { try { await putAttributes( - getToken(), + token, getData(LS_KEYS.ORIGINAL_KEY_ATTRIBUTES) ); } catch (e) {