Files
freeCodeCamp/packages/learn/src/templates/Challenges/redux/execute-challenge-epic.js

117 lines
3.5 KiB
JavaScript
Raw Normal View History

import { Subject } from 'rxjs';
import { merge } from 'rxjs/observable/merge';
import { of } from 'rxjs/observable/of';
import { from } from 'rxjs/observable/from';
2018-04-06 14:51:52 +01:00
import {
debounceTime,
switchMap,
map,
filter,
pluck,
concat
} from 'rxjs/operators';
import { ofType, combineEpics } from 'redux-observable';
import _ from 'lodash';
import {
types,
challengeMetaSelector,
2018-04-06 14:51:52 +01:00
challengeTestsSelector,
initConsole,
updateConsole,
checkChallenge,
updateTests,
disableJSOnError
2018-04-06 14:51:52 +01:00
} from './';
import { buildFromFiles, buildBackendChallenge } from '../utils/build';
2018-04-06 14:51:52 +01:00
import {
runTestsInTestFrame,
createTestFramer,
createMainFramer
} from '../utils/frame.js';
import { backend } from '../../../../utils/challengeTypes';
2018-04-06 14:51:52 +01:00
const executeDebounceTimeout = 750;
function updateMainEpic(actions, { getState }, { document }) {
return of(document).pipe(
2018-04-06 14:51:52 +01:00
filter(Boolean),
switchMap(() => {
const proxyLogger = new Subject();
const frameMain = createMainFramer(document, getState, proxyLogger);
const buildAndFrameMain = actions.pipe(
ofType(types.updateFile, types.executeChallenge),
debounceTime(executeDebounceTimeout),
switchMap(() =>
buildFromFiles(getState(), true)
.map(frameMain)
.ignoreElements()
2018-04-12 16:07:21 +01:00
.catch(() => of({ type: 'NULL' }))
2018-04-06 14:51:52 +01:00
)
);
2018-04-12 16:07:21 +01:00
return merge(buildAndFrameMain, proxyLogger.map(updateConsole));
2018-04-06 14:51:52 +01:00
})
);
}
function executeChallengeEpic(action$, { getState }, { document }) {
return of(document).pipe(
2018-04-06 14:51:52 +01:00
filter(Boolean),
switchMap(() => {
const frameReady = new Subject();
const frameTests = createTestFramer(document, getState, frameReady);
const challengeResults = frameReady.pipe(
pluck('checkChallengePayload'),
map(checkChallengePayload => ({
checkChallengePayload,
tests: challengeTestsSelector(getState())
})),
switchMap(({ checkChallengePayload, tests }) => {
const postTests = of(
2018-04-06 14:51:52 +01:00
updateConsole('// tests completed'),
checkChallenge(checkChallengePayload)
).delay(250);
// run the tests within the test iframe
return runTestsInTestFrame(document, tests)
.flatMap(tests => {
return from(tests).pipe(
2018-04-06 14:51:52 +01:00
map(({ message }) => message),
filter(_.overEvery(_.isString, Boolean)),
map(updateConsole),
concat(of(updateTests(tests)))
2018-04-06 14:51:52 +01:00
);
})
.concat(postTests);
})
);
const buildAndFrameChallenge = action$.pipe(
ofType(types.executeChallenge),
debounceTime(executeDebounceTimeout),
// if isCodeLocked do not run challenges
// .filter(() => !codeLockedSelector(getState()))
switchMap(() => {
const state = getState();
const { challengeType } = challengeMetaSelector(state);
if (challengeType === backend) {
return buildBackendChallenge(state)
.do(frameTests)
.ignoreElements()
.startWith(initConsole('// running test'))
.catch(err => disableJSOnError(err));
}
2018-04-06 14:51:52 +01:00
return buildFromFiles(state, false)
.do(frameTests)
.ignoreElements()
.startWith(initConsole('// running test'))
.catch(err => disableJSOnError(err));
2018-04-06 14:51:52 +01:00
})
);
return merge(buildAndFrameChallenge, challengeResults);
2018-04-06 14:51:52 +01:00
})
);
}
export default combineEpics(updateMainEpic, executeChallengeEpic);