Skip to content

Commit

Permalink
Add console mute and unmute methods #35
Browse files Browse the repository at this point in the history
  • Loading branch information
longouyang committed Aug 20, 2016
1 parent bdcaa8c commit 95e12ac
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
9 changes: 7 additions & 2 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,14 @@
console.warn("warning")
console.error("error")

var as = repeat(100, function() { beta(0.001, 0.001) });
var as = repeat(20, function() { beta(0.001, 0.001) });
util.warn("foo");
var bs = repeat(100, function() { beta(0.001, 0.001) });
var bs = repeat(20, function() { beta(0.001, 0.001) });

console.mute();
console.log("this won't show up")
console.unmute();
console.log("this will show up")

3</code></pre>

Expand Down
49 changes: 32 additions & 17 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ var CodeEditor = React.createClass({
var code = comp.refs.editor.getCodeMirror().getValue();
var language = comp.props.language; // TODO: detect this from CodeMirror text

// TODO: make runtime clock dynamic
var runT0;

var endJob = function(store, returnValue) {
Expand Down Expand Up @@ -324,31 +325,45 @@ var CodeEditor = React.createClass({
var lastMessages = {};
var makeConsoleMethod = function(subtype) {
return function() {
var args = _.toArray(arguments);
var message = args.join(' ');
var lastMessage = lastMessages[subtype];

if (lastMessage == message) {
comp.setState(function(state, props) {
// TODO: is mutating state okay?
var idx = _.findLastIndex(state.results,
function(res) { return res.subtype == subtype});
state.results[idx].count += 1;
return state;
})
} else {
comp.addResult({type: 'text', subtype: subtype, message: message, count: 1});
lastMessages[subtype] = message;
nativeConsole[subtype](message);
if (!comp.state.consoleMuted) {

var args = _.toArray(arguments);
var message = args.join(' ');
var lastMessage = lastMessages[subtype];

// if this message is a repeat of the previous one,
// increment a counter on the previous counter rather than
// redundantly printing this new message
if (lastMessage == message) {
comp.setState(function(state, props) {
// TODO: is mutating state okay?
var idx = _.findLastIndex(state.results,
function(res) { return res.subtype == subtype});
state.results[idx].count += 1;
return state;
})
} else {
comp.addResult({type: 'text', subtype: subtype, message: message, count: 1});
lastMessages[subtype] = message;
}

}

nativeConsole[subtype](message);
}
}

global['console'] = {
log: makeConsoleMethod('log'),
info: makeConsoleMethod('info'),
warn: makeConsoleMethod('warn'),
error: makeConsoleMethod('error')
error: makeConsoleMethod('error'),
mute: function() {
comp.setState({consoleMuted: true})
},
unmute: function() {
comp.setState({consoleMuted: false})
}
};

// inject this component's side effect methods into global
Expand Down

0 comments on commit 95e12ac

Please sign in to comment.