Same shape of virtual tree #28
-
const block1 = createBlock(`<div>block1</div>`);
const block2 = createBlock(`<div>block2</div>`);
// incorrect: trees have not the same shape!!!
{
const tree = block1();
mount(tree, document.body);
patch(tree, block2());
}
// correct: trees uses the toggler element to differentiate between two sub trees
{
const tree = toggler("tree1", block1());
mount(tree, document.body);
patch(tree, toggler("tree2", block2()));
}Based on the above example, you mean Does exist any functionality (as a boolean) to compare two blocks for the same shape? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
I can see that the documentation is not very good on that topic. In the two examples above, the incorrect case is because the two trees that are being patched are: const tree = block1();
const tree2 = block2();But as you can see, the first tree is created from a function coming from a different This is kind of a subtle issue. This comes from the fact that, to optimize performance as much as possible, For a framework using templates, this is mostly a non issue: the framework would internally compiles a template into a function, but then, due to the fact that we start with a template, the function will automatically return a tree with the same shape/structure. So I decided that this was okay. But let's explain more what we mean by the same shape: two virtual nodes
However, in some cases, we do not know before the shape of what we will get. For example, if we call a function that will return a blockdom (for example, if we call a dynamic sub template). In that case, we can use the There you go, hope it helps |
Beta Was this translation helpful? Give feedback.
I can see that the documentation is not very good on that topic.
In the two examples above, the incorrect case is because the two trees that are being patched are:
But as you can see, the first tree is created from a function coming from a different
createBlockthan the second one. That is the problem.This is kind of a subtle issue. This comes from the fact that, to optimize performance as much as possible,
blockdomdoes not check if two virtual tree are the same. Wheneverblockdomis patching avnode, it blindly assumes that the targetvnodeis of the same type, and applies directly the patch operation from the first vnode with the data/chi…