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

fix: fix memory leak affecting components using conditionally-rendered slots #9208

Merged
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
17 changes: 3 additions & 14 deletions packages/calcite-components/src/utils/conditionalSlot.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { forceUpdate } from "@stencil/core";
import { createObserver } from "./observers";

const observed = new Set<HTMLElement>();
import { createObserver, ExtendedMutationObserver } from "./observers";

/**
* Defines interface for components with a dynamically changing slot.
Expand All @@ -19,7 +17,7 @@ export interface ConditionalSlotComponent {
readonly el: HTMLElement;
}

let mutationObserver: MutationObserver;
let mutationObserver: ExtendedMutationObserver;
const observerOptions: Pick<Parameters<MutationObserver["observe"]>[1], "childList"> = { childList: true };

/**
Expand Down Expand Up @@ -51,16 +49,7 @@ export function connectConditionalSlotComponent(component: ConditionalSlotCompon
* ```
*/
export function disconnectConditionalSlotComponent(component: ConditionalSlotComponent): void {
observed.delete(component.el);

// we explicitly process queued mutations and disconnect and reconnect
// the observer until MutationObserver gets an `unobserve` method
// see https://github.com/whatwg/dom/issues/126
processMutations(mutationObserver.takeRecords());
mutationObserver.disconnect();
for (const [element] of observed.entries()) {
mutationObserver.observe(element, observerOptions);
}
mutationObserver.unobserve(component.el);
}

function processMutations(mutations: MutationRecord[]): void {
Expand Down
Loading