Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cancel chunking when disconnected. Fixes #5667 #5668

Merged
merged 2 commits into from
Jul 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/elements/dom-repeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,10 @@ export class DomRepeat extends domRepeatBase {
for (let i=0; i<this.__instances.length; i++) {
this.__detachInstance(i);
}
// Stop chunking if one was in progress
if (this.__chunkingId) {
cancelAnimationFrame(this.__chunkingId);
}
}

/**
Expand All @@ -364,6 +368,10 @@ export class DomRepeat extends domRepeatBase {
for (let i=0; i<this.__instances.length; i++) {
this.__attachInstance(i, wrappedParent);
}
// Restart chunking if one was in progress when disconnected
if (this.__chunkingId) {
this.__render();
}
}
}

Expand Down Expand Up @@ -552,7 +560,10 @@ export class DomRepeat extends domRepeatBase {
if (this.initialCount &&
(this.__shouldMeasureChunk || this.__shouldContinueChunking)) {
cancelAnimationFrame(this.__chunkingId);
this.__chunkingId = requestAnimationFrame(() => this.__continueChunking());
this.__chunkingId = requestAnimationFrame(() => {
this.__chunkingId = null;
this.__continueChunking();
});
}
// Set rendered item count
this._setRenderedItemCount(this.__instances.length);
Expand Down
29 changes: 25 additions & 4 deletions test/unit/dom-repeat.html
Original file line number Diff line number Diff line change
Expand Up @@ -3933,16 +3933,24 @@ <h4>x-repeat-limit</h4>
suite('chunked rendering', function() {

let chunked;
let resolve;
let notifyChange;
let targetCount;
const handleChange = () => {
let handleChange = () => {
kevinpschaaf marked this conversation as resolved.
Show resolved Hide resolved
if (!targetCount || chunked.$.repeater.renderedItemCount >= targetCount) {
resolve(Array.from(chunked.root.querySelectorAll('*:not(template):not(dom-repeat)')));
notifyChange(Array.from(chunked.root.querySelectorAll('*:not(template):not(dom-repeat)')));
}
};
const waitUntilRendered = async (count) => {
targetCount = count;
return await new Promise(r => resolve = r);
return await new Promise(r => notifyChange = r);
};
const expectNoChanges = async () => {
targetCount = null;
notifyChange = () => {
assert.fail('no changes were expected');
};
// Ensure no changes happen in the next rAF+sT
await new Promise(r => requestAnimationFrame(() => setTimeout(() => r())));
};
setup(() => {
chunked = document.createElement('x-repeat-chunked');
Expand Down Expand Up @@ -4259,6 +4267,19 @@ <h4>x-repeat-limit</h4>
assert.isAtLeast(frameCount, 2);
});

test('chunking stops after detaching, restarts after attaching', async () => {
chunked.items = chunked.preppedItems.slice();
const initialLength = (await waitUntilRendered()).length;
assert.isAbove(initialLength, 0);
assert.isBelow(initialLength, chunked.preppedItems.length);
document.body.removeChild(chunked);
await expectNoChanges();
const done = waitUntilRendered(chunked.preppedItems.length);
document.body.appendChild(chunked);
const endLength = (await done).length;
assert.equal(endLength, chunked.items.length);
});

});

suite('misc', function() {
Expand Down