fix(client): pass logs incrementally from test worker
This commit is contained in:
committed by
mrugesh mohapatra
parent
38c5039e00
commit
e8f5b54d63
@ -2,14 +2,38 @@ import chai from 'chai';
|
||||
import '@babel/polyfill';
|
||||
import __toString from 'lodash/toString';
|
||||
|
||||
const oldLog = self.console.log.bind(self.console);
|
||||
self.console.log = function proxyConsole(...args) {
|
||||
self.postMessage({
|
||||
type: 'LOG',
|
||||
data: args.map(arg => JSON.stringify(arg)).join(' ')
|
||||
});
|
||||
return oldLog(...args);
|
||||
};
|
||||
const __utils = (() => {
|
||||
const MAX_LOGS_SIZE = 64 * 1024;
|
||||
|
||||
let logs = [];
|
||||
function flushLogs() {
|
||||
if (logs.length) {
|
||||
self.postMessage({
|
||||
type: 'LOG',
|
||||
data: logs.join('\n')
|
||||
});
|
||||
logs = [];
|
||||
}
|
||||
}
|
||||
|
||||
const oldLog = self.console.log.bind(self.console);
|
||||
self.console.log = function proxyConsole(...args) {
|
||||
logs.push(args.map(arg => JSON.stringify(arg)).join(' '));
|
||||
if (logs.join('\n').length > MAX_LOGS_SIZE) {
|
||||
flushLogs();
|
||||
}
|
||||
return oldLog(...args);
|
||||
};
|
||||
|
||||
function postResult(data) {
|
||||
flushLogs();
|
||||
self.postMessage(data);
|
||||
}
|
||||
|
||||
return {
|
||||
postResult
|
||||
};
|
||||
})();
|
||||
|
||||
self.onmessage = async e => {
|
||||
/* eslint-disable no-unused-vars */
|
||||
@ -38,11 +62,11 @@ self.onmessage = async e => {
|
||||
if (typeof testResult === 'function') {
|
||||
await testResult(fileName => __toString(e.data.sources[fileName]));
|
||||
}
|
||||
self.postMessage({
|
||||
__utils.postResult({
|
||||
pass: true
|
||||
});
|
||||
} catch (err) {
|
||||
self.postMessage({
|
||||
__utils.postResult({
|
||||
err: {
|
||||
message: err.message,
|
||||
stack: err.stack
|
||||
|
@ -215,6 +215,8 @@ export const challengeDataSelector = state => {
|
||||
return challengeData;
|
||||
};
|
||||
|
||||
const MAX_LOGS_SIZE = 64 * 1024;
|
||||
|
||||
export const reducer = handleActions(
|
||||
{
|
||||
[types.fetchIdToNameMapComplete]: (state, { payload }) => ({
|
||||
@ -259,19 +261,17 @@ export const reducer = handleActions(
|
||||
}),
|
||||
[types.initLogs]: state => ({
|
||||
...state,
|
||||
logsOut: []
|
||||
logsOut: ''
|
||||
}),
|
||||
[types.updateLogs]: (state, { payload }) => ({
|
||||
...state,
|
||||
logsOut: [...state.logsOut, payload]
|
||||
logsOut: (state.logsOut + '\n' + payload).slice(-MAX_LOGS_SIZE)
|
||||
}),
|
||||
[types.logsToConsole]: (state, { payload }) => ({
|
||||
...state,
|
||||
consoleOut:
|
||||
state.consoleOut +
|
||||
(state.logsOut.length
|
||||
? '\n' + payload + '\n' + state.logsOut.join('\n')
|
||||
: '')
|
||||
(state.logsOut ? '\n' + payload + '\n' + state.logsOut : '')
|
||||
}),
|
||||
[types.updateChallengeMeta]: (state, { payload }) => ({
|
||||
...state,
|
||||
|
Reference in New Issue
Block a user