Last active
December 12, 2018 15:22
-
-
Save jlgrall/674704d98b7223f9a87264171b5f5921 to your computer and use it in GitHub Desktop.
domvm test case: unnecessarily redrawing child vm when parent vm is also redrawing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var el = domvm.defineElement, | |
vw = domvm.defineView; | |
// State: | |
var color = "purple", | |
numbers = [{nb: 0}, {nb: 1}, {nb: 2}, {nb: 3}, {nb: 4}, {nb: 5}]; | |
// Views: | |
function ListView() { | |
return function(vm) { | |
var items = []; | |
for (var i = 0; i < numbers.length; i++) items.push(vw(ItemView, {i: i})); | |
return el("ol", items); | |
}; | |
} | |
function ItemView() { | |
return function(vm, data) { | |
return el("li", {style: "color:" + color}, "Item " + numbers[data.i].nb); | |
}; | |
} | |
var listView = domvm.createView(ListView).mount(document.body); | |
// Redraw functions: | |
function redrawItems() { | |
for (var el of document.querySelectorAll("li")) el._node.vm.redraw(); | |
} | |
function redrawList() { | |
document.querySelector("ol")._node.vm.redraw(); | |
} | |
// Test: | |
setTimeout(function() { | |
console.log("ACTION: length = 2, then color = olive..."); | |
numbers.length = 2; | |
redrawList(); // Will removes children vm. | |
// ... other computations ... | |
color = "olive"; | |
redrawItems(); // Children vm will be redrawn even after being removed. | |
// Thus an error will be thrown during redrawing. | |
}, 1000); | |
setTimeout(function() { | |
console.log("RESET: initial state..."); | |
color = "purple"; | |
numbers = [{nb: 0}, {nb: 1}, {nb: 2}, {nb: 3}, {nb: 4}, {nb: 5}]; | |
redrawList(); | |
}, 2000); | |
setTimeout(function() { | |
console.log("ACTION (reversed order): color = olive, then length = 2..."); | |
color = "olive"; | |
redrawItems(); // Children vm will be redrawn even after being removed. | |
// ... other computations ... | |
numbers.length = 2; | |
redrawList(); // Will removes children vm. | |
// Thus an error will be thrown during redrawing. | |
}, 3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Related domvm issue: domvm/domvm#206