diff --git a/app/mattermost.js b/app/mattermost.js index 6c9c9f5df..738c3b576 100644 --- a/app/mattermost.js +++ b/app/mattermost.js @@ -16,6 +16,7 @@ import 'app/init/fetch'; import globalEventHandler from 'app/init/global_event_handler'; import {registerScreens} from 'app/screens'; import store from 'app/store'; +import {waitForHydration} from 'app/store/utils'; import EphemeralStore from 'app/store/ephemeral_store'; import telemetry from 'app/telemetry'; import pushNotificationsUtils from 'app/utils/push_notifications'; @@ -54,6 +55,7 @@ const launchApp = async (credentials) => { ]); if (credentials) { + await waitForHydration(store); store.dispatch(loadMe()); resetToChannel({skipMetrics: true}); } else { diff --git a/app/store/utils.js b/app/store/utils.js index fcc67d31f..bd94cc917 100644 --- a/app/store/utils.js +++ b/app/store/utils.js @@ -40,3 +40,25 @@ export function transformSet(incoming, setTransforms, toStorage = true) { return state; } + +export function waitForHydration(store, callback) { + let unsubscribeFromStore; + return new Promise((resolve) => { + if (__DEV__) { + // when in DEV mode resetting the app can get into a white screen + resolve(); + return; + } + const subscription = () => { + if (store.getState().views.root.hydrationComplete) { + unsubscribeFromStore(); + if (callback && typeof callback === 'function') { + callback(); + } + resolve(); + } + }; + + unsubscribeFromStore = store.subscribe(subscription); + }); +} \ No newline at end of file diff --git a/app/utils/push_notifications.js b/app/utils/push_notifications.js index 6d8b480ea..2bb1e26d3 100644 --- a/app/utils/push_notifications.js +++ b/app/utils/push_notifications.js @@ -23,6 +23,7 @@ import {getCurrentServerUrl, getAppCredentials} from 'app/init/credentials'; import PushNotifications from 'app/push_notifications'; import {getCurrentLocale} from 'app/selectors/i18n'; import EphemeralStore from 'app/store/ephemeral_store'; +import {waitForHydration} from 'app/store/utils'; import {t} from 'app/utils/i18n'; class PushNotificationUtils { @@ -64,9 +65,6 @@ class PushNotificationUtils { onPushNotification = async (deviceNotification) => { const {dispatch, getState} = this.store; - let unsubscribeFromStore = null; - let stopLoadingNotification = false; - const {data, foreground, message, userInteraction} = deviceNotification; const notification = { data, @@ -88,15 +86,9 @@ class PushNotificationUtils { this.loadFromNotification(notification); }, 0); } else { - const waitForHydration = () => { - if (getState().views.root.hydrationComplete && !stopLoadingNotification) { - stopLoadingNotification = true; - unsubscribeFromStore(); - this.loadFromNotification(notification); - } - }; - - unsubscribeFromStore = this.store.subscribe(waitForHydration); + waitForHydration(this.store, () => { + this.loadFromNotification(notification); + }); } } } @@ -164,8 +156,7 @@ class PushNotificationUtils { }; onRegisterDevice = (data) => { - const {dispatch, getState} = this.store; - let unsubscribeFromStore = null; + const {dispatch} = this.store; let prefix; if (Platform.OS === 'ios') { @@ -180,15 +171,10 @@ class PushNotificationUtils { EphemeralStore.deviceToken = `${prefix}:${data.token}`; // TODO: Remove when realm is ready - const waitForHydration = () => { - if (getState().views.root.hydrationComplete && !this.configured) { - this.configured = true; - dispatch(setDeviceToken(EphemeralStore.deviceToken)); - unsubscribeFromStore(); - } - }; - - unsubscribeFromStore = this.store.subscribe(waitForHydration); + waitForHydration(this.store, () => { + this.configured = true; + dispatch(setDeviceToken(EphemeralStore.deviceToken)); + }); }; getNotification = () => {