Matt-Esch / virtual-dom

A Virtual DOM and diffing algorithm
MIT License
11.66k stars 779 forks source link

Widget update never being called? #375

Closed rmccue closed 8 years ago

rmccue commented 8 years ago

When I call diff with a node with widget children, the update method on the widget is never called.

function ExampleWidget() {}
ExampleWidget.prototype.type = 'Widget';
ExampleWidget.prototype.init = function () {
    var vdom = h('p', ['hello!']);
    return createElement(vdom);
}
ExampleWidget.prototype.update = function () {
    console.log('widget update');
};

var widget = new ExampleWidget();
var first = h(
    'p',
    [widget]
);
// Something changes in widget
widget.someproperty = true;
var second = h(
    'p',
    [widget]
);

diff(first, second);

I expected update to be called (so that the widget can handle updates on its subtree), but it's never actually called. This occurs because the a === b optimisation at the start of diff.js#walk returns true (since it's the same widget object), so a VPatch.WIDGET patch is never emitted.

Am I just completely misunderstanding how widgets work? From what I can see, update on widgets will never be called currently.

rmccue commented 8 years ago

And as soon as I posted this, think I worked it out: I need to recreate the widget object on each render.