prevent infinite recursion in m.redraw

This commit is contained in:
Leo Horie 2015-02-07 23:20:51 -05:00
parent 2ac027a2af
commit ad303427bf
2 changed files with 32 additions and 0 deletions

View file

@ -548,7 +548,10 @@ var m = (function app(window, undefined) {
return controllers[index]
}
};
var redrawing = false
m.redraw = function(force) {
if (redrawing) return
redrawing = true
//lastRedrawId is a positive number if a second redraw is requested before the next animation frame
//lastRedrawID is null if it's the first redraw and not an event handler
if (lastRedrawId && force !== true) {
@ -563,6 +566,7 @@ var m = (function app(window, undefined) {
redraw();
lastRedrawId = $requestAnimationFrame(function() {lastRedrawId = null}, FRAME_BUDGET)
}
redrawing = false
};
m.redraw.strategy = m.prop();
var blank = function() {return ""}

View file

@ -323,6 +323,33 @@ function testMithril(mock) {
return unloaded === 3
})
test(function() {
//calling m.redraw synchronously from controller constructor should not trigger extra redraws
mock.requestAnimationFrame.$resolve()
var root = mock.document.createElement("div")
var count = 0
var module = {
controller: function() {},
view: function(ctrl) {
return m.module(sub)
}
}
var sub = {
controller: function(opts) {
m.redraw()
},
view: function() {
count++
return m("div")
}
}
m.module(root, module)
mock.requestAnimationFrame.$resolve()
return count === 1
})
//m.withAttr
test(function() {
@ -1123,6 +1150,7 @@ function testMithril(mock) {
m.redraw()
m.redraw()
mock.requestAnimationFrame.$resolve() //teardown
return count === 3
})
test(function() {