Skip to content

Commit

Permalink
Lazy components (#606)
Browse files Browse the repository at this point in the history
  • Loading branch information
frenzzy authored and jorgebucaran committed Feb 28, 2018
1 parent f053412 commit 11b7776
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 37 deletions.
33 changes: 22 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@ export function h(name, attributes /*, ...rest*/) {
}
}

return typeof name === "function"
? name(attributes || {}, children)
: {
nodeName: name,
attributes: attributes || {},
children: children,
key: attributes && attributes.key
}
return {
nodeName: name,
attributes: attributes || {},
children: children,
key: attributes && attributes.key
}
}

export function app(state, actions, view, container) {
Expand Down Expand Up @@ -51,12 +49,23 @@ export function app(state, actions, view, container) {
}
}

function getVNode(node) {
return typeof node.nodeName === "function"
? getVNode(node.nodeName(node.attributes, node.children))
: node
}

function render() {
renderLock = !renderLock

var next = view(globalState, wiredActions)
if (container && !renderLock) {
rootElement = patch(container, rootElement, oldNode, (oldNode = next))
rootElement = patch(
container,
rootElement,
oldNode,
(oldNode = getVNode(next))
)
firstRender = false
}

Expand Down Expand Up @@ -168,7 +177,9 @@ export function app(state, actions, view, container) {
}

for (var i = 0; i < node.children.length; i++) {
element.appendChild(createElement(node.children[i], isSVG))
element.appendChild(
createElement((node.children[i] = getVNode(node.children[i])), isSVG)
)
}

for (var name in node.attributes) {
Expand Down Expand Up @@ -262,7 +273,7 @@ export function app(state, actions, view, container) {

while (j < node.children.length) {
var oldChild = oldNode.children[i]
var newChild = node.children[j]
var newChild = (node.children[j] = getVNode(node.children[j]))

var oldKey = getKey(oldChild)
var newKey = getKey(newChild)
Expand Down
60 changes: 60 additions & 0 deletions test/components.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { h, app } from "../src"

beforeEach(() => {
document.body.innerHTML = ""
})

test("components", done => {
const Component = (props, children) => h("div", props, children)

const view = () =>
h(
Component,
{
oncreate() {
expect(document.body.innerHTML).toBe("<div>foo<div>bar</div></div>")
done()
}
},
["foo", h(Component, {}, "bar")]
)

app({}, {}, view, document.body)
})

test("nested components", done => {
const Child = (props, children) => h("div", props, children)
const Parent = (props, children) => h(Child, props, children)

const view = () =>
h(
Parent,
{
oncreate() {
expect(document.body.innerHTML).toBe("<div>foo</div>")
done()
}
},
["foo"]
)

app({}, {}, view, document.body)
})

test("component with no props adds default props", done => {
const Component = ({ name = "world" }, children) =>
h(
"div",
{
oncreate() {
expect(document.body.innerHTML).toBe("<div>Hello world</div>")
done()
}
},
"Hello " + name
)

const view = () => h(Component)

app({}, {}, view, document.body)
})
29 changes: 3 additions & 26 deletions test/h.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,35 +79,12 @@ test("skip null and Boolean children", () => {
expect(h("div", {}, null)).toEqual(expected)
})

test("components", () => {
const Component = (props, children) => h("div", props, children)
test("component", () => {
const Component = () => h("div", {}, ["baz"])

expect(h(Component, { id: "foo" }, "bar")).toEqual({
nodeName: "div",
nodeName: Component,
attributes: { id: "foo" },
children: ["bar"]
})

expect(h(Component, { id: "foo" }, [h(Component, { id: "bar" })])).toEqual({
nodeName: "div",
attributes: { id: "foo" },
children: [
{
nodeName: "div",
attributes: { id: "bar" },
children: []
}
]
})
})

test("component with no props adds default props", () => {
const Component = ({ name = "world" }, children) =>
h("div", {}, "Hello " + name)

expect(h(Component)).toEqual({
nodeName: "div",
attributes: {},
children: ["Hello world"]
})
})

0 comments on commit 11b7776

Please sign in to comment.