* feat(seed): Add modern challenge * chore(react): Use prop-types package * feat: Initial refactor to redux-first-router BREAKING CHANGE: Everything is different! * feat: First rendering * feat(routes): Challenges view render but failing * fix(Challenges): Remove contain HOC * fix(RFR): Add params selector * fix(RFR): :en should be :lang * fix: Update berks utils for redux * fix(Map): Challenge link to arg * fix(Map): Add trailing slash to map page * fix(RFR): Use FCC Link Use fcc Link to get around issue of lang being undefined * fix(Router): Link to is required * fix(app): Rely on RFR state for app lang * chore(RFR): Remove unused RFR Link * fix(RFR): Hydrate initial challenge using RFR and RO * fix: Casing issue * fix(RFR): Undefined links * fix(RFR): Use onRoute<name> convention for route types * feat(server/react): Add helpful redux logging/throwing * fix(server/react): Strip out nonjson from state This prevents thunks in routesMap from breaking serialization * fix(RFR/Link): Should accept any renderable * fix(RFR): Get redirects working * fix(RFR): Redirects and not found's * fix(Map): Move challenge onClick handler * fix(Map): Allow Router.link to handle clicks after onClick * fix(routes): Remove react-router-redux * feat(Router): Add lang to all route actions by default * fix(entities): Only fetch challenge if not already loaded * fix(Files): Move files to own feature * chore(Challenges): Remove vestigial hints logic * fix(RFR): Update challenges on route challenges * fix(code-storage): Should use events instead of commands * fix(Map): ClickOnMap should not hold on to event * chore(lint): Use eslint-config-freecodecamp Closes #15938 * feat(Panes): Update panes on route instead of render * fix(Panes): Store panesmap and update on fetchchallenges * fix(Panes): Normalize panesmaps * fix(Panes): Remove filter from createpanemap * fix(Panes): Middleware on location meta object * feat(Panes): Filter preview on nonhtml challenges * build(babel): Add lodash babel plugin * chore(lint): Lint js files * fix(server/user-stats): Remove use of lodash chain this interferes with babel-plugin-lodash * feat(dev): Add remote redux devtools for ssr * fix(Panes): Dispatch mount action this is needed to trigger window/divider epics * fix(Panes): Getpane to use new panesmap format * fix(Panes): Always update panes after state this lets the panes logic be affected by changes in state
137 lines
4.0 KiB
JavaScript
137 lines
4.0 KiB
JavaScript
import Rx, { Observable, Subject } from 'rx';
|
|
import { ofType } from 'redux-epic';
|
|
/* eslint-disable import/no-unresolved */
|
|
import loopProtect from 'loop-protect';
|
|
/* eslint-enable import/no-unresolved */
|
|
import {
|
|
types,
|
|
|
|
updateOutput,
|
|
checkChallenge,
|
|
updateTests,
|
|
|
|
codeLockedSelector,
|
|
testsSelector
|
|
} from '../../common/app/routes/Challenges/redux';
|
|
|
|
// we use two different frames to make them all essentially pure functions
|
|
// main iframe is responsible rendering the preview and is where we proxy the
|
|
// console.log
|
|
const mainId = 'fcc-main-frame';
|
|
// the test frame is responsible for running the assert tests
|
|
const testId = 'fcc-test-frame';
|
|
|
|
const createHeader = (id = mainId) => `
|
|
<script>
|
|
window.__frameId = '${id}';
|
|
window.onerror = function(msg, url, ln, col, err) {
|
|
window.__err = err;
|
|
return true;
|
|
};
|
|
</script>
|
|
`;
|
|
|
|
|
|
function createFrame(document, id = mainId) {
|
|
const frame = document.createElement('iframe');
|
|
frame.id = id;
|
|
frame.className = 'hide-test-frame';
|
|
document.body.appendChild(frame);
|
|
return frame;
|
|
}
|
|
|
|
function refreshFrame(frame) {
|
|
frame.src = 'about:blank';
|
|
return frame;
|
|
}
|
|
|
|
function getFrameDocument(document, id = mainId) {
|
|
let frame = document.getElementById(id);
|
|
if (!frame) {
|
|
frame = createFrame(document, id);
|
|
}
|
|
frame.contentWindow.loopProtect = loopProtect;
|
|
return {
|
|
frame: frame.contentDocument || frame.contentWindow.document,
|
|
frameWindow: frame.contentWindow
|
|
};
|
|
}
|
|
|
|
function buildProxyConsole(window, proxyLogger) {
|
|
const oldLog = window.console.log.bind(console);
|
|
window.__console = {};
|
|
window.__console.log = function proxyConsole(...args) {
|
|
proxyLogger.onNext(args);
|
|
return oldLog(...args);
|
|
};
|
|
}
|
|
|
|
function frameMain({ build } = {}, document, proxyLogger) {
|
|
const { frame: main, frameWindow } = getFrameDocument(document);
|
|
refreshFrame(main);
|
|
buildProxyConsole(frameWindow, proxyLogger);
|
|
main.Rx = Rx;
|
|
main.open();
|
|
main.write(createHeader() + build);
|
|
main.close();
|
|
}
|
|
|
|
function frameTests({ build, sources, checkChallengePayload } = {}, document) {
|
|
const { frame: tests } = getFrameDocument(document, testId);
|
|
refreshFrame(tests);
|
|
tests.Rx = Rx;
|
|
// default for classic challenges
|
|
// should not be used for modern
|
|
tests.__source = sources['index'] || '';
|
|
tests.__getUserInput = key => sources[key];
|
|
tests.__checkChallengePayload = checkChallengePayload;
|
|
tests.open();
|
|
tests.write(createHeader(testId) + build);
|
|
tests.close();
|
|
}
|
|
|
|
export default function frameEpic(actions, { getState }, { window, document }) {
|
|
// we attach a common place for the iframes to pull in functions from
|
|
// the main process
|
|
window.__common = {};
|
|
window.__common.shouldRun = () => true;
|
|
// this will proxy console.log calls
|
|
const proxyLogger = new Subject();
|
|
// frameReady will let us know when the test iframe is ready to run
|
|
const frameReady = window.__common[testId + 'Ready'] = new Subject();
|
|
const result = actions::ofType(types.frameMain, types.frameTests)
|
|
// if isCodeLocked is true do not frame user code
|
|
.filter(() => !codeLockedSelector(getState()))
|
|
.map(action => {
|
|
if (action.type === types.frameMain) {
|
|
return frameMain(action.payload, document, proxyLogger);
|
|
}
|
|
return frameTests(action.payload, document);
|
|
})
|
|
.ignoreElements();
|
|
|
|
return Observable.merge(
|
|
proxyLogger.map(updateOutput),
|
|
frameReady.flatMap(({ checkChallengePayload }) => {
|
|
const { frame } = getFrameDocument(document, testId);
|
|
const tests = testsSelector(getState());
|
|
const postTests = Observable.of(
|
|
updateOutput('// tests completed'),
|
|
checkChallenge(checkChallengePayload)
|
|
).delay(250);
|
|
// run the tests within the test iframe
|
|
return frame.__runTests(tests)
|
|
.do(tests => {
|
|
tests.forEach(test => {
|
|
if (typeof test.message === 'string') {
|
|
proxyLogger.onNext(test.message);
|
|
}
|
|
});
|
|
})
|
|
.map(updateTests)
|
|
.concat(postTests);
|
|
}),
|
|
result
|
|
);
|
|
}
|