fix(client): remove a dependency on the store from build challenges

pull/27100/head^2
Valeriy 2019-01-12 03:37:00 +03:00 committed by Stuart Taylor
parent 02f3714735
commit 7bfc733da6
2 changed files with 24 additions and 26 deletions

View File

@ -10,6 +10,8 @@ import {
import { delay, channel } from 'redux-saga';
import {
backendFormValuesSelector,
challengeFilesSelector,
challengeMetaSelector,
challengeTestsSelector,
initConsole,
@ -46,19 +48,17 @@ export function* executeChallengeSaga() {
yield fork(logToConsole, consoleProxy);
const proxyLogger = args => consoleProxy.put(args);
const state = yield select();
let testResults;
switch (challengeType) {
case js:
case bonfire:
testResults = yield executeJSChallengeSaga(state, proxyLogger);
testResults = yield executeJSChallengeSaga(proxyLogger);
break;
case backend:
testResults = yield executeBackendChallengeSaga(state, proxyLogger);
testResults = yield executeBackendChallengeSaga(proxyLogger);
break;
default:
testResults = yield executeDOMChallengeSaga(state, proxyLogger);
testResults = yield executeDOMChallengeSaga(proxyLogger);
}
yield put(updateTests(testResults));
@ -77,8 +77,9 @@ function* logToConsole(channel) {
});
}
function* executeJSChallengeSaga(state, proxyLogger) {
const { build, sources } = yield call(buildJSChallenge, state);
function* executeJSChallengeSaga(proxyLogger) {
const files = yield select(challengeFilesSelector);
const { build, sources } = yield call(buildJSChallenge, files);
const code = sources && 'index' in sources ? sources['index'] : '';
const testWorker = createWorker('test-evaluator');
@ -106,9 +107,11 @@ function createTestFrame(document, ctx, proxyLogger) {
);
}
function* executeDOMChallengeSaga(state, proxyLogger) {
function* executeDOMChallengeSaga(proxyLogger) {
const files = yield select(challengeFilesSelector);
const meta = yield select(challengeMetaSelector);
const document = yield getContext('document');
const ctx = yield call(buildDOMChallenge, state);
const ctx = yield call(buildDOMChallenge, files, meta);
yield call(createTestFrame, document, ctx, proxyLogger);
// wait for a code execution on a "ready" event in jQuery challenges
yield delay(100);
@ -119,9 +122,10 @@ function* executeDOMChallengeSaga(state, proxyLogger) {
}
// TODO: use a web worker
function* executeBackendChallengeSaga(state, proxyLogger) {
function* executeBackendChallengeSaga(proxyLogger) {
const formValues = yield select(backendFormValuesSelector);
const document = yield getContext('document');
const ctx = yield call(buildBackendChallenge, state);
const ctx = yield call(buildBackendChallenge, formValues);
yield call(createTestFrame, document, ctx, proxyLogger);
return yield call(executeTests, (testString, testTimeout) =>
@ -163,12 +167,13 @@ function* executeTests(testRunner) {
function* updateMainSaga() {
try {
const { html, modern } = challengeTypes;
const { challengeType } = yield select(challengeMetaSelector);
const meta = yield select(challengeMetaSelector);
const { challengeType } = meta;
if (challengeType !== html && challengeType !== modern) {
return;
}
const state = yield select();
const ctx = yield call(buildDOMChallenge, state);
const files = yield select(challengeFilesSelector);
const ctx = yield call(buildDOMChallenge, files, meta);
const document = yield getContext('document');
const frameMain = yield call(createMainFramer, document);
yield call(frameMain, ctx);

View File

@ -1,9 +1,4 @@
import { throwers } from '../rechallenge/throwers';
import {
challengeFilesSelector,
challengeMetaSelector,
backendFormValuesSelector
} from '../redux';
import { transformers } from '../rechallenge/transformers';
import { cssToHtml, jsToHtml, concatHtml } from '../rechallenge/builders.js';
@ -56,9 +51,8 @@ function checkFilesErrors(files) {
return files;
}
export function buildDOMChallenge(state) {
const files = challengeFilesSelector(state);
const { required = [], template } = challengeMetaSelector(state);
export function buildDOMChallenge(files, meta = {}) {
const { required = [], template = '' } = meta;
const finalRequires = [...globalRequires, ...required, ...frameRunner];
const toHtml = [jsToHtml, cssToHtml];
const pipeLine = composeFunctions(...throwers, ...transformers, ...toHtml);
@ -73,8 +67,7 @@ export function buildDOMChallenge(state) {
}));
}
export function buildJSChallenge(state) {
const files = challengeFilesSelector(state);
export function buildJSChallenge(files) {
const pipeLine = composeFunctions(...throwers, ...transformers);
const finalFiles = Object.keys(files)
.map(key => files[key])
@ -92,10 +85,10 @@ export function buildJSChallenge(state) {
}));
}
export function buildBackendChallenge(state) {
export function buildBackendChallenge(formValues) {
const {
solution: { value: url }
} = backendFormValuesSelector(state);
} = formValues;
return {
build: concatHtml({ required: frameRunner }),
sources: { url }