throttle m.route redraws

This commit is contained in:
Leo Horie 2016-12-04 01:53:39 -05:00
parent 7368cf6f26
commit 2ffd2fb7e4
9 changed files with 161 additions and 119 deletions

View file

@ -3,27 +3,6 @@
var Vnode = require("../render/vnode")
module.exports = function(redrawService) {
function throttle(callback) {
//60fps translates to 16.6ms, round it down since setTimeout requires int
var time = 16
var last = 0, pending = null
var timeout = typeof requestAnimationFrame === "function" ? requestAnimationFrame : setTimeout
return function() {
var now = Date.now()
if (last === 0 || now - last >= time) {
last = now
callback()
}
else if (pending === null) {
pending = timeout(function() {
pending = null
callback()
last = Date.now()
}, time - (now - last))
}
}
}
return function(root, component) {
if (component === null) {
redrawService.render(root, [])
@ -33,10 +12,10 @@ module.exports = function(redrawService) {
if (component.view == null) throw new Error("m.mount(element, component) expects a component, not a vnode")
var run = throttle(function() {
var run = function() {
redrawService.render(root, Vnode(component))
})
}
redrawService.subscribe(root, run)
run()
redrawService.redraw()
}
}