Skip to content

Commit

Permalink
Avoid (some) unnecessary removal and reinsertion of DOM elements
Browse files Browse the repository at this point in the history
  • Loading branch information
Rosuav committed Nov 8, 2024
1 parent f7bb76d commit c5becd1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
12 changes: 10 additions & 2 deletions factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,15 @@ export function replace_content(target, template) {
if (t && typeof t === "object" && !Array.isArray(t) && !t.river) pristine = false;
});
if (pristine) return target;
return set_content(target, content);
//At this point, we could just "return set_content(target, content);" but this would unnecessarily
//remove and reinsert DOM elements. This next step is far from perfect but will cope with common
//cases: the first N matching DOM elements will not be removed. TODO: Also check for the _last_ N
//matching elements, which will allow any single block of deletions/insertions.
let keep = 0;
while (keep < content.length && content[keep] instanceof Node && target.children[keep] === content[keep]) ++keep;
while (target.children.length > keep) target.removeChild(target.lastChild);
append_child(target, content.slice(keep));
return target;
}

//TODO: Unify lindt and choc. Maybe have choc call lindt and then render?
Expand All @@ -361,7 +369,7 @@ function autobind(obj, prop) {
choc = new Proxy(choc, {get: autobind});
lindt = new Proxy(lindt, {get: autobind});

choc.__version__ = "1.8.3";
choc.__version__ = "1.9.0";

//For modules, make the main entry-point easily available.
export default choc;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chocfactory",
"version": "1.8.3",
"version": "1.9.0",
"description": "Simple JS front end library",
"main": "factory.js",
"scripts": {
Expand Down
4 changes: 4 additions & 0 deletions whatsnew.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Chocolate Factory version history

### v1.9.0
* In Lindt mode, appending new children to a DOM element will not cause existing
elements to be removed and reinserted unnecessarily.

### v1.8.3
* Importer bugfix: `//extcall` now works on arrow functions (JS only)

Expand Down

0 comments on commit c5becd1

Please sign in to comment.