UNPKG

824 BJavaScriptView Raw
1import {patchElement} from './utils/patchElement'
2import {recycleElement} from './utils/recycleElement'
3
4/**
5 * @description A function to patch a virtual node agains a DOM element, updating it in the most efficient manner possible.
6 * @param {object} node A virtual node. This may be a JSX tag or a hyperscript function.
7 * @param {HTMLElement} element The element to patch.
8 * @returns {HTMLElement} The updated element.
9 */
10export function patch(node, element) {
11 if (element) {
12 patchElement(
13 element.parentNode,
14 element,
15 element.node == null
16 ? recycleElement(element, [].map)
17 : element.node,
18 node,
19 element.node == null
20 )
21 } else {
22 element = patchElement(
23 null,
24 null,
25 null,
26 node
27 )
28 }
29
30 element.node = node
31
32 return element
33}