Skip to content

Commit

Permalink
Revert "Redux logic errors (#1954)" (#2122)
Browse files Browse the repository at this point in the history
This reverts commit 3314044.
  • Loading branch information
outoftime authored Jan 31, 2020
1 parent cf86adb commit df903da
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 173 deletions.
11 changes: 0 additions & 11 deletions src/logic/__tests__/helpers.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import noop from 'lodash-es/noop';
import reduce from 'lodash-es/reduce';
import {createLogicMiddleware} from 'redux-logic';
import configureStore from 'redux-mock-store';
import {first} from 'rxjs/operators';

import rootReducer from '../../reducers';

export function makeTestLogic(logic) {
return async (action, {state = {}, afterDispatch = noop} = {}) => {
const logicMiddleware = createLogicMiddleware([logic]);
Expand All @@ -28,11 +25,3 @@ export function makeTestLogic(logic) {
return dispatch;
};
}

export function applyActions(...actions) {
return reduce(
actions,
(state, action) => rootReducer(state, action),
undefined,
);
}
13 changes: 12 additions & 1 deletion src/logic/__tests__/startAccountMigration.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import reduce from 'lodash-es/reduce';

import {
accountMigrationComplete,
accountMigrationError,
Expand All @@ -8,10 +10,11 @@ import {
userAuthenticated,
} from '../../actions/user';
import {migrateAccount} from '../../clients/firebase';
import rootReducer from '../../reducers';
import {bugsnagClient} from '../../util/bugsnag';
import startAccountMigration from '../startAccountMigration';

import {applyActions, makeTestLogic} from './helpers';
import {makeTestLogic} from './helpers';

import {
credentialFactory,
Expand Down Expand Up @@ -108,3 +111,11 @@ describe('startAccountMigration', () => {
expect(migrateAccount).not.toHaveBeenCalledWith(mockCredential);
});
});

function applyActions(...actions) {
return reduce(
actions,
(state, action) => rootReducer(state, action),
undefined,
);
}
78 changes: 0 additions & 78 deletions src/logic/__tests__/validateProject.test.js

This file was deleted.

6 changes: 2 additions & 4 deletions src/logic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ import projectSuccessfullySaved from './projectSuccessfullySaved';
import saveProject from './saveProject';
import startAccountMigration from './startAccountMigration';
import unlinkGithubIdentity from './unlinkGithubIdentity';
import validateProject from './validateProject';

export default [
instrumentApplicationLoaded,
instrumentEnvironmentReady,
linkGithubIdentity,
logout,
projectSuccessfullySaved,
saveProject,
startAccountMigration,
unlinkGithubIdentity,
validateProject,
projectSuccessfullySaved,
saveProject,
];
79 changes: 0 additions & 79 deletions src/logic/validateProject.js

This file was deleted.

80 changes: 80 additions & 0 deletions src/sagas/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import {
all,
call,
cancel,
fork,
join,
put,
select,
takeEvery,
} from 'redux-saga/effects';
import Analyzer from '../analyzers';
import {getCurrentProject} from '../selectors';
import {validatedSource} from '../actions/errors';
import retryingFailedImports from '../util/retryingFailedImports';

export async function importValidations() {
return retryingFailedImports(() =>
import(
/* webpackChunkName: "mainAsync" */
'../validations'
),
);
}

export function* toggleLibrary(tasks) {
yield call(validateCurrentProject, tasks);
}

export function* updateProjectSource(tasks, {payload: {language, newValue}}) {
const state = yield select();
const analyzer = new Analyzer(getCurrentProject(state));
yield call(validateSource, tasks, {
payload: {language, source: newValue, projectAttributes: analyzer},
});
}

export function* validateCurrentProject(tasks) {
const state = yield select();
const currentProject = getCurrentProject(state);
const analyzer = new Analyzer(currentProject);

for (const language of Reflect.ownKeys(currentProject.sources)) {
const source = currentProject.sources[language];
yield fork(validateSource, tasks, {
payload: {language, source, projectAttributes: analyzer},
});
}
}

export function* validateSource(
tasks,
{payload: {language, source, projectAttributes}},
) {
if (tasks.has(language)) {
yield cancel(tasks.get(language));
}
const validations = yield call(importValidations);
const task = yield fork(validations[language], source, projectAttributes);
tasks.set(language, task);
const validationErrors = yield join(task);
tasks.delete(language);
yield put(validatedSource(language, validationErrors));
}

export default function* errors() {
const tasks = new Map();

yield all([
takeEvery('CHANGE_CURRENT_PROJECT', validateCurrentProject, tasks),
takeEvery('GIST_IMPORTED', validateCurrentProject, tasks),
takeEvery('SNAPSHOT_IMPORTED', validateCurrentProject, tasks),
takeEvery(
'PROJECT_RESTORED_FROM_LAST_SESSION',
validateCurrentProject,
tasks,
),
takeEvery('UPDATE_PROJECT_SOURCE', updateProjectSource, tasks),
takeEvery('TOGGLE_LIBRARY', toggleLibrary, tasks),
]);
}
2 changes: 2 additions & 0 deletions src/sagas/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {all} from 'redux-saga/effects';

import manageUserState from './manageUserState';
import watchErrors from './errors';
import watchProjects from './projects';
import watchUi from './ui';
import watchClients from './clients';
Expand All @@ -9,6 +10,7 @@ import watchCompiledProjects from './compiledProjects';
export default function* rootSaga() {
yield all([
manageUserState(),
watchErrors(),
watchProjects(),
watchUi(),
watchClients(),
Expand Down
Loading

0 comments on commit df903da

Please sign in to comment.