change console throttling to batch deliver messages

This commit is contained in:
Cassie Tarakajian 2016-10-06 16:58:14 -04:00
parent fe13349850
commit e511db64fe
4 changed files with 31 additions and 44 deletions

View file

@ -8,7 +8,7 @@ import classNames from 'classnames';
* How many console messages to store * How many console messages to store
* @type {Number} * @type {Number}
*/ */
const consoleMax = 100; const consoleMax = 200;
class Console extends React.Component { class Console extends React.Component {
@ -27,18 +27,22 @@ class Console extends React.Component {
if (nextProps.isPlaying && !this.props.isPlaying) { if (nextProps.isPlaying && !this.props.isPlaying) {
this.children = []; this.children = [];
} else if (nextProps.consoleEvent !== this.props.consoleEvent && this.props.isPlaying) { } else if (nextProps.consoleEvent !== this.props.consoleEvent && this.props.isPlaying) {
const args = nextProps.consoleEvent.arguments; nextProps.consoleEvent.forEach(consoleEvent => {
if (consoleEvent.source === 'sketch') {
const args = consoleEvent.arguments;
Object.keys(args).forEach((key) => { Object.keys(args).forEach((key) => {
if (args[key].includes('Exiting potential infinite loop')) { if (args[key].includes('Exiting potential infinite loop')) {
this.props.stopSketch(); this.props.stopSketch();
this.props.expandConsole(); this.props.expandConsole();
this.appendConsoleEvent(nextProps.consoleEvent); this.appendConsoleEvent(consoleEvent);
} }
}); });
if (nextProps.isExpanded) { if (nextProps.isExpanded) {
this.appendConsoleEvent(nextProps.consoleEvent); this.appendConsoleEvent(consoleEvent);
} }
} }
});
}
} }
shouldComponentUpdate(nextProps) { shouldComponentUpdate(nextProps) {
@ -90,7 +94,7 @@ class Console extends React.Component {
} }
Console.propTypes = { Console.propTypes = {
consoleEvent: PropTypes.object, consoleEvent: PropTypes.array,
isPlaying: PropTypes.bool.isRequired, isPlaying: PropTypes.bool.isRequired,
isExpanded: PropTypes.bool.isRequired, isExpanded: PropTypes.bool.isRequired,
collapseConsole: PropTypes.func.isRequired, collapseConsole: PropTypes.func.isRequired,

View file

@ -42,29 +42,11 @@ function hijackConsoleLogsScript() {
'debug', 'clear', 'error', 'info', 'log', 'warn' 'debug', 'clear', 'error', 'info', 'log', 'warn'
]; ];
var consoleBuffer = [];
function throttle(fn, threshhold, scope) { var LOGWAIT = 500;
var last, deferTimer;
return function() {
var context = scope || this;
var now = +new Date,
args = arguments;
if (last && now < last + threshhold) {
// hold on to it
clearTimeout(deferTimer);
deferTimer = setTimeout(function() {
last = now;
fn.apply(context, args);
}, threshhold);
} else {
last = now;
fn.apply(context, args);
}
};
}
methods.forEach( function(method) { methods.forEach( function(method) {
iframeWindow.console[method] = throttle(function() { iframeWindow.console[method] = function() {
originalConsole[method].apply(originalConsole, arguments); originalConsole[method].apply(originalConsole, arguments);
var args = Array.from(arguments); var args = Array.from(arguments);
@ -73,14 +55,20 @@ function hijackConsoleLogsScript() {
return (typeof i === 'string') ? i : JSON.stringify(i); return (typeof i === 'string') ? i : JSON.stringify(i);
}); });
// post message to parent window consoleBuffer.push({
window.parent.postMessage({
method: method, method: method,
arguments: args, arguments: args,
source: 'sketch' source: 'sketch'
}, '*');
}, 100);
}); });
};
});
setInterval(function() {
if (consoleBuffer.length > 0) {
window.parent.postMessage(consoleBuffer, '*');
consoleBuffer.length = 0;
}
}, LOGWAIT);
</script>`; </script>`;
return s; return s;
} }
@ -133,9 +121,7 @@ class PreviewFrame extends React.Component {
if (this.props.dispatchConsoleEvent) { if (this.props.dispatchConsoleEvent) {
window.addEventListener('message', (msg) => { window.addEventListener('message', (msg) => {
if (msg.data.source === 'sketch') {
this.props.dispatchConsoleEvent(msg); this.props.dispatchConsoleEvent(msg);
}
}); });
} }
} }

View file

@ -415,7 +415,7 @@ IDEView.propTypes = {
ide: PropTypes.shape({ ide: PropTypes.shape({
isPlaying: PropTypes.bool.isRequired, isPlaying: PropTypes.bool.isRequired,
isTextOutputPlaying: PropTypes.bool.isRequired, isTextOutputPlaying: PropTypes.bool.isRequired,
consoleEvent: PropTypes.object, consoleEvent: PropTypes.array,
modalIsVisible: PropTypes.bool.isRequired, modalIsVisible: PropTypes.bool.isRequired,
sidebarIsExpanded: PropTypes.bool.isRequired, sidebarIsExpanded: PropTypes.bool.isRequired,
consoleIsExpanded: PropTypes.bool.isRequired, consoleIsExpanded: PropTypes.bool.isRequired,

View file

@ -3,10 +3,7 @@ import * as ActionTypes from '../../../constants';
const initialState = { const initialState = {
isPlaying: false, isPlaying: false,
isTextOutputPlaying: false, isTextOutputPlaying: false,
consoleEvent: { consoleEvent: [],
method: undefined,
arguments: []
},
modalIsVisible: false, modalIsVisible: false,
sidebarIsExpanded: false, sidebarIsExpanded: false,
consoleIsExpanded: false, consoleIsExpanded: false,