mithril-vndb/api/redraw.js
Isiah Meadows 462fb659d7
Fix event handler this value, simplify redraw handling (#2320)
Shaves 10 bytes off the bundle from smaller name + reduced indirection
2018-12-03 06:35:27 -05:00

42 lines
1 KiB
JavaScript

"use strict"
var coreRenderer = require("../render/render")
function throttle(callback) {
var pending = null
return function() {
if (pending === null) {
pending = requestAnimationFrame(function() {
pending = null
callback()
})
}
}
}
module.exports = function($window, throttleMock) {
var renderService = coreRenderer($window)
var callbacks = []
var rendering = false
function subscribe(key, callback) {
unsubscribe(key)
callbacks.push(key, callback)
}
function unsubscribe(key) {
var index = callbacks.indexOf(key)
if (index > -1) callbacks.splice(index, 2)
}
function sync() {
if (rendering) throw new Error("Nested m.redraw.sync() call")
rendering = true
for (var i = 1; i < callbacks.length; i+=2) try {callbacks[i]()} catch (e) {if (typeof console !== "undefined") console.error(e)}
rendering = false
}
var redraw = (throttleMock || throttle)(sync)
redraw.sync = sync
renderService.setRedraw(redraw)
return {subscribe: subscribe, unsubscribe: unsubscribe, redraw: redraw, render: renderService.render}
}