Skip to content

Commit

Permalink
fix(html): upfront remove unused parts when update array expression
Browse files Browse the repository at this point in the history
  • Loading branch information
smalluban committed Aug 27, 2018
1 parent 990f00d commit aa6c8de
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions src/html/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,33 @@ function resolveArray(host, target, value) {
const data = dataMap.get(target);
const { arrayEntries } = data;

data.arrayEntries = value.reduce((entries, item, index) => {
let id = item;
if (typeof item === 'function') {
id = Object.prototype.hasOwnProperty.call(item, 'id') ? item.id : index;
}
const indexedValue = value.map((item, index) => [
Object.prototype.hasOwnProperty.call(item, 'id') ? item.id : index,
item,
]);

if (arrayEntries) {
const ids = new Set();
indexedValue.forEach(([id]) => ids.add(id));

arrayEntries.forEach((entry) => {
const { id, placeholder } = entry;
if (!ids.has(id)) {
removeTemplate(placeholder);
placeholder.parentNode.removeChild(placeholder);
entry.available = false;
}
});
}

data.arrayEntries = indexedValue.reduce((entries, [id, item], index) => {
const entry = arrayEntries && arrayEntries
.find(entryItem => entryItem[0] && entryItem[1] === id);
.find(entryItem => entryItem.available && entryItem.id === id);

let placeholder;
if (entry) {
placeholder = entry[2];
entry[0] = false;
entry.available = false;
placeholder = entry.placeholder;

if (placeholder.previousSibling !== previousSibling) {
movePlaceholder(placeholder, previousSibling);
Expand All @@ -118,13 +132,14 @@ function resolveArray(host, target, value) {
if (index === 0) data.startNode = placeholder;
if (index === lastIndex) data.endNode = previousSibling;

entries.push([true, id, placeholder]);
entries.push({ available: true, id, placeholder });

return entries;
}, []);

if (arrayEntries) {
arrayEntries.forEach(([available,, placeholder]) => {
arrayEntries.forEach((entry) => {
const { available, placeholder } = entry;
if (available) {
removeTemplate(placeholder);
placeholder.parentNode.removeChild(placeholder);
Expand Down

0 comments on commit aa6c8de

Please sign in to comment.