@@ -17,21 +17,36 @@ export class NgtRendererFactory implements RendererFactory2 {
1717 private readonly catalogue = inject ( NGT_CATALOGUE ) ;
1818 private readonly compoundPrefixes = inject ( NGT_COMPOUND_PREFIXES ) ;
1919
20- private readonly rendererStore = new NgtRendererStore ( {
21- store : this . store ,
22- cdr : this . cdr ,
23- compoundPrefixes : this . compoundPrefixes ,
24- } ) ;
25-
26- private renderer ?: NgtRenderer ;
20+ private rendererMap = new Map < string , Renderer2 > ( ) ;
2721
2822 createRenderer ( hostElement : any , type : RendererType2 | null ) : Renderer2 {
29- // TODO we might need to check on "type" to return DomRenderer for that particular type to support HTML
30- if ( ! this . renderer ) {
31- const domRenderer = this . domRendererFactory . createRenderer ( hostElement , type ) ;
32- this . renderer = new NgtRenderer ( domRenderer , this . rendererStore , this . catalogue ) ;
23+ const domRenderer = this . domRendererFactory . createRenderer ( hostElement , type ) ;
24+ if ( ! type ) return domRenderer ;
25+
26+ let renderer = this . rendererMap . get ( type . id ) ;
27+ if ( renderer ) return renderer ;
28+
29+ if ( ! hostElement ) {
30+ const store = new NgtRendererStore ( {
31+ store : this . store ,
32+ cdr : this . cdr ,
33+ compoundPrefixes : this . compoundPrefixes ,
34+ } ) ;
35+ renderer = new NgtRenderer ( domRenderer , store , this . catalogue , true ) ;
36+ this . rendererMap . set ( type . id , renderer ) ;
37+ }
38+
39+ if ( ! renderer ) {
40+ const store = new NgtRendererStore ( {
41+ store : this . store ,
42+ cdr : this . cdr ,
43+ compoundPrefixes : this . compoundPrefixes ,
44+ } ) ;
45+ renderer = new NgtRenderer ( domRenderer , store , this . catalogue ) ;
46+ this . rendererMap . set ( type . id , renderer ) ;
3347 }
34- return this . renderer ;
48+
49+ return renderer ;
3550 }
3651}
3752
@@ -41,14 +56,15 @@ export class NgtRenderer implements Renderer2 {
4156 constructor (
4257 private readonly domRenderer : Renderer2 ,
4358 private readonly store : NgtRendererStore ,
44- private readonly catalogue : Record < string , new ( ...args : any [ ] ) => any >
59+ private readonly catalogue : Record < string , new ( ...args : any [ ] ) => any > ,
60+ private readonly root = false
4561 ) { }
4662
4763 createElement ( name : string , namespace ?: string | null | undefined ) {
4864 const element = this . domRenderer . createElement ( name , namespace ) ;
4965
5066 // on first pass, we return the Root Scene as the root node
51- if ( ! this . first ) {
67+ if ( this . root && ! this . first ) {
5268 this . first = true ;
5369 return this . store . createNode ( 'three' , this . store . rootScene ) ;
5470 }
@@ -191,19 +207,15 @@ export class NgtRenderer implements Renderer2 {
191207 }
192208 }
193209
194- if ( newChild . __ngt_renderer__ [ NgtRendererClassId . type ] === 'three' && ! getLocalState ( newChild ) . parent ) {
195- // we'll try to get the grandparent instance here so that we can run appendChild with both instances
196- const closestGrandparentInstance = this . store . getClosestParentWithInstance ( parent ) ;
197- if ( closestGrandparentInstance ) {
198- this . appendChild ( closestGrandparentInstance , newChild ) ;
199- }
200- return ;
201- }
210+ const shouldFindGrandparentInstance =
211+ // if child is three but haven't been attached to a parent yet
212+ ( newChild . __ngt_renderer__ [ NgtRendererClassId . type ] === 'three' && ! getLocalState ( newChild ) . parent ) ||
213+ // or both parent and child are DOM elements
214+ ( parent . __ngt_renderer__ [ NgtRendererClassId . type ] === 'dom' &&
215+ newChild . __ngt_renderer__ [ NgtRendererClassId . type ] === 'dom' ) ;
202216
203- if (
204- parent . __ngt_renderer__ [ NgtRendererClassId . type ] === 'dom' &&
205- newChild . __ngt_renderer__ [ NgtRendererClassId . type ] === 'dom'
206- ) {
217+ if ( shouldFindGrandparentInstance ) {
218+ // we'll try to get the grandparent instance here so that we can run appendChild with both instances
207219 const closestGrandparentInstance = this . store . getClosestParentWithInstance ( parent ) ;
208220 if ( closestGrandparentInstance ) {
209221 this . appendChild ( closestGrandparentInstance , newChild ) ;
@@ -215,7 +227,7 @@ export class NgtRenderer implements Renderer2 {
215227 parent : NgtRendererNode ,
216228 newChild : NgtRendererNode
217229 // TODO we might need these?
218- // refChild: NgtRendererNode,
230+ // refChild: NgtRendererNode
219231 // isMove?: boolean | undefined
220232 ) : void {
221233 if ( ! parent . __ngt_renderer__ ) return ;
0 commit comments