mithril-vndb/api/mount-redraw.js
2023-07-28 08:17:49 +02:00

48 lines
1,011 B
JavaScript

"use strict"
var Vnode = require("../render/vnode")
module.exports = function(render, schedule, console) {
var subscriptions = []
var pending = false
var offset = -1
function sync() {
for (offset = 0; offset < subscriptions.length; offset += 2) {
render(subscriptions[offset], Vnode(subscriptions[offset + 1]), redraw)
}
offset = -1
}
function redraw() {
if (!pending) {
pending = true
schedule(function() {
pending = false
sync()
})
}
}
redraw.sync = sync
function mount(root, component) {
if (component != null && component.view == null && typeof component !== "function") {
throw new TypeError("m.mount expects a component, not a vnode.")
}
var index = subscriptions.indexOf(root)
if (index >= 0) {
subscriptions.splice(index, 2)
if (index <= offset) offset -= 2
render(root, [])
}
if (component != null) {
subscriptions.push(root, component)
render(root, Vnode(component), redraw)
}
}
return {mount: mount, redraw: redraw}
}