Skip to content

Commit

Permalink
Catch all errors in our CM event handlers so (I hope) we can't ever b…
Browse files Browse the repository at this point in the history
…reak

other code handling CM events, such as Firepad as happened in
cben/mathdown#85.
  • Loading branch information
cben committed Apr 30, 2015
1 parent 4ec4f34 commit 700f42f
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions render-math.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ CodeMirror.hookMath = function(editor, MathJax) {
return Function.prototype.bind.call(console[logMethod], console,
formatDuration(timestampMs() - t0));
} catch(err) {
return function() {
return function(var_args) {
try {
var args = Array.prototype.slice.call(arguments, 0);
args.unshift(formatDuration(timestampMs() - t0));
Expand All @@ -73,7 +73,7 @@ CodeMirror.hookMath = function(editor, MathJax) {

// Log time if non-negligible.
function logFuncTime(func) {
return function() {
return function(var_args) {
var start = timestampMs();
func.apply(this, arguments);
var duration = timestampMs() - start;
Expand All @@ -83,6 +83,16 @@ CodeMirror.hookMath = function(editor, MathJax) {
};
}

function catchAllErrors(func) {
return function(var_args) {
try {
return func.apply(this, arguments);
} catch(err) {
errorf()("catching error:", err);
}
}
}

// Position arithmetic
// -------------------

Expand Down Expand Up @@ -141,9 +151,10 @@ CodeMirror.hookMath = function(editor, MathJax) {
mark.clear();
}

editor.on("cursorActivity", function(doc) {
editor.on("cursorActivity", catchAllErrors(function(doc) {
if(unrenderedMath !== null) {
// TODO: selection behavior?
// TODO: handle multiple cursors/selections
var cursor = doc.getCursor();
var unrenderedRange = unrenderedMath.find();
if(unrenderedRange === undefined) {
Expand All @@ -160,7 +171,7 @@ CodeMirror.hookMath = function(editor, MathJax) {
flushMarkTextQueue();
}
}
});
}));

// Rendering on changes
// --------------------
Expand Down Expand Up @@ -230,9 +241,9 @@ CodeMirror.hookMath = function(editor, MathJax) {
var mark = doc.markText(from, to, {replacedWith: elem,
clearOnEnter: false});
mark.xMathState = "rendered"; // helps later remove only our marks.
CodeMirror.on(mark, "beforeCursorEnter", function() {
CodeMirror.on(mark, "beforeCursorEnter", catchAllErrors(function() {
unrenderMark(mark);
});
}));
});
}
});
Expand Down Expand Up @@ -282,8 +293,10 @@ CodeMirror.hookMath = function(editor, MathJax) {
}
}

// Documents don't batch "change" events, so should never have .next.
CodeMirror.on(doc, "change", logFuncTime(function processChange(doc, changeObj) {
// CM < 4 batched editor's "change" events via a .next property, which we'd
// have to chase - and what's worse, adjust all coordinates.
// Documents' "change" events were never batched, so not a problem.
CodeMirror.on(doc, "change", catchAllErrors(logFuncTime(function processChange(doc, changeObj) {
logf()("change", changeObj);
// changeObj.{from,to} are pre-change coordinates; adding text.length
// (number of inserted lines) is a conservative(?) fix.
Expand All @@ -296,7 +309,7 @@ CodeMirror.hookMath = function(editor, MathJax) {
processChange(changeObj.next);
}
MathJax.Hub.Queue(flushMarkTextQueue);
}));
})));

// First pass - process whole document.
editor.renderAllMath = logFuncTime(function renderAllMath() {
Expand Down

0 comments on commit 700f42f

Please sign in to comment.