-
Notifications
You must be signed in to change notification settings - Fork 4
/
render-subtree.js
51 lines (44 loc) · 1.17 KB
/
render-subtree.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
42
43
44
45
46
47
48
49
50
51
import React from "react"
import ReactDOM from "react-dom"
import { containerExists } from "./update-dom"
import { reduce, map, prop, propEq, compose, filter, uniq } from "rambda"
function partial(fn, args) {
return fn.bind(null, ...args)
}
function keys(object) {
return Object.keys(object)
}
function covertToArray(registry) {
return reduce(
(accum, key) => {
accum.push(registry[key])
return accum
},
[],
keys(registry)
)
}
function uniqueContainers(registry) {
return compose(uniq, map(prop("selector")))(covertToArray(registry))
}
function appendToDOM(selector, arrayOfElements) {
const container = containerExists(selector)
if (container) {
return ReactDOM.render(<span>{arrayOfElements}</span>, container)
}
}
function injectSubtree(registry, selector) {
return compose(
partial(appendToDOM, [selector]),
reduce(
(arrayOfElements, item) => (
arrayOfElements.push(item.element), arrayOfElements
),
[]
),
filter(propEq("selector", selector))
)(covertToArray(registry))
}
export function renderSubtree(registry) {
return map(injectSubtree.bind(null, registry), uniqueContainers(registry))
}