UNPKG

mithril

Version:

A framework for building brilliant applications

51 lines (41 loc) 1.12 kB
"use strict" var Vnode = require("../render/vnode") module.exports = function(render, schedule, console) { var subscriptions = [] var rendering = false var pending = false function sync() { if (rendering) throw new Error("Nested m.redraw.sync() call") rendering = true for (var i = 0; i < subscriptions.length; i += 2) { try { render(subscriptions[i], Vnode(subscriptions[i + 1]), redraw) } catch (e) { console.error(e) } } rendering = false } 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(element, component) expects a component, not a vnode") } var index = subscriptions.indexOf(root) if (index >= 0) { subscriptions.splice(index, 2) render(root, [], redraw) } if (component != null) { subscriptions.push(root, component) render(root, Vnode(component), redraw) } } return {mount: mount, redraw: redraw} }