Teact: Fix memory leak if head is removed (#2083)
This commit is contained in:
parent
decbfd39a1
commit
eb69f55f29
@ -19,7 +19,6 @@ import {
|
|||||||
unmountComponent,
|
unmountComponent,
|
||||||
isFragmentElement,
|
isFragmentElement,
|
||||||
} from './teact';
|
} from './teact';
|
||||||
import generateIdFor from '../../util/generateIdFor';
|
|
||||||
import { DEBUG } from '../../config';
|
import { DEBUG } from '../../config';
|
||||||
import { addEventListener, removeAllDelegatedListeners, removeEventListener } from './dom-events';
|
import { addEventListener, removeAllDelegatedListeners, removeEventListener } from './dom-events';
|
||||||
import { unique } from '../../util/iteratees';
|
import { unique } from '../../util/iteratees';
|
||||||
@ -37,19 +36,16 @@ const MAPPED_ATTRIBUTES: { [k: string]: string } = {
|
|||||||
};
|
};
|
||||||
const INDEX_KEY_PREFIX = '__indexKey#';
|
const INDEX_KEY_PREFIX = '__indexKey#';
|
||||||
|
|
||||||
const headsByElement: Record<string, VirtualDomHead> = {};
|
const headsByElement = new WeakMap<HTMLElement, VirtualDomHead>();
|
||||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||||
let DEBUG_virtualTreeSize = 1;
|
let DEBUG_virtualTreeSize = 1;
|
||||||
|
|
||||||
function render($element: VirtualElement | undefined, parentEl: HTMLElement) {
|
function render($element: VirtualElement | undefined, parentEl: HTMLElement) {
|
||||||
let headId = parentEl.getAttribute('data-teact-head-id');
|
if (!headsByElement.has(parentEl)) {
|
||||||
if (!headId) {
|
headsByElement.set(parentEl, { children: [] });
|
||||||
headId = generateIdFor(headsByElement);
|
|
||||||
headsByElement[headId] = { children: [] };
|
|
||||||
parentEl.setAttribute('data-teact-head-id', headId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const $head = headsByElement[headId];
|
const $head = headsByElement.get(parentEl)!;
|
||||||
const $newElement = renderWithVirtual(parentEl, $head.children[0], $element, $head, 0);
|
const $newElement = renderWithVirtual(parentEl, $head.children[0], $element, $head, 0);
|
||||||
$head.children = $newElement ? [$newElement] : [];
|
$head.children = $newElement ? [$newElement] : [];
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user