refactor redraw
This commit is contained in:
parent
0f5d051d4b
commit
7295b6e9e9
6 changed files with 26 additions and 9 deletions
|
|
@ -47,7 +47,7 @@ app.controller = function() {
|
||||||
this.pages = app.PageList();
|
this.pages = app.PageList();
|
||||||
|
|
||||||
this.rotate = function() {
|
this.rotate = function() {
|
||||||
this.pages.push(this.pages.shift())
|
this.pages().push(this.pages().shift())
|
||||||
}.bind(this)
|
}.bind(this)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -376,6 +376,8 @@ m.module(document, todo);
|
||||||
|
|
||||||
Mithril's auto-redrawing system keeps track of controller stability, and only redraws the view once it detects that the controller has finished running all of its code, including asynchronous AJAX payloads.
|
Mithril's auto-redrawing system keeps track of controller stability, and only redraws the view once it detects that the controller has finished running all of its code, including asynchronous AJAX payloads.
|
||||||
|
|
||||||
|
Also note that this mechanism itself is not asynchronous if it doesn't need to be: Mithril does not need to wait for the next browser repaint frame to redraw - it doesn't even need to wait for the document ready event on the first redraw - it will redraw immediately upon script completion, if able to.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
### Summary
|
### Summary
|
||||||
|
|
|
||||||
|
|
@ -89,7 +89,7 @@ app.controller = function() {
|
||||||
this.pages = app.PageList();
|
this.pages = app.PageList();
|
||||||
|
|
||||||
this.rotate = function() {
|
this.rotate = function() {
|
||||||
this.pages.push(this.pages.shift())
|
this.pages().push(this.pages().shift())
|
||||||
}.bind(this)
|
}.bind(this)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
12
mithril.js
12
mithril.js
|
|
@ -284,7 +284,10 @@ Mithril = m = new function app(window) {
|
||||||
e = e || event
|
e = e || event
|
||||||
m.startComputation()
|
m.startComputation()
|
||||||
try {return callback.call(object, e)}
|
try {return callback.call(object, e)}
|
||||||
finally {m.endComputation()}
|
finally {
|
||||||
|
if (!lastRedrawId) lastRedrawId = -1;
|
||||||
|
m.endComputation()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -351,9 +354,15 @@ Mithril = m = new function app(window) {
|
||||||
m.redraw = function() {
|
m.redraw = function() {
|
||||||
var cancel = window.cancelAnimationFrame || window.clearTimeout
|
var cancel = window.cancelAnimationFrame || window.clearTimeout
|
||||||
var defer = window.requestAnimationFrame || window.setTimeout
|
var defer = window.requestAnimationFrame || window.setTimeout
|
||||||
|
if (lastRedrawId) {
|
||||||
cancel(lastRedrawId)
|
cancel(lastRedrawId)
|
||||||
lastRedrawId = defer(redraw, 0)
|
lastRedrawId = defer(redraw, 0)
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
redraw()
|
||||||
|
lastRedrawId = defer(function() {lastRedrawId = null}, 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
function redraw() {
|
function redraw() {
|
||||||
for (var i = 0; i < roots.length; i++) {
|
for (var i = 0; i < roots.length; i++) {
|
||||||
if (controllers[i]) m.render(roots[i], modules[i].view(controllers[i]))
|
if (controllers[i]) m.render(roots[i], modules[i].view(controllers[i]))
|
||||||
|
|
@ -362,6 +371,7 @@ Mithril = m = new function app(window) {
|
||||||
computePostRedrawHook()
|
computePostRedrawHook()
|
||||||
computePostRedrawHook = null
|
computePostRedrawHook = null
|
||||||
}
|
}
|
||||||
|
lastRedrawId = null
|
||||||
}
|
}
|
||||||
|
|
||||||
var pendingRequests = 0
|
var pendingRequests = 0
|
||||||
|
|
|
||||||
|
|
@ -714,12 +714,13 @@ function testMithril(mock) {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
mock.requestAnimationFrame.$resolve() //teardown
|
mock.requestAnimationFrame.$resolve() //teardown
|
||||||
m.redraw()
|
m.redraw() //should run synchronously
|
||||||
m.redraw()
|
|
||||||
|
m.redraw() //rest should run asynchronously since they're spamming
|
||||||
m.redraw()
|
m.redraw()
|
||||||
m.redraw()
|
m.redraw()
|
||||||
mock.requestAnimationFrame.$resolve() //teardown
|
mock.requestAnimationFrame.$resolve() //teardown
|
||||||
return count === 2
|
return count === 3
|
||||||
})
|
})
|
||||||
|
|
||||||
//m.route
|
//m.route
|
||||||
|
|
|
||||||
|
|
@ -72,7 +72,11 @@ mock.window = new function() {
|
||||||
}
|
}
|
||||||
window.scrollTo = function() {}
|
window.scrollTo = function() {}
|
||||||
window.cancelAnimationFrame = function() {}
|
window.cancelAnimationFrame = function() {}
|
||||||
window.requestAnimationFrame = function(callback) {window.requestAnimationFrame.$callback = callback}
|
window.requestAnimationFrame = function(callback) {
|
||||||
|
window.requestAnimationFrame.$callback = callback
|
||||||
|
return window.requestAnimationFrame.$id++
|
||||||
|
}
|
||||||
|
window.requestAnimationFrame.$id = 1
|
||||||
window.requestAnimationFrame.$resolve = function() {
|
window.requestAnimationFrame.$resolve = function() {
|
||||||
if (window.requestAnimationFrame.$callback) window.requestAnimationFrame.$callback()
|
if (window.requestAnimationFrame.$callback) window.requestAnimationFrame.$callback()
|
||||||
window.requestAnimationFrame.$callback = null
|
window.requestAnimationFrame.$callback = null
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue