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

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

View File

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