From f321750896135d78bb401dad272eff93b6ed45da Mon Sep 17 00:00:00 2001 From: Leo Horie Date: Wed, 7 May 2014 09:42:15 -0400 Subject: [PATCH] ensure redrawing in event handlers --- docs/mithril.computation.md | 15 +++++++++++++++ mithril.js | 5 ++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/docs/mithril.computation.md b/docs/mithril.computation.md index 59a6f230..e9ccd80a 100644 --- a/docs/mithril.computation.md +++ b/docs/mithril.computation.md @@ -34,6 +34,21 @@ For each `m.startComputation` call a library makes, it MUST also make one and ON You should not use these methods if your code is intended to run repeatedly (e.g. by using `setInterval`). If you want to repeatedly redraw the view without necessarily waiting for user input, you should manually call [`m.redraw`](mithril.redraw.md) within the repeatable context. +Note that failing to call `endComputation` after a respective `startComputation` call will halt the redrawing system. It's a good idea to wrap exception-prone code in a `try` block and call `m.endComputation` from within the respective `finally` block, in order to prevent rendering from halting. + +```javascript +window.onfocus = function() { + m.startComputation(); + + try { + doStuff(); + } + finally { + m.endComputation(); //redraw regardless of whether `doStuff` threw errors + } +} +``` + --- ### Integrating multiple execution threads diff --git a/mithril.js b/mithril.js index 205721f6..bd3a8c69 100644 --- a/mithril.js +++ b/mithril.js @@ -174,9 +174,8 @@ Mithril = m = new function app(window) { function autoredraw(callback, object) { return function(e) { m.startComputation() - var output = callback.call(object, e) - m.endComputation() - return output + try {return callback.call(object, e)} + finally {m.endComputation()} } }