Skip to content

Commit

Permalink
#1048 - avoid calling dispatch within a dispatch during dump restore
Browse files Browse the repository at this point in the history
  • Loading branch information
petmongrels committed Oct 18, 2023
1 parent 0bd1901 commit 8d29f53
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
1 change: 1 addition & 0 deletions packages/openchs-android/index.android.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* @flow */
import {AppRegistry} from 'react-native';
// import App from './integrationTest/RealmIssuesApp';
// import App from "./integrationTest/IntegrationTestApp";
import App from "./src/Avni";
AppRegistry.registerComponent('Avni', () => App);
26 changes: 14 additions & 12 deletions packages/openchs-android/src/action/LoginActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ import BackupRestoreRealmService from "../service/BackupRestoreRealm";
import SettingsService from "../service/SettingsService";
import { IDP_PROVIDERS } from "../model/IdpProviders";

function restoreDump(context, action, source, successCb) {
const restoreService = context.get(BackupRestoreRealmService);
restoreService.restore((percentProgress, message, failed = false, failureMessage) => {
if (failed) action.checkForRetry(failureMessage, source);
else if (percentProgress === 100) successCb(source);
else action.onLoginProgress(percentProgress, message);
});
}

class LoginActions {
static getInitialState() {
return {
Expand Down Expand Up @@ -82,21 +91,12 @@ class LoginActions {
const doRestoreDump = backupRestoreRealmService.isDatabaseNeverSynced();
General.logInfo("LoginActions", `Dump restore can be done = ${doRestoreDump}`);
if (doRestoreDump) {
LoginActions.restoreDump(context, action, source, successCb);
restoreDump(context, action, source, successCb);
} else {
successCb(source);
}
}

static restoreDump(context, action, source, successCb) {
let restoreService = context.get(BackupRestoreRealmService);
restoreService.restore((percentProgress, message, failed = false, failureMessage) => {
if (failed) action.checkForRetry(failureMessage, source);
else if (percentProgress === 100) successCb(source);
else action.onLoginProgress(percentProgress, message);
});
}

static onStateChange(state, action) {
return _.assignIn({}, state, action.newState);
}
Expand All @@ -117,14 +117,16 @@ class LoginActions {

static onDumpRestoreRetry(state, action, context) {
let newState = _.assignIn({}, state, {percentProgress: 0});
LoginActions.restoreDump(context, action, action.source);
//restore dump calls dispatch internally
setTimeout(() => {
restoreDump(context, action, action.source);
}, 1);
return newState;
}

static onUserToggleIdp(state) {
return _.assignIn({}, state, {userSelectedIdp: state.userSelectedIdp === IDP_PROVIDERS.COGNITO ? IDP_PROVIDERS.KEYCLOAK: IDP_PROVIDERS.COGNITO});
}

}

const LoginActionsNames = {
Expand Down
2 changes: 1 addition & 1 deletion packages/openchs-android/src/views/LoginView.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ class LoginView extends AbstractComponent {
}

onLoginProgress(percentProgress, message) {
General.logDebug('LoginView', message);
General.logDebug('LoginView', `onLoginProgress ${message}`);
this.dispatchAction(Actions.ON_DUMP_RESTORING, {percentProgress: percentProgress, message: message});
}

Expand Down

0 comments on commit 8d29f53

Please sign in to comment.