-
Notifications
You must be signed in to change notification settings - Fork 131
/
index.js
41 lines (35 loc) · 1.08 KB
/
index.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
34
35
36
37
38
39
40
41
import * as dom from '../dom'
import {diffNode} from '../diff'
import empty from '@f/empty-element'
import noop from '@f/noop'
/**
* 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.
*/
export function createApp (container, handler = noop, options = {}) {
let oldVnode = null
let node = null
let rootId = options.id || '0'
let dispatch = effect => effect && handler(effect)
if (container) {
empty(container)
}
let update = (newVnode, context) => {
let changes = diffNode(oldVnode, newVnode, rootId)
node = changes.reduce(dom.updateElement(dispatch, context), node)
oldVnode = newVnode
return node
}
let create = (vnode, context) => {
node = dom.createElement(vnode, rootId, dispatch, context)
if (container) container.appendChild(node)
oldVnode = vnode
return node
}
return (vnode, context = {}) => {
return node !== null
? update(vnode, context)
: create(vnode, context)
}
}