From 774be35cc29821e4cc3521b1c6e2b0d08e6c094d Mon Sep 17 00:00:00 2001 From: Zolmeister Date: Wed, 13 Aug 2014 12:59:03 -0700 Subject: [PATCH] added optional param to m.redraw() to allow forced update --- archive/v0.1.20/mithril-tests.js | 23 +++++++++++++++++++++-- mithril.js | 4 ++-- tests/mithril-tests.js | 19 +++++++++++++++++++ 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/archive/v0.1.20/mithril-tests.js b/archive/v0.1.20/mithril-tests.js index 07324e42..0d6a257e 100644 --- a/archive/v0.1.20/mithril-tests.js +++ b/archive/v0.1.20/mithril-tests.js @@ -406,10 +406,10 @@ Mithril = m = new function app(window, undefined) { m.endComputation() } } - m.redraw = function() { + m.redraw = function(force) { var cancel = window.cancelAnimationFrame || window.clearTimeout var defer = window.requestAnimationFrame || window.setTimeout - if (lastRedrawId) { + if (lastRedrawId && !force) { cancel(lastRedrawId) lastRedrawId = defer(redraw, 0) } @@ -1740,6 +1740,25 @@ function testMithril(mock) { mock.requestAnimationFrame.$resolve() //teardown return count === 3 }) + test(function() { + mock.requestAnimationFrame.$resolve() //setup + var count = 0 + var root = mock.document.createElement("div") + m.module(root, { + controller: function() {}, + view: function(ctrl) { + count++ + } + }) + mock.requestAnimationFrame.$resolve() //teardown + m.redraw(true) //should run synchronously + + m.redraw(true) //forced to run synchronously + m.redraw(true) + m.redraw(true) + mock.requestAnimationFrame.$resolve() //teardown + return count === 5 + }) //m.route test(function() { diff --git a/mithril.js b/mithril.js index e1a10c34..187febf0 100644 --- a/mithril.js +++ b/mithril.js @@ -406,10 +406,10 @@ Mithril = m = new function app(window, undefined) { m.endComputation() } } - m.redraw = function() { + m.redraw = function(force) { var cancel = window.cancelAnimationFrame || window.clearTimeout var defer = window.requestAnimationFrame || window.setTimeout - if (lastRedrawId) { + if (lastRedrawId && !force) { cancel(lastRedrawId) lastRedrawId = defer(redraw, 0) } diff --git a/tests/mithril-tests.js b/tests/mithril-tests.js index a009ee72..3de5fe91 100644 --- a/tests/mithril-tests.js +++ b/tests/mithril-tests.js @@ -743,6 +743,25 @@ function testMithril(mock) { mock.requestAnimationFrame.$resolve() //teardown return count === 3 }) + test(function() { + mock.requestAnimationFrame.$resolve() //setup + var count = 0 + var root = mock.document.createElement("div") + m.module(root, { + controller: function() {}, + view: function(ctrl) { + count++ + } + }) + mock.requestAnimationFrame.$resolve() //teardown + m.redraw(true) //should run synchronously + + m.redraw(true) //forced to run synchronously + m.redraw(true) + m.redraw(true) + mock.requestAnimationFrame.$resolve() //teardown + return count === 5 + }) //m.route test(function() {