Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ce31fc5

Browse files
committed
fix(template): properly calculate diff on update
the virtual list template manager sometimes didn't produce the correct diff, resulting in missing updates and a broken view
1 parent a81e209 commit ce31fc5

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

libs/template/experimental/virtual-scrolling/src/lib/virtual-list-template-manager.ts

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -274,40 +274,48 @@ export function createVirtualListTemplateManager<
274274
changedIdxs.add(item);
275275
listChanges.push([
276276
itemIndex,
277-
() => {
278-
const view = _updateView(
279-
item,
277+
() =>
278+
maybeUpdateView(
280279
itemIndex,
281280
count,
282-
itemIndex + adjustIndexWith
283-
);
284-
return {
285-
view,
286-
index: itemIndex,
287-
item,
288-
};
289-
},
281+
itemIndex + adjustIndexWith,
282+
item
283+
),
290284
]);
291285
}
292286
}
293-
if (itemCount !== count && changedIdxs.size < items.length) {
287+
if (changedIdxs.size < items.length) {
294288
for (let i = 0; i < items.length; i++) {
295289
const item = items[i];
296290
if (!changedIdxs.has(item)) {
297291
listChanges.push([
298292
i,
299-
() => {
300-
const view = _updateView(item, i, count, i + adjustIndexWith);
301-
return {
302-
view,
303-
index: i,
304-
item,
305-
};
306-
},
293+
() => maybeUpdateView(i, count, i + adjustIndexWith, item),
307294
]);
308295
}
309296
}
310297
}
311298
return [listChanges, notifyParent];
312299
}
300+
301+
function maybeUpdateView(
302+
viewIndex: number,
303+
count: number,
304+
itemIndex: number,
305+
item: T
306+
) {
307+
const view = <EmbeddedViewRef<C>>viewContainerRef.get(viewIndex);
308+
if (view.context.count !== count || view.context.index !== itemIndex) {
309+
return {
310+
view: _updateView(item, viewIndex, count, itemIndex),
311+
index: viewIndex,
312+
item,
313+
};
314+
}
315+
return {
316+
index: viewIndex,
317+
view,
318+
item,
319+
};
320+
}
313321
}

0 commit comments

Comments
 (0)