diff --git a/package-lock.json b/package-lock.json index ffe6dee17d80..b77e514e11b8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -69,7 +69,7 @@ "react-native-image-picker": "^4.10.2", "react-native-image-size": "git+https://github.com/Expensify/react-native-image-size#6b5ab5110dc3ed554f8eafbc38d7d87c17147972", "react-native-modal": "^13.0.0", - "react-native-onyx": "1.0.29", + "react-native-onyx": "1.0.31", "react-native-pdf": "^6.6.2", "react-native-performance": "^2.0.0", "react-native-permissions": "^3.0.1", @@ -35456,9 +35456,9 @@ } }, "node_modules/react-native-onyx": { - "version": "1.0.29", - "resolved": "https://registry.npmjs.org/react-native-onyx/-/react-native-onyx-1.0.29.tgz", - "integrity": "sha512-Zg7v6bimkRPqaAM5C/CqMswSObPGX+2BK2Gr7r4P1htSMjNlvn+fZ9gD4mwJBPgnoEUqMdFGLUshksHbE+GSMg==", + "version": "1.0.31", + "resolved": "https://registry.npmjs.org/react-native-onyx/-/react-native-onyx-1.0.31.tgz", + "integrity": "sha512-WaX6dD6II7sZoiiNnifCh7X+V2AjwOtMx5mmrGYBuspPUpLOIj3Lt3GM66YRe5T9d5PmyoKUl0ULuYip/7uBTQ==", "dependencies": { "ascii-table": "0.0.9", "fast-equals": "^4.0.3", @@ -69828,9 +69828,9 @@ } }, "react-native-onyx": { - "version": "1.0.29", - "resolved": "https://registry.npmjs.org/react-native-onyx/-/react-native-onyx-1.0.29.tgz", - "integrity": "sha512-Zg7v6bimkRPqaAM5C/CqMswSObPGX+2BK2Gr7r4P1htSMjNlvn+fZ9gD4mwJBPgnoEUqMdFGLUshksHbE+GSMg==", + "version": "1.0.31", + "resolved": "https://registry.npmjs.org/react-native-onyx/-/react-native-onyx-1.0.31.tgz", + "integrity": "sha512-WaX6dD6II7sZoiiNnifCh7X+V2AjwOtMx5mmrGYBuspPUpLOIj3Lt3GM66YRe5T9d5PmyoKUl0ULuYip/7uBTQ==", "requires": { "ascii-table": "0.0.9", "fast-equals": "^4.0.3", diff --git a/package.json b/package.json index 94711febc725..4c3c624dc868 100644 --- a/package.json +++ b/package.json @@ -100,7 +100,7 @@ "react-native-image-picker": "^4.10.2", "react-native-image-size": "git+https://github.com/Expensify/react-native-image-size#6b5ab5110dc3ed554f8eafbc38d7d87c17147972", "react-native-modal": "^13.0.0", - "react-native-onyx": "1.0.29", + "react-native-onyx": "1.0.31", "react-native-pdf": "^6.6.2", "react-native-performance": "^2.0.0", "react-native-permissions": "^3.0.1", diff --git a/src/libs/actions/SignInRedirect.js b/src/libs/actions/SignInRedirect.js index e66e50cab26c..0ae54436e15b 100644 --- a/src/libs/actions/SignInRedirect.js +++ b/src/libs/actions/SignInRedirect.js @@ -6,20 +6,6 @@ import * as Localize from '../Localize'; import * as PersistedRequests from './PersistedRequests'; import NetworkConnection from '../NetworkConnection'; -let currentActiveClients; -Onyx.connect({ - key: ONYXKEYS.ACTIVE_CLIENTS, - callback: (val) => { - currentActiveClients = !val ? [] : val; - }, -}); - -let currentPreferredLocale; -Onyx.connect({ - key: ONYXKEYS.NVP_PREFERRED_LOCALE, - callback: val => currentPreferredLocale = val, -}); - let currentIsOffline; let currentShouldForceOffline; Onyx.connect({ @@ -37,31 +23,28 @@ Onyx.connect({ * @param {String} errorMessage */ function clearStorageAndRedirect(errorMessage) { - const activeClients = currentActiveClients; - const preferredLocale = currentPreferredLocale; - const isOffline = currentIsOffline; - const shouldForceOffline = currentShouldForceOffline; + // Under certain conditions, there are key-values we'd like to keep in storage even when a user is logged out. + // We pass these into the clear() method in order to avoid having to reset them on a delayed tick and getting + // flashes of unwanted default state. + const keysToPreserve = []; + keysToPreserve.push(ONYXKEYS.NVP_PREFERRED_LOCALE); + keysToPreserve.push(ONYXKEYS.ACTIVE_CLIENTS); + + // After signing out, set ourselves as offline if we were offline before logging out and we are not forcing it. + // If we are forcing offline, ignore it while signed out, otherwise it would require a refresh because there's no way to toggle the switch to go back online while signed out. + if (currentIsOffline && !currentShouldForceOffline) { + keysToPreserve.push(ONYXKEYS.NETWORK); + } // Clearing storage discards the authToken. This causes a redirect to the SignIn screen - Onyx.clear() + Onyx.clear(keysToPreserve) .then(() => { - if (preferredLocale) { - Onyx.set(ONYXKEYS.NVP_PREFERRED_LOCALE, preferredLocale); - } - if (activeClients && activeClients.length > 0) { - Onyx.set(ONYXKEYS.ACTIVE_CLIENTS, activeClients); + if (!errorMessage) { + return; } - // After signing out, set ourselves as offline if we were offline before logging out and we are not forcing it. - // If we are forcing offline, ignore it while signed out, otherwise it would require a refresh because there's no way to toggle the switch to go back online while signed out. - if (isOffline && !shouldForceOffline) { - Onyx.set(ONYXKEYS.NETWORK, {isOffline}); - } - - // `Onyx.clear` reinitialize the Onyx instance with initial values so use `Onyx.merge` instead of `Onyx.set` - if (errorMessage) { - Onyx.merge(ONYXKEYS.SESSION, {errors: {[DateUtils.getMicroseconds()]: Localize.translateLocal(errorMessage)}}); - } + // `Onyx.clear` reinitializes the Onyx instance with initial values so use `Onyx.merge` instead of `Onyx.set` + Onyx.merge(ONYXKEYS.SESSION, {errors: {[DateUtils.getMicroseconds()]: Localize.translateLocal(errorMessage)}}); }); } diff --git a/tests/ui/UnreadIndicatorsTest.js b/tests/ui/UnreadIndicatorsTest.js index cb777bcd61e2..9883c1bf04fc 100644 --- a/tests/ui/UnreadIndicatorsTest.js +++ b/tests/ui/UnreadIndicatorsTest.js @@ -184,7 +184,7 @@ function signInAndGetAppWithUnreadChat() { } describe('Unread Indicators', () => { - afterEach(Onyx.clear); + afterEach(() => Onyx.clear()); it('Display bold in the LHN for unread chat and new line indicator above the chat message when we navigate to it', () => { let renderedApp; diff --git a/tests/unit/NetworkTest.js b/tests/unit/NetworkTest.js index f0cb04f7cd5f..6b725d94626b 100644 --- a/tests/unit/NetworkTest.js +++ b/tests/unit/NetworkTest.js @@ -36,7 +36,7 @@ beforeEach(() => { // Wait for any Log command to finish and Onyx to fully clear jest.advanceTimersByTime(CONST.NETWORK.PROCESS_REQUEST_DELAY_MS); return waitForPromisesToResolve() - .then(Onyx.clear) + .then(() => Onyx.clear()) .then(waitForPromisesToResolve); });