From eb69f55f29da40f21058f24918a0c53fb2a140f9 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Sat, 29 Oct 2022 15:18:29 +0200 Subject: [PATCH] Teact: Fix memory leak if head is removed (#2083) --- src/lib/teact/teact-dom.ts | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/lib/teact/teact-dom.ts b/src/lib/teact/teact-dom.ts index 8d287191d..2c34a765b 100644 --- a/src/lib/teact/teact-dom.ts +++ b/src/lib/teact/teact-dom.ts @@ -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 = {}; +const headsByElement = new WeakMap(); // 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] : [];