2018-04-11 15:10:48 +01:00
|
|
|
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,
|
2018-05-26 21:32:40 +01:00
|
|
|
concat,
|
|
|
|
tap,
|
|
|
|
catchError,
|
|
|
|
ignoreElements,
|
|
|
|
startWith,
|
|
|
|
delay
|
2018-04-06 14:51:52 +01:00
|
|
|
} from 'rxjs/operators';
|
|
|
|
import { ofType, combineEpics } from 'redux-observable';
|
2018-05-18 14:54:21 +01:00
|
|
|
import { overEvery, isString } from 'lodash';
|
2018-04-06 14:51:52 +01:00
|
|
|
|
|
|
|
import {
|
|
|
|
types,
|
2018-04-17 11:33:04 +01:00
|
|
|
challengeMetaSelector,
|
2018-04-06 14:51:52 +01:00
|
|
|
challengeTestsSelector,
|
|
|
|
initConsole,
|
|
|
|
updateConsole,
|
|
|
|
checkChallenge,
|
2018-04-13 16:45:21 +01:00
|
|
|
updateTests,
|
2018-05-26 21:32:40 +01:00
|
|
|
disableJSOnError,
|
|
|
|
isJSEnabledSelector
|
2018-04-06 14:51:52 +01:00
|
|
|
} from './';
|
2018-04-17 11:33:04 +01:00
|
|
|
import { buildFromFiles, buildBackendChallenge } from '../utils/build';
|
2018-04-06 14:51:52 +01:00
|
|
|
import {
|
|
|
|
runTestsInTestFrame,
|
|
|
|
createTestFramer,
|
|
|
|
createMainFramer
|
|
|
|
} from '../utils/frame.js';
|
|
|
|
|
2018-04-17 11:33:04 +01:00
|
|
|
import { backend } from '../../../../utils/challengeTypes';
|
|
|
|
|
2018-04-06 14:51:52 +01:00
|
|
|
const executeDebounceTimeout = 750;
|
|
|
|
|
|
|
|
function updateMainEpic(actions, { getState }, { document }) {
|
2018-04-11 15:10:48 +01:00
|
|
|
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(
|
2018-05-19 22:11:30 +01:00
|
|
|
ofType(
|
|
|
|
types.updateFile,
|
|
|
|
types.challengeMounted
|
|
|
|
),
|
2018-04-06 14:51:52 +01:00
|
|
|
debounceTime(executeDebounceTimeout),
|
|
|
|
switchMap(() =>
|
2018-05-26 21:32:40 +01:00
|
|
|
buildFromFiles(getState(), true).pipe(
|
|
|
|
map(frameMain),
|
|
|
|
ignoreElements(),
|
2018-07-04 16:38:42 +03:00
|
|
|
startWith(initConsole('')),
|
2018-05-26 21:32:40 +01:00
|
|
|
catchError(err => of(disableJSOnError(err)))
|
|
|
|
)
|
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 }) {
|
2018-04-11 15:10:48 +01:00
|
|
|
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 }) => {
|
2018-04-11 15:10:48 +01:00
|
|
|
const postTests = of(
|
2018-04-06 14:51:52 +01:00
|
|
|
updateConsole('// tests completed'),
|
|
|
|
checkChallenge(checkChallengePayload)
|
2018-05-26 21:32:40 +01:00
|
|
|
).pipe(delay(250));
|
|
|
|
return runTestsInTestFrame(document, tests).pipe(
|
|
|
|
switchMap(tests => {
|
2018-04-11 15:10:48 +01:00
|
|
|
return from(tests).pipe(
|
2018-04-06 14:51:52 +01:00
|
|
|
map(({ message }) => message),
|
2018-05-18 14:54:21 +01:00
|
|
|
filter(overEvery(isString, Boolean)),
|
2018-04-06 14:51:52 +01:00
|
|
|
map(updateConsole),
|
2018-04-11 15:10:48 +01:00
|
|
|
concat(of(updateTests(tests)))
|
2018-04-06 14:51:52 +01:00
|
|
|
);
|
2018-05-26 21:32:40 +01:00
|
|
|
}),
|
|
|
|
concat(postTests)
|
|
|
|
);
|
2018-04-06 14:51:52 +01:00
|
|
|
})
|
|
|
|
);
|
|
|
|
const buildAndFrameChallenge = action$.pipe(
|
|
|
|
ofType(types.executeChallenge),
|
|
|
|
debounceTime(executeDebounceTimeout),
|
2018-05-26 21:32:40 +01:00
|
|
|
filter(() => isJSEnabledSelector(getState())),
|
2018-04-06 14:51:52 +01:00
|
|
|
switchMap(() => {
|
|
|
|
const state = getState();
|
2018-04-17 11:33:04 +01:00
|
|
|
const { challengeType } = challengeMetaSelector(state);
|
|
|
|
if (challengeType === backend) {
|
2018-05-26 21:32:40 +01:00
|
|
|
return buildBackendChallenge(state).pipe(
|
|
|
|
tap(frameTests),
|
|
|
|
ignoreElements(),
|
|
|
|
startWith(initConsole('// running test')),
|
|
|
|
catchError(err => of(disableJSOnError(err)))
|
|
|
|
);
|
2018-04-17 11:33:04 +01:00
|
|
|
}
|
2018-05-26 21:32:40 +01:00
|
|
|
return buildFromFiles(state, false).pipe(
|
|
|
|
tap(frameTests),
|
|
|
|
ignoreElements(),
|
|
|
|
startWith(initConsole('// running test')),
|
|
|
|
catchError(err => of(disableJSOnError(err)))
|
|
|
|
);
|
2018-04-06 14:51:52 +01:00
|
|
|
})
|
|
|
|
);
|
2018-04-11 15:10:48 +01:00
|
|
|
return merge(buildAndFrameChallenge, challengeResults);
|
2018-04-06 14:51:52 +01:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default combineEpics(updateMainEpic, executeChallengeEpic);
|