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

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/wicked-mails-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lit-labs/virtualizer': patch
---

Guard against layout updates or re-observing when disconnected (fixes #4182, #3831)
29 changes: 20 additions & 9 deletions packages/labs/virtualizer/src/Virtualizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,14 @@ export class Virtualizer {
);
this._scrollEventListeners = [];
this._clippingAncestors = [];
this._scrollerController!.detach(this);
this._scrollerController?.detach(this);
this._scrollerController = null;
this._mutationObserver!.disconnect();
this._hostElementRO!.disconnect();
this._childrenRO!.disconnect();
this._mutationObserver?.disconnect();
this._mutationObserver = null;
this._hostElementRO?.disconnect();
this._hostElementRO = null;
this._childrenRO?.disconnect();
this._childrenRO = null;
this._rejectLayoutCompletePromise('disconnected');
}

Expand Down Expand Up @@ -550,8 +553,16 @@ export class Virtualizer {
}

_updateLayout() {
if (this._layout) {
this._layout!.items = this._items;
// Only update the layout and trigger a re-render if we have:
// a) A layout
// b) A scrollerController, which means we're connected
// c) An offsetParent, which means we're not hidden
Copy link

@bramkragten bramkragten Nov 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be null if the hostElement has a parent that is positioned fixed (in chrome, in firefox it returns body). The virtualizer doesn't render inside a mwc-dialog anymore now for example in chrome.

if (
this._layout &&
this._scrollerController &&
this._hostElement?.offsetParent
) {
this._layout.items = this._items;
this._updateView();
if (this._childMeasurements !== null) {
// If the layout has been changed, we may have measurements but no callback
Expand All @@ -560,7 +571,7 @@ export class Virtualizer {
}
this._childMeasurements = null;
}
this._layout!.reflowIfNeeded();
this._layout.reflowIfNeeded();
if (this._benchmarkStart && 'mark' in window.performance) {
window.performance.mark('uv-end');
}
Expand Down Expand Up @@ -821,12 +832,12 @@ export class Virtualizer {
this._layoutCompleteRejecter = reject;
});
}
return this._layoutCompletePromise!;
return this._layoutCompletePromise;
}

private _rejectLayoutCompletePromise(reason: string) {
if (this._layoutCompleteRejecter !== null) {
this._layoutCompleteRejecter!(reason);
this._layoutCompleteRejecter(reason);
}
this._resetLayoutCompleteState();
}
Expand Down
2 changes: 1 addition & 1 deletion packages/labs/virtualizer/src/virtualize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class VirtualizeDirective<T = unknown> extends AsyncDirective {
const {layout, scroller, items} = config;
this._virtualizer = new Virtualizer({hostElement, layout, scroller});
this._virtualizer.items = items;
this._virtualizer!.connected();
this._virtualizer.connected();
}

private _initialize(part: ChildPart, config: VirtualizeDirectiveConfig<T>) {
Expand Down