Skip to content

Commit

Permalink
Extend (vnode, old) signature to all repeating methods, fix #2098
Browse files Browse the repository at this point in the history
  • Loading branch information
barneycarroll committed Dec 8, 2018
1 parent eaa1c13 commit 411f130
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
12 changes: 6 additions & 6 deletions render/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ module.exports = function($window) {
if (shouldNotUpdate(vnode, old)) return
if (typeof oldTag === "string") {
if (vnode.attrs != null) {
updateLifecycle(vnode.attrs, vnode, hooks)
updateLifecycle(vnode.attrs, vnode, old, hooks)
}
switch (oldTag) {
case "#": updateText(old, vnode); break
Expand Down Expand Up @@ -509,10 +509,10 @@ module.exports = function($window) {
}
}
function updateComponent(parent, old, vnode, hooks, nextSibling, ns) {
vnode.instance = Vnode.normalize(callHook.call(vnode.state.view, vnode))
vnode.instance = Vnode.normalize(callHook.call(vnode.state.view, vnode, old))
if (vnode.instance === vnode) throw Error("A view cannot return the vnode it received as argument")
updateLifecycle(vnode.state, vnode, hooks)
if (vnode.attrs != null) updateLifecycle(vnode.attrs, vnode, hooks)
updateLifecycle(vnode.state, vnode, old, hooks)
if (vnode.attrs != null) updateLifecycle(vnode.attrs, vnode, old, hooks)
if (vnode.instance != null) {
if (old.instance == null) createNode(parent, vnode.instance, hooks, ns, nextSibling)
else updateNode(parent, old.instance, vnode.instance, hooks, nextSibling, ns)
Expand Down Expand Up @@ -867,8 +867,8 @@ module.exports = function($window) {
if (typeof source.oninit === "function") callHook.call(source.oninit, vnode)
if (typeof source.oncreate === "function") hooks.push(callHook.bind(source.oncreate, vnode))
}
function updateLifecycle(source, vnode, hooks) {
if (typeof source.onupdate === "function") hooks.push(callHook.bind(source.onupdate, vnode))
function updateLifecycle(source, vnode, old, hooks) {
if (typeof source.onupdate === "function") hooks.push(callHook.bind(source.onupdate, vnode, old))
}
function shouldNotUpdate(vnode, old) {
do {
Expand Down
13 changes: 8 additions & 5 deletions render/tests/test-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,17 @@ o.spec("component", function() {
o(root.firstChild.firstChild.nodeValue).equals("b")
})
o("updates", function() {
var view = o.spy(function(vnode) {
return {tag: "div", attrs: vnode.attrs, text: vnode.text}
})
var component = createComponent({
view: function(vnode) {
return {tag: "div", attrs: vnode.attrs, text: vnode.text}
}
view: view
})
render(root, [{tag: component, attrs: {id: "a"}, text: "b"}])
render(root, [{tag: component, attrs: {id: "c"}, text: "d"}])

o(view.args[0].attrs).deepEquals({id: "c"})
o(view.args[1].attrs).deepEquals({id: "a"})
o(root.firstChild.nodeName).equals("DIV")
o(root.firstChild.attributes["id"].value).equals("c")
o(root.firstChild.firstChild.nodeValue).equals("d")
Expand Down Expand Up @@ -806,11 +809,11 @@ o.spec("component", function() {
o(methods[hook].this).equals(methods.view.this)(hook)
})

o(methods.view.args.length).equals(1)
o(methods.view.args.length).equals(2)
o(methods.oninit.args.length).equals(1)
o(methods.oncreate.args.length).equals(1)
o(methods.onbeforeupdate.args.length).equals(2)
o(methods.onupdate.args.length).equals(1)
o(methods.onupdate.args.length).equals(2)
o(methods.onbeforeremove.args.length).equals(1)
o(methods.onremove.args.length).equals(1)

Expand Down
10 changes: 10 additions & 0 deletions render/tests/test-onupdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,16 @@ o.spec("onupdate", function() {
}
o(called).equals(true)
})
o("receives the old vnode as its second argument", function(){
var update = o.spy()
var vnode = {tag: "div", key: 1, attrs: {onupdate: update}}
var updated = {tag: "div", key: 1, attrs: {onupdate: update}}

render(root, [vnode])
render(root, [updated])

o(update.args[1]).equals(vnode)
})
o("does not set onupdate as an event handler", function() {
var update = o.spy()
var vnode = {tag: "div", attrs: {onupdate: update}, children: []}
Expand Down

0 comments on commit 411f130

Please sign in to comment.