Skip to content

Commit ddb37df

Browse files
committed
Ensure any previously enqueued rAF is canceled when re-rendering.
Also, use instances length instead of renderedItemCount since it will be undefined on first render.
1 parent d92ff92 commit ddb37df

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

lib/elements/dom-repeat.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ export class DomRepeat extends domRepeatBase {
324324
this.__itemsArrayChanged = false;
325325
this.__shouldMeasureChunk = false;
326326
this.__shouldContinueChunking = false;
327+
this.__chunkingId = 0;
327328
this.__sortFn = null;
328329
this.__filterFn = null;
329330
this.__observePaths = null;
@@ -550,7 +551,8 @@ export class DomRepeat extends domRepeatBase {
550551
// pre-empt this measurement.
551552
if (this.initialCount &&
552553
(this.__shouldMeasureChunk || this.__shouldContinueChunking)) {
553-
this.__debounceRender(this.__continueChunkingAfterRaf);
554+
cancelAnimationFrame(this.__chunkingId);
555+
this.__chunkingId = requestAnimationFrame(() => this.__continueChunking());
554556
}
555557
// Set rendered item count
556558
this._setRenderedItemCount(this.__instances.length);
@@ -583,6 +585,7 @@ export class DomRepeat extends domRepeatBase {
583585

584586
__calculateLimit(filteredItemCount) {
585587
let limit = filteredItemCount;
588+
const currentCount = this.__instances.length;
586589
// When chunking, we increase the limit from the currently rendered count
587590
// by the chunk count that is re-calculated after each rAF (with special
588591
// cases for reseting the limit to initialCount after changing items)
@@ -594,18 +597,18 @@ export class DomRepeat extends domRepeatBase {
594597
limit = Math.min(filteredItemCount, this.initialCount);
595598
// Subtract off any existing instances to determine the number of
596599
// instances that will be created
597-
newCount = Math.max(limit - this.renderedItemCount, 0);
600+
newCount = Math.max(limit - currentCount, 0);
598601
// Initialize the chunk size with how many items we're creating
599602
this.__chunkCount = newCount || 1;
600603
} else {
601604
// The number of new instances that will be created is based on the
602605
// existing instances, the new list size, and the chunk size
603606
newCount = Math.min(
604-
Math.max(filteredItemCount - this.renderedItemCount, 0),
607+
Math.max(filteredItemCount - currentCount, 0),
605608
this.__chunkCount);
606609
// Update the limit based on how many new items we're making, limited
607610
// buy the total size of the list
608-
limit = Math.min(this.renderedItemCount + newCount, filteredItemCount);
611+
limit = Math.min(currentCount + newCount, filteredItemCount);
609612
}
610613
// Record some state about chunking for use in `__continueChunking`
611614
this.__shouldMeasureChunk = newCount === this.__chunkCount;
@@ -616,10 +619,6 @@ export class DomRepeat extends domRepeatBase {
616619
return limit;
617620
}
618621

619-
__continueChunkingAfterRaf() {
620-
requestAnimationFrame(() => this.__continueChunking());
621-
}
622-
623622
__continueChunking() {
624623
// Simple auto chunkSize throttling algorithm based on feedback loop:
625624
// measure actual time between frames and scale chunk count by ratio of

0 commit comments

Comments
 (0)