Teact: Fix memory leak if head is removed (#2083)

This commit is contained in:
Alexander Zinchuk 2022-10-29 15:18:29 +02:00
parent decbfd39a1
commit eb69f55f29

View File

@ -19,7 +19,6 @@ import {
unmountComponent,
isFragmentElement,
} from './teact';
import generateIdFor from '../../util/generateIdFor';
import { DEBUG } from '../../config';
import { addEventListener, removeAllDelegatedListeners, removeEventListener } from './dom-events';
import { unique } from '../../util/iteratees';
@ -37,19 +36,16 @@ const MAPPED_ATTRIBUTES: { [k: string]: string } = {
};
const INDEX_KEY_PREFIX = '__indexKey#';
const headsByElement: Record<string, VirtualDomHead> = {};
const headsByElement = new WeakMap<HTMLElement, VirtualDomHead>();
// eslint-disable-next-line @typescript-eslint/naming-convention
let DEBUG_virtualTreeSize = 1;
function render($element: VirtualElement | undefined, parentEl: HTMLElement) {
let headId = parentEl.getAttribute('data-teact-head-id');
if (!headId) {
headId = generateIdFor(headsByElement);
headsByElement[headId] = { children: [] };
parentEl.setAttribute('data-teact-head-id', headId);
if (!headsByElement.has(parentEl)) {
headsByElement.set(parentEl, { children: [] });
}
const $head = headsByElement[headId];
const $head = headsByElement.get(parentEl)!;
const $newElement = renderWithVirtual(parentEl, $head.children[0], $element, $head, 0);
$head.children = $newElement ? [$newElement] : [];