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

@ -2,6 +2,27 @@
var coreRenderer = require("../render/render")
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))
}
}
}
module.exports = function($window) {
var renderService = coreRenderer($window)
renderService.setEventCallback(function(e) {
@ -11,7 +32,7 @@ module.exports = function($window) {
var callbacks = []
function subscribe(key, callback) {
unsubscribe(key)
callbacks.push(key, callback)
callbacks.push(key, throttle(callback))
}
function unsubscribe(key) {
var index = callbacks.indexOf(key)