Files
freeCodeCamp/client/iFrameScripts.js

71 lines
2.0 KiB
JavaScript
Raw Normal View History

2016-05-20 12:42:26 -07:00
document.addEventListener('DOMContentLoaded', function() {
var common = parent.__common;
2015-11-22 19:42:53 -08:00
var Rx = parent.Rx;
2015-11-30 14:27:39 -08:00
2016-05-20 12:42:26 -07:00
common.getJsOutput = function evalJs(source = '') {
2015-12-03 15:09:52 -08:00
if (window.__err || !common.shouldRun()) {
2016-05-20 12:42:26 -07:00
return window.__err || 'source disabled';
2015-12-03 15:09:52 -08:00
}
2015-11-30 14:27:39 -08:00
let output;
try {
/* eslint-disable no-eval */
2016-05-20 12:42:26 -07:00
output = eval(source);
2015-11-30 14:27:39 -08:00
/* eslint-enable no-eval */
} catch (e) {
window.__err = e;
}
return output;
};
2016-05-20 12:42:26 -07:00
common.runTests$ = function runTests$({ tests = [], source }) {
const editor = { getValue() { return source; } };
if (window.__err) {
return Rx.Observable.throw(window.__err);
}
2015-11-30 14:27:39 -08:00
2016-05-20 12:42:26 -07:00
// Iterate through the test one at a time
// on new stacks
return Rx.Observable.from(tests, null, null, Rx.Scheduler.default)
// add delay here for firefox to catch up
.delay(100)
.map(({ text, testString }) => {
const newTest = { text, testString };
let test;
try {
/* eslint-disable no-eval */
test = eval(testString);
/* eslint-enable no-eval */
if (typeof test === 'function') {
// maybe sync/promise/observable
if (test.length === 0) {
test();
}
// callback test
if (test.length === 1) {
console.log('callback test');
}
2015-11-22 19:42:53 -08:00
}
2016-05-20 12:42:26 -07:00
} catch (e) {
newTest.err = e.message.split(':').shift();
}
return newTest;
})
// gather tests back into an array
.toArray();
};
2015-11-22 19:42:53 -08:00
2015-12-02 14:56:06 -08:00
// used when updating preview without running tests
common.checkPreview$ = function checkPreview$(args) {
if (window.__err) {
return Rx.Observable.throw(window.__err);
}
return Rx.Observable.just(args);
};
2015-12-01 08:01:51 -08:00
// now that the runPreviewTest$ is defined
// we set the subject to true
// this will let the updatePreview
// script now that we are ready.
2016-05-20 12:42:26 -07:00
common.testFrameReady$.onNext(true);
2015-11-13 11:10:23 -08:00
});