-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrender-vdom.js
33 lines (29 loc) · 1.05 KB
/
render-vdom.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import {patch} from './vdom'
import {h} from './h'
// Keep tract of rendered nodes.
const watchedNodes = {
container: undefined,
element: undefined,
oldNode: undefined
}
/**
* A function to create and inject a virtual node into the document. On the first render, the node will be appended to the container. After that, each subsequential render will patch and update the DOM. The first argument can be either a JSX tag or an h function.
*
* @param {function} tag A JSX tag or hyperscript function to render.
* @param {Element|boolean} [container] The element into which the tag will be rendered.
*/
export const render = (tag, container) => {
if (typeof container === 'string') container = document.querySelector(container)
// First time rendering.
if (watchedNodes.container !== container) {
watchedNodes.container = container
watchedNodes.element = undefined
watchedNodes.oldNode = undefined
}
watchedNodes.element = patch(
watchedNodes.oldNode,
(watchedNodes.oldNode = tag),
watchedNodes.element,
container
)
}