From eb0e304ff8e47b226713df6072dc87d58da89004 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Sat, 5 Mar 2022 18:06:23 +0100 Subject: [PATCH] [Debug] Teact: Assert when cached virtual element was moved within tree --- src/lib/teact/teact-dom.ts | 7 +++++++ src/lib/teact/teact.ts | 3 ++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/lib/teact/teact-dom.ts b/src/lib/teact/teact-dom.ts index c82b3c80..065027c0 100644 --- a/src/lib/teact/teact-dom.ts +++ b/src/lib/teact/teact-dom.ts @@ -98,6 +98,13 @@ function renderWithVirtual( return $new; } + if (DEBUG && $new) { + const newTarget = getTarget($new); + if (newTarget && (!$current || newTarget !== getTarget($current))) { + throw new Error('[Teact] Cached virtual element was moved within tree'); + } + } + if (!$current && $new) { if (isNewComponent) { $new = initComponent($new as VirtualElementComponent, $parent, index, parentEl); diff --git a/src/lib/teact/teact.ts b/src/lib/teact/teact.ts index 2e61bf2c..a42ea8f8 100644 --- a/src/lib/teact/teact.ts +++ b/src/lib/teact/teact.ts @@ -449,7 +449,8 @@ function forceUpdateComponent(componentInstance: ComponentInstance) { export function getTarget($element: VirtualElement): Node | undefined { if (isComponentElement($element)) { - return getTarget($element.children[0]); + const componentElement = $element.children[0]; + return componentElement ? getTarget(componentElement) : undefined; } else { return $element.target; }