UNPKG

deku

Version:

Render interfaces using pure functions and virtual DOM

52 lines (39 loc) 1.62 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.create = create; var _dom = require('../dom'); var dom = _interopRequireWildcard(_dom); var _diff = require('../diff'); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } /** * Create a DOM renderer using a container element. Everything will be rendered * inside of that container. Returns a function that accepts new state that can * replace what is currently rendered. */ function create(container, dispatch) { var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; var oldVnode = null; var node = null; var rootId = options.id || '0'; if (container && container.childNodes.length > 0) { container.innerHTML = ''; } var update = function update(newVnode, context) { var changes = (0, _diff.diffNode)(oldVnode, newVnode, rootId); node = changes.reduce(dom.update(dispatch, context), node); oldVnode = newVnode; return node; }; var create = function create(vnode, context) { node = dom.create(vnode, rootId, dispatch, context); if (container) container.appendChild(node); oldVnode = vnode; return node; }; return function (vnode) { var context = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; return node !== null ? update(vnode, context) : create(vnode, context); }; }