From 1fc9df8a043ba1d9495ef8151aed1d247ffcf672 Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Thu, 29 Dec 2022 10:09:32 -0700 Subject: [PATCH 1/7] Re-revert https://github.com/Expensify/App/pull/13758 --- src/libs/actions/SignInRedirect.js | 41 +++++++++++++++++------------- tests/ui/UnreadIndicatorsTest.js | 2 +- tests/unit/NetworkTest.js | 2 +- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/src/libs/actions/SignInRedirect.js b/src/libs/actions/SignInRedirect.js index e66e50cab26c..0c7ba243465e 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,10 +23,29 @@ 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); + } + + Onyx.clear(keysToPreserve) + .then(() => { + if (!errorMessage) { + return; + } + + // `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)}}); + }); + // Clearing storage discards the authToken. This causes a redirect to the SignIn screen Onyx.clear() 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 1586e1a10c7c..0a02132af77e 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); }); From d8b3f7929684b024e3140545be964f657eeec80a Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Mon, 2 Jan 2023 14:48:38 -0800 Subject: [PATCH 2/7] Remove bad merge conflict --- src/libs/actions/SignInRedirect.js | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/src/libs/actions/SignInRedirect.js b/src/libs/actions/SignInRedirect.js index 0c7ba243465e..c7b1079c04f4 100644 --- a/src/libs/actions/SignInRedirect.js +++ b/src/libs/actions/SignInRedirect.js @@ -45,29 +45,6 @@ function clearStorageAndRedirect(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)}}); }); - - - // Clearing storage discards the authToken. This causes a redirect to the SignIn screen - Onyx.clear() - .then(() => { - if (preferredLocale) { - Onyx.set(ONYXKEYS.NVP_PREFERRED_LOCALE, preferredLocale); - } - if (activeClients && activeClients.length > 0) { - Onyx.set(ONYXKEYS.ACTIVE_CLIENTS, activeClients); - } - - // 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)}}); - } - }); } /** From e58cad2eac2d13fa3f9167e61a3d779b2dbbec04 Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Mon, 2 Jan 2023 16:16:50 -0800 Subject: [PATCH 3/7] Add some timing of Onyx.clear --- src/libs/actions/SignInRedirect.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/libs/actions/SignInRedirect.js b/src/libs/actions/SignInRedirect.js index c7b1079c04f4..7ff0a85808b8 100644 --- a/src/libs/actions/SignInRedirect.js +++ b/src/libs/actions/SignInRedirect.js @@ -5,6 +5,7 @@ import DateUtils from '../DateUtils'; import * as Localize from '../Localize'; import * as PersistedRequests from './PersistedRequests'; import NetworkConnection from '../NetworkConnection'; +import Timing from './Timing'; let currentIsOffline; let currentShouldForceOffline; @@ -36,8 +37,10 @@ function clearStorageAndRedirect(errorMessage) { keysToPreserve.push(ONYXKEYS.NETWORK); } + Timing.start('clear'); Onyx.clear(keysToPreserve) .then(() => { + Timing.end('clear'); if (!errorMessage) { return; } From 6def1b37c89cdf3fd93e52cc4b68ae0984eae324 Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Tue, 3 Jan 2023 15:08:41 -0800 Subject: [PATCH 4/7] Don't sign out until Onyx.clear() is finished --- src/libs/actions/Session/index.js | 14 +------------- src/libs/actions/SignInRedirect.js | 2 -- 2 files changed, 1 insertion(+), 15 deletions(-) diff --git a/src/libs/actions/Session/index.js b/src/libs/actions/Session/index.js index b564efd5e7bb..655d1e29555d 100644 --- a/src/libs/actions/Session/index.js +++ b/src/libs/actions/Session/index.js @@ -29,18 +29,6 @@ Onyx.connect({ function signOut() { Log.info('Flushing logs before signing out', true, {}, true); - const optimisticData = [ - { - onyxMethod: CONST.ONYX.METHOD.SET, - key: ONYXKEYS.SESSION, - value: null, - }, - { - onyxMethod: CONST.ONYX.METHOD.SET, - key: ONYXKEYS.CREDENTIALS, - value: {}, - }, - ]; API.write('LogOut', { // Send current authToken because we will immediately clear it once triggering this command authToken: NetworkStore.getAuthToken(), @@ -48,7 +36,7 @@ function signOut() { partnerName: CONFIG.EXPENSIFY.PARTNER_NAME, partnerPassword: CONFIG.EXPENSIFY.PARTNER_PASSWORD, shouldRetry: false, - }, {optimisticData}); + }); Timing.clearData(); } diff --git a/src/libs/actions/SignInRedirect.js b/src/libs/actions/SignInRedirect.js index 7ff0a85808b8..aaccbf35ef54 100644 --- a/src/libs/actions/SignInRedirect.js +++ b/src/libs/actions/SignInRedirect.js @@ -37,10 +37,8 @@ function clearStorageAndRedirect(errorMessage) { keysToPreserve.push(ONYXKEYS.NETWORK); } - Timing.start('clear'); Onyx.clear(keysToPreserve) .then(() => { - Timing.end('clear'); if (!errorMessage) { return; } From e7651685ba1f8b9e4ee54d023a3c917de5179b2b Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Thu, 12 Jan 2023 08:26:45 -0800 Subject: [PATCH 5/7] Update react-native-onyx version --- package-lock.json | 14 +++++++------- package.json | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index e66eceaa28bc..120867b5834b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -68,7 +68,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.32", + "react-native-onyx": "1.0.34", "react-native-pdf": "^6.6.2", "react-native-performance": "^4.0.0", "react-native-permissions": "^3.0.1", @@ -35507,9 +35507,9 @@ } }, "node_modules/react-native-onyx": { - "version": "1.0.32", - "resolved": "https://registry.npmjs.org/react-native-onyx/-/react-native-onyx-1.0.32.tgz", - "integrity": "sha512-mhmCrxYfNlLM8bpP2M5g1u90115VqbJ1Lt2PjyrQsJihHRTdc7yqbiWwWlEQ1KyAYVR79JCSesKelgkdRAZIag==", + "version": "1.0.34", + "resolved": "https://registry.npmjs.org/react-native-onyx/-/react-native-onyx-1.0.34.tgz", + "integrity": "sha512-k2iqx72KWr/Ggwao/FtJwHwopz2h9ByRc2dIIkUG3APba7+xMZC7JSUMJCoopoQXIht/k0eVL8kXsPXSZElKNg==", "dependencies": { "ascii-table": "0.0.9", "fast-equals": "^4.0.3", @@ -69938,9 +69938,9 @@ } }, "react-native-onyx": { - "version": "1.0.32", - "resolved": "https://registry.npmjs.org/react-native-onyx/-/react-native-onyx-1.0.32.tgz", - "integrity": "sha512-mhmCrxYfNlLM8bpP2M5g1u90115VqbJ1Lt2PjyrQsJihHRTdc7yqbiWwWlEQ1KyAYVR79JCSesKelgkdRAZIag==", + "version": "1.0.34", + "resolved": "https://registry.npmjs.org/react-native-onyx/-/react-native-onyx-1.0.34.tgz", + "integrity": "sha512-k2iqx72KWr/Ggwao/FtJwHwopz2h9ByRc2dIIkUG3APba7+xMZC7JSUMJCoopoQXIht/k0eVL8kXsPXSZElKNg==", "requires": { "ascii-table": "0.0.9", "fast-equals": "^4.0.3", diff --git a/package.json b/package.json index 6c5d2de74cb8..e5e29ebf96c0 100644 --- a/package.json +++ b/package.json @@ -99,7 +99,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.32", + "react-native-onyx": "1.0.34", "react-native-pdf": "^6.6.2", "react-native-performance": "^4.0.0", "react-native-permissions": "^3.0.1", From 2600587389e8eaeb9705c12b3f66a9bc8d7ac12a Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Thu, 12 Jan 2023 16:12:36 -0800 Subject: [PATCH 6/7] Update react-native-onyx version --- package-lock.json | 14 +++++++------- package.json | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index 120867b5834b..91b135024490 100644 --- a/package-lock.json +++ b/package-lock.json @@ -68,7 +68,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.34", + "react-native-onyx": "1.0.35", "react-native-pdf": "^6.6.2", "react-native-performance": "^4.0.0", "react-native-permissions": "^3.0.1", @@ -35507,9 +35507,9 @@ } }, "node_modules/react-native-onyx": { - "version": "1.0.34", - "resolved": "https://registry.npmjs.org/react-native-onyx/-/react-native-onyx-1.0.34.tgz", - "integrity": "sha512-k2iqx72KWr/Ggwao/FtJwHwopz2h9ByRc2dIIkUG3APba7+xMZC7JSUMJCoopoQXIht/k0eVL8kXsPXSZElKNg==", + "version": "1.0.35", + "resolved": "https://registry.npmjs.org/react-native-onyx/-/react-native-onyx-1.0.35.tgz", + "integrity": "sha512-HQDSM0c2ADb54NoSQdxqeJOhViICB9HwE8aMEB62AdHkRw6crCoIX7iSIF0ewbZ2A/hbX3frewWq8AUh3AyMvA==", "dependencies": { "ascii-table": "0.0.9", "fast-equals": "^4.0.3", @@ -69938,9 +69938,9 @@ } }, "react-native-onyx": { - "version": "1.0.34", - "resolved": "https://registry.npmjs.org/react-native-onyx/-/react-native-onyx-1.0.34.tgz", - "integrity": "sha512-k2iqx72KWr/Ggwao/FtJwHwopz2h9ByRc2dIIkUG3APba7+xMZC7JSUMJCoopoQXIht/k0eVL8kXsPXSZElKNg==", + "version": "1.0.35", + "resolved": "https://registry.npmjs.org/react-native-onyx/-/react-native-onyx-1.0.35.tgz", + "integrity": "sha512-HQDSM0c2ADb54NoSQdxqeJOhViICB9HwE8aMEB62AdHkRw6crCoIX7iSIF0ewbZ2A/hbX3frewWq8AUh3AyMvA==", "requires": { "ascii-table": "0.0.9", "fast-equals": "^4.0.3", diff --git a/package.json b/package.json index e5e29ebf96c0..1a1fb94b5a1c 100644 --- a/package.json +++ b/package.json @@ -99,7 +99,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.34", + "react-native-onyx": "1.0.35", "react-native-pdf": "^6.6.2", "react-native-performance": "^4.0.0", "react-native-permissions": "^3.0.1", From f6916b9616e0ed7c0c5d070cade519bd4e413e41 Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Fri, 13 Jan 2023 13:19:35 -0700 Subject: [PATCH 7/7] Update src/libs/actions/SignInRedirect.js Co-authored-by: Luthfi --- src/libs/actions/SignInRedirect.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libs/actions/SignInRedirect.js b/src/libs/actions/SignInRedirect.js index aaccbf35ef54..c7b1079c04f4 100644 --- a/src/libs/actions/SignInRedirect.js +++ b/src/libs/actions/SignInRedirect.js @@ -5,7 +5,6 @@ import DateUtils from '../DateUtils'; import * as Localize from '../Localize'; import * as PersistedRequests from './PersistedRequests'; import NetworkConnection from '../NetworkConnection'; -import Timing from './Timing'; let currentIsOffline; let currentShouldForceOffline;