mithril-vndb/api/mount-redraw.js
Isiah Meadows b98ab29efd
Make errors and their messages more accurate and helpful (#2536)
Also, I normalized them to all be sentences for consistency, and I moved
the reentrancy check from `m.mount` to `m.render` to be a little more
helpful. The router change during mounting is inconsequential and only
to avoid the new modified error, and the change to the update loop is to
send the original error if an error occurred while initializing the
default route. (This is all around more useful anyways.)

And while I was at it, I fixed an obscure bug with sync redraws.
2019-09-30 16:08:04 -04:00

49 lines
1 KiB
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) {
try { render(subscriptions[offset], Vnode(subscriptions[offset + 1]), redraw) }
catch (e) { console.error(e) }
}
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}
}