Skip to content

Instantly share code, notes, and snippets.

@dead-claudia
Last active April 8, 2017 16:43
Show Gist options
  • Save dead-claudia/9898852646f4b5e10f403a22e82c88cb to your computer and use it in GitHub Desktop.
Save dead-claudia/9898852646f4b5e10f403a22e82c88cb to your computer and use it in GitHub Desktop.
Mithril's api/redraw.js, rewritten to throttle globally, support sync redraws, and be simpler.
"use strict"
var coreRenderer = require("../render/render")
function throttle(callback) {
//60fps translates to 16.6ms, round it down since setTimeout requires int
var delay = 16, last = 0, pending
var timeout = typeof requestAnimationFrame === "function" ? requestAnimationFrame : setTimeout
return function() {
if (pending == null) {
pending = timeout(function() {
pending = undefined
callback()
last = Date.now()
}, delay - Date.now() - last)
}
}
}
module.exports = function($window, throttleMock) {
var _throttle = throttleMock || throttle
var renderService = coreRenderer($window)
var roots = [], callbacks = []
var redraw = _throttle(redrawSync)
var locked = false
renderService.setEventCallback(function(e) {
if (e.redraw !== false) redraw()
})
function unsubscribe(key) {
var index = callbacks.indexOf(key)
if (index > -1) callbacks.splice(index, 2)
}
function subscribe(key, callback) {
unsubscribe(key)
callbacks.push(key, callback)
}
function redrawSync() {
if (locked) throw new Error("Can't redraw synchronously while already redrawing")
locked = true
for (var i = 1; i < callbacks.length; i += 2) callbacks[i]()
locked = false
}
return {subscribe: subscribe, unsubscribe: unsubscribe, redraw: redraw, redrawSync: redrawSync, render: renderService.render}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment