Skip to content

Commit 28701b6

Browse files
authored
fix: fix memory leak affecting components using conditionally-rendered slots (#9208)
**Related Issue:** #9177 ## Summary Fixes an issue caused by `conditionalSlot` implementing `unobserve` on top of `ExtendedMutationObserver`, which already implements it.
1 parent 23eb3a4 commit 28701b6

File tree

1 file changed

+3
-14
lines changed

1 file changed

+3
-14
lines changed

packages/calcite-components/src/utils/conditionalSlot.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { forceUpdate } from "@stencil/core";
2-
import { createObserver } from "./observers";
3-
4-
const observed = new Set<HTMLElement>();
2+
import { createObserver, ExtendedMutationObserver } from "./observers";
53

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

22-
let mutationObserver: MutationObserver;
20+
let mutationObserver: ExtendedMutationObserver;
2321
const observerOptions: Pick<Parameters<MutationObserver["observe"]>[1], "childList"> = { childList: true };
2422

2523
/**
@@ -51,16 +49,7 @@ export function connectConditionalSlotComponent(component: ConditionalSlotCompon
5149
* ```
5250
*/
5351
export function disconnectConditionalSlotComponent(component: ConditionalSlotComponent): void {
54-
observed.delete(component.el);
55-
56-
// we explicitly process queued mutations and disconnect and reconnect
57-
// the observer until MutationObserver gets an `unobserve` method
58-
// see https://github.com/whatwg/dom/issues/126
59-
processMutations(mutationObserver.takeRecords());
60-
mutationObserver.disconnect();
61-
for (const [element] of observed.entries()) {
62-
mutationObserver.observe(element, observerOptions);
63-
}
52+
mutationObserver.unobserve(component.el);
6453
}
6554

6655
function processMutations(mutations: MutationRecord[]): void {

0 commit comments

Comments
 (0)