Skip to content

Commit 1cbe198

Browse files
committed
abstract shouldFindGrandparentInstance
1 parent e1a1f6f commit 1cbe198

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

libs/core/src/lib/renderer/index.ts

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -287,23 +287,7 @@ class NgtRenderer implements Renderer2 {
287287
}
288288
}
289289

290-
const shouldFindGrandparentInstance =
291-
// if child is three but haven't been attached to a parent yet
292-
(cRS[NgtRendererClassId.type] === 'three' && !untracked(getLocalState(newChild).parent)) ||
293-
// or both parent and child are DOM elements
294-
// or they are compound AND haven't had a THREE instance yet
295-
((pRS[NgtRendererClassId.type] === 'dom' ||
296-
(pRS[NgtRendererClassId.type] === 'compound' && !pRS[NgtRendererClassId.compounded])) &&
297-
(cRS[NgtRendererClassId.type] === 'dom' ||
298-
(cRS[NgtRendererClassId.type] === 'compound' && !cRS[NgtRendererClassId.compounded]))) ||
299-
// or the parent is a non-compounded compound
300-
// and the child is a compounded compound
301-
((pRS[NgtRendererClassId.type] === 'dom' ||
302-
(pRS[NgtRendererClassId.type] === 'compound' && !pRS[NgtRendererClassId.compounded])) &&
303-
cRS[NgtRendererClassId.type] === 'compound' &&
304-
!!cRS[NgtRendererClassId.compounded]);
305-
306-
if (shouldFindGrandparentInstance) {
290+
if (this.shouldFindGrandparentInstance(pRS, cRS, newChild)) {
307291
// we'll try to get the grandparent instance here so that we can run appendChild with both instances
308292
const closestGrandparentInstance = this.store.getClosestParentWithInstance(parent);
309293
if (closestGrandparentInstance) this.appendChild(closestGrandparentInstance, newChild);
@@ -461,6 +445,28 @@ class NgtRenderer implements Renderer2 {
461445
return () => {};
462446
}
463447

448+
private shouldFindGrandparentInstance(pRS: NgtRendererState, cRS: NgtRendererState, child: NgtRendererNode) {
449+
const pType = pRS[NgtRendererClassId.type];
450+
const cType = cRS[NgtRendererClassId.type];
451+
const isParentCompounded = pRS[NgtRendererClassId.compounded];
452+
const isChildCompounded = cRS[NgtRendererClassId.compounded];
453+
454+
// if child is three but haven't been attached to a parent yet
455+
const isDanglingThreeChild = cType === 'three' && !untracked(getLocalState(child).parent);
456+
// or both parent and child are DOM elements
457+
// or they are compound AND haven't had a THREE instance yet
458+
const isParentStillDOM = pType === 'dom' || (pType === 'compound' && !isParentCompounded);
459+
const isChildStillDOM = cType === 'dom' || (cType === 'compound' && !isChildCompounded);
460+
// and the child is a compounded compound
461+
const isCompoundChildCompounded = cType === 'compound' && !!isChildCompounded;
462+
463+
return (
464+
isDanglingThreeChild ||
465+
(isParentStillDOM && isChildStillDOM) ||
466+
(isParentStillDOM && isCompoundChildCompounded)
467+
);
468+
}
469+
464470
createText = this.delegate.createText.bind(this.delegate);
465471
destroy = this.delegate.destroy.bind(this.delegate);
466472
destroyNode: ((node: any) => void) | null = null;

0 commit comments

Comments
 (0)