Skip to content

Commit

Permalink
updates after review
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalydosos committed Jan 7, 2025
1 parent d78885f commit f026b07
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/components/Image/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, {useCallback, useContext, useEffect, useMemo, useRef, useState} fr
import FullScreenLoadingIndicator from '@components/FullscreenLoadingIndicator';
import {useSession} from '@components/OnyxProvider';
import {isExpiredSession} from '@libs/actions/Session';
import activateReauthenticator from '@libs/actions/Session/Reauthenticator';
import activateReauthenticator from '@libs/actions/Session/AttachmentImageReauthenticator';
import CONST from '@src/CONST';
import BaseImage from './BaseImage';
import {ImageBehaviorContext} from './ImageBehaviorContextProvider';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import Onyx from 'react-native-onyx';
import {reauthenticate} from '@libs/Authentication';
import Log from '@libs/Log';
import ONYXKEYS from '@src/ONYXKEYS';
import type Session from '@src/types/onyx/Session';

let isOffline = false;
let active = false;
let currentActiveSession: Session = {};
let timer: NodeJS.Timeout;
// the delay before requesting a reauthentication once activated
// we think in that timing a "natural" reauthentication could happen (a session expired in the carousel is the only exception)
// and we wish not to overlap and make a double reauthentication
// The delay before requesting a reauthentication once activated
// When the session is expired we will give it this time to reauthenticate via normal flows, like the Reauthentication middleware, in an attempt to not duplicate authentication requests
// also, this is an arbitrary number so we may tweak as needed
const TIMING_BEFORE_REAUTHENTICATION_MS = 3500; // 3.5s

// We subscribe to network's online/offline status
Expand All @@ -20,13 +21,6 @@ Onyx.connect({
return;
}
isOffline = !!network.shouldForceOffline || !!network.isOffline;
// if offline we make sure of that by requesting the status 0.5s later
if (!isOffline) {
return;
}
setTimeout(() => {
isOffline = !!network.shouldForceOffline || !!network.isOffline;
}, 500);
},
});

Expand All @@ -52,8 +46,8 @@ function deactivate() {
}

/**
* the reauthenticator is currently only used by attachment images and only when the current session is expired
* it will only request reauthentification only once between two receptions of different sessions from Onyx
* The reauthenticator is currently only used by attachment images and only when the current session is expired.
* It will only request reauthentification only once between two receptions of different sessions from Onyx
* @param session the current session
* @returns
*/
Expand All @@ -63,15 +57,16 @@ function activate(session: Session) {
}
currentActiveSession = session;
active = true;
// no need to Timers.register()
timer = setTimeout(tryReauthenticate, TIMING_BEFORE_REAUTHENTICATION_MS);
}

function tryReauthenticate() {
if (isOffline || !active) {
return;
}
reauthenticate();
reauthenticate().catch((error) => {
Log.hmmm('Could not reauthenticate attachment image or receipt', {error});
});
}

export default activate;

0 comments on commit f026b07

Please sign in to comment.