@@ -54,7 +54,12 @@ export class NgtRendererFactory2 implements RendererFactory2 {
54
54
if ( ! type ) return delegateRenderer ;
55
55
56
56
let renderer = this . rendererMap . get ( type . id ) ;
57
- if ( renderer ) return renderer ;
57
+ if ( renderer ) {
58
+ if ( renderer instanceof NgtRenderer2 ) {
59
+ renderer . count += 1 ;
60
+ }
61
+ return renderer ;
62
+ }
58
63
59
64
if ( hostElement && ! isRendererNode ( hostElement ) ) {
60
65
createRendererNode ( 'platform' , hostElement , this . document ) ;
@@ -80,101 +85,6 @@ export class NgtRendererFactory2 implements RendererFactory2 {
80
85
81
86
this . rendererMap . set ( type . id , ( renderer = new NgtRenderer2 ( delegateRenderer , this . catalogue , this . document ) ) ) ;
82
87
return renderer ;
83
-
84
- // const isPortal = isRendererNode(hostElement) && hostElement.__ngt_renderer__[NgtRendererClassId.type] === 'portal';
85
- //
86
- // const debugNode = hostElement ? new DebugNode(hostElement) : null;
87
- // let store = debugNode?.injector?.get(NGT_STORE, null, { optional: true }) || null;
88
- //
89
- // // if the host element is already a renderer node, it should hav a store
90
- // if (!store && isRendererNode(hostElement) && hostElement.__ngt_renderer__[NgtRendererClassId.store]) {
91
- // store = hostElement.__ngt_renderer__[NgtRendererClassId.store];
92
- // }
93
- //
94
- // // if there's still no store but there's NGT_MANUAL_INJECTED_STORE (i.e: from NgtRouterOutlet)
95
- // if (!store && 'type' in type && typeof type.type === 'function' && NGT_MANUAL_INJECTED_STORE in type.type) {
96
- // store = type.type[NGT_MANUAL_INJECTED_STORE] as SignalState<NgtState> | null;
97
- // }
98
- //
99
- // let cacheKey = store ? `${type.id}-${store.snapshot.id}` : type.id;
100
- // let renderer = !isPortal ? this.rendererMap.get(cacheKey) : null;
101
- //
102
- // if (!isRendererNode(hostElement)) {
103
- // renderer = null;
104
- // }
105
- //
106
- // if (!renderer) {
107
- // // detect the entry point of *canvasContent directive
108
- // const hasCanvasContent = (type as any)['consts']?.some((constArr: unknown[]) =>
109
- // constArr.some((item) => item === 'canvasContent'),
110
- // );
111
- //
112
- // if (!store && !hasCanvasContent) {
113
- // renderer = delegateRenderer;
114
- // } else {
115
- // // NOTE: if we have a store but hostElement isn't a renderer node,
116
- // // this means the element was created in a context outside of NgtCanvas
117
- // // but is _embedded_ in the NgtCanvas context later on via ng-template
118
- // // we'll make the hostElement a RendererNode here
119
- // if (store && hostElement) {
120
- // if (!(NGT_RENDERER_NODE_FLAG in hostElement)) {
121
- // createRendererNode('platform', store, hostElement, this.document);
122
- // }
123
- //
124
- // const rS = (hostElement as NgtRendererNode).__ngt_renderer__;
125
- //
126
- // if (!rS[NgtRendererClassId.store]) {
127
- // rS[NgtRendererClassId.store] = store;
128
- // } else if (rS[NgtRendererClassId.store] !== store) {
129
- // // reassign store if it's different
130
- // let resolvedStore = store;
131
- // let shouldReassign = false;
132
- //
133
- // // check if resolved store has renderer state store as an ancestor
134
- // while (resolvedStore.snapshot.previousRoot) {
135
- // // if it is, then we reassign
136
- // if (resolvedStore.snapshot.previousRoot.snapshot.id === rS[NgtRendererClassId.store].snapshot.id) {
137
- // shouldReassign = true;
138
- // break;
139
- // }
140
- // resolvedStore = resolvedStore.snapshot.previousRoot;
141
- // }
142
- //
143
- // if (shouldReassign) {
144
- // rS[NgtRendererClassId.store] = store;
145
- // }
146
- // }
147
- //
148
- // // reassign cache key
149
- // cacheKey = `${type.id}-${rS[NgtRendererClassId.store].snapshot.id}`;
150
- // }
151
- //
152
- // const removeRenderer = (renderer: Renderer2) => {
153
- // if (isPortal) {
154
- // this.portals.delete(renderer);
155
- // } else {
156
- // const existing = this.rendererMap.get(cacheKey);
157
- // if (existing === renderer) {
158
- // this.rendererMap.delete(cacheKey);
159
- // }
160
- // }
161
- // };
162
- //
163
- // renderer = new NgtRenderer2(delegateRenderer, this.catalogue, this.document, store, removeRenderer);
164
- // }
165
- //
166
- // if (isPortal) {
167
- // this.portals.add(renderer);
168
- // } else {
169
- // this.rendererMap.set(cacheKey, renderer);
170
- // }
171
- // }
172
- //
173
- // if ('count' in renderer && typeof renderer.count === 'number') {
174
- // renderer.count += 1;
175
- // }
176
- //
177
- // return renderer;
178
88
}
179
89
}
180
90
@@ -187,23 +97,24 @@ export class NgtRenderer2 implements Renderer2 {
187
97
private delegateRenderer : Renderer2 ,
188
98
private catalogue : Record < string , NgtConstructorRepresentation > ,
189
99
private document : Document ,
100
+ public count = 1 ,
190
101
) { }
191
102
192
103
get data ( ) : { [ key : string ] : any } {
193
104
return { ...this . delegateRenderer . data , __ngt_renderer__ : true } ;
194
105
}
195
106
196
107
destroy ( ) : void {
197
- // if (this.count > 1) {
198
- // this.count -= 1;
199
- // return;
200
- // }
201
- //
202
- // // this is the last instance of the same NgtRenderer2
203
- // this.count = 0;
204
- // this.argsCommentNodes = [];
205
- // this.parentCommentNodes = [];
206
- // this.removeRenderer(this) ;
108
+ if ( this . count > 1 ) {
109
+ this . count -= 1 ;
110
+ return ;
111
+ }
112
+
113
+ // this is the last instance of the same NgtRenderer2
114
+ this . count = 0 ;
115
+ this . argsInjectors = [ ] ;
116
+ this . parentInjectors = [ ] ;
117
+ this . portalInjectors = [ ] ;
207
118
}
208
119
209
120
createElement ( name : string , namespace ?: string | null ) {
@@ -394,109 +305,6 @@ export class NgtRenderer2 implements Renderer2 {
394
305
return this . delegateRenderer . appendChild ( parent , newChild ) ;
395
306
}
396
307
397
- // // if the child is a comment, we'll set the parent then bail
398
- // if (cRS?.[NgtRendererClassId.type] === 'comment') {
399
- // setRendererParentNode(newChild, parent);
400
- //
401
- // // reassign store for comment node if it's different than the parent
402
- // if (pRS?.[NgtRendererClassId.store] && cRS[NgtRendererClassId.store] !== pRS[NgtRendererClassId.store]) {
403
- // cRS[NgtRendererClassId.store] = pRS[NgtRendererClassId.store];
404
- // }
405
- //
406
- // return;
407
- // }
408
- //
409
- // if (cRS?.[NgtRendererClassId.type] === 'three') {
410
- // // if child is three and parent is platform
411
- // if (pRS?.[NgtRendererClassId.type] === 'platform') {
412
- // // first check if there's a parent on the parent
413
- // const grandParent = pRS[NgtRendererClassId.parent];
414
- // // if there is, we'll recurse with the grandparent
415
- // if (grandParent) {
416
- // return this.appendChild(grandParent, newChild);
417
- // }
418
- //
419
- // // if not, then we'll set the relationship between these two for later stages
420
- // this.setNodeRelationship(parent, newChild);
421
- // return;
422
- // }
423
- //
424
- // // if child is three and parent is also three, straight-forward case
425
- // if (pRS?.[NgtRendererClassId.type] === 'three') {
426
- // return this.appendThreeRendererNodes(parent, newChild);
427
- // }
428
- //
429
- // // if parent is portal
430
- // if (pRS?.[NgtRendererClassId.type] === 'portal') {
431
- // let portalContainer = pRS[NgtRendererClassId.portalContainer];
432
- // if (!portalContainer) {
433
- // // const container = pRS[NgtRendererClassId.store].snapshot.scene;
434
- // // if (!isRendererNode(container)) {
435
- // // createRendererNode('three', pRS[NgtRendererClassId.store], container, this.document);
436
- // // }
437
- // //
438
- // // const rendererNode = container as unknown as NgtRendererNode;
439
- // // if (!rendererNode.__ngt_renderer__[NgtRendererClassId.parent]) {
440
- // // rendererNode.__ngt_renderer__[NgtRendererClassId.parent] = parent;
441
- // // }
442
- // // portalContainer = pRS[NgtRendererClassId.portalContainer] = rendererNode;
443
- // }
444
- // if (portalContainer) {
445
- // return this.appendChild(portalContainer, newChild);
446
- // }
447
- //
448
- // // if not, then we'll set the relationship between these two for later stages
449
- // this.setNodeRelationship(parent, newChild);
450
- // return;
451
- // }
452
- // }
453
- //
454
- // if (pRS?.[NgtRendererClassId.type] === 'three') {
455
- // // if parent is three and child is platform
456
- // if (cRS?.[NgtRendererClassId.type] === 'platform') {
457
- // if (!cRS[NgtRendererClassId.parent] || cRS[NgtRendererClassId.parent] !== parent) {
458
- // setRendererParentNode(newChild, parent);
459
- // }
460
- //
461
- // for (const rendererChildNode of cRS[NgtRendererClassId.children]) {
462
- // this.appendChild(parent, rendererChildNode);
463
- // }
464
- //
465
- // return;
466
- // }
467
- //
468
- // if (cRS?.[NgtRendererClassId.type] === 'portal') {
469
- // if (!cRS[NgtRendererClassId.parent] || cRS[NgtRendererClassId.parent] !== parent) {
470
- // setRendererParentNode(newChild, parent);
471
- // }
472
- // return;
473
- // }
474
- //
475
- // // if parent is three and child is also three, straight-forward case
476
- // if (cRS?.[NgtRendererClassId.type] === 'three') {
477
- // return this.appendThreeRendererNodes(parent, newChild);
478
- // }
479
- // }
480
- //
481
- // if (pRS?.[NgtRendererClassId.type] === 'platform') {
482
- // // if parent is platform, and child is platform
483
- // if (cRS?.[NgtRendererClassId.type] === 'platform') {
484
- // this.setNodeRelationship(parent, newChild);
485
- // this.delegateRenderer.appendChild(parent, newChild);
486
- //
487
- // const closestAncestorThreeNode = this.findClosestAncestorThreeNode(parent);
488
- // if (closestAncestorThreeNode) this.appendChild(closestAncestorThreeNode, newChild);
489
- // return;
490
- // }
491
- //
492
- // // if parent is platform and child is portal
493
- // if (cRS?.[NgtRendererClassId.type] === 'portal') {
494
- // this.setNodeRelationship(parent, newChild);
495
- // this.delegateRenderer.appendChild(parent, newChild);
496
- // return;
497
- // }
498
- // }
499
-
500
308
return delegatedFn ( ) ;
501
309
}
502
310
@@ -573,33 +381,6 @@ export class NgtRenderer2 implements Renderer2 {
573
381
return this . removeChild ( threeParent as unknown as NgtRendererNode , oldChild ) ;
574
382
}
575
383
576
- // if (pRS[NgtRendererClassId.type] === 'three') {
577
- // if (cRS[NgtRendererClassId.type] === 'three') {
578
- // return removeThreeChild(oldChild, parent, true);
579
- // }
580
- //
581
- // if (cRS[NgtRendererClassId.type] === 'platform') {
582
- // return;
583
- // }
584
- // }
585
- //
586
- // if (pRS[NgtRendererClassId.type] === 'platform') {
587
- // if (cRS[NgtRendererClassId.type] === 'three') {
588
- // return;
589
- // }
590
- //
591
- // if (cRS[NgtRendererClassId.type] === 'platform') {
592
- // return this.delegateRenderer.removeChild(parent, oldChild, isHostElement);
593
- // }
594
- // }
595
- //
596
- // if (pRS[NgtRendererClassId.type] === 'portal') {
597
- // const portalContainer = pRS[NgtRendererClassId.portalContainer];
598
- // if (portalContainer) {
599
- // return this.removeChild(portalContainer, oldChild, isHostElement);
600
- // }
601
- // }
602
-
603
384
return this . delegateRenderer . removeChild ( parent , oldChild , isHostElement ) ;
604
385
}
605
386
@@ -846,33 +627,6 @@ export class NgtRenderer2 implements Renderer2 {
846
627
return this . delegateRenderer . listen ( target , eventName , callback ) ;
847
628
}
848
629
849
- private findClosestAncestorThreeNode < TNode = any > ( node : TNode ) {
850
- // TODO: handle portal
851
-
852
- const rS = ( node as NgtRendererNode ) . __ngt_renderer__ ;
853
- if ( ! rS ) return null ;
854
-
855
- if ( rS [ NgtRendererClassId . type ] === 'three' ) return node ;
856
-
857
- let parent = rS [ NgtRendererClassId . parent ] ;
858
-
859
- if (
860
- parent &&
861
- parent . __ngt_renderer__ [ NgtRendererClassId . type ] === 'portal' &&
862
- parent . __ngt_renderer__ [ NgtRendererClassId . portalContainer ] ?. __ngt_renderer__ [ NgtRendererClassId . type ] === 'three'
863
- ) {
864
- return parent . __ngt_renderer__ [ NgtRendererClassId . portalContainer ] ;
865
- }
866
-
867
- while ( parent && parent . __ngt_renderer__ && parent . __ngt_renderer__ [ NgtRendererClassId . type ] !== 'three' ) {
868
- parent =
869
- parent . __ngt_renderer__ [ NgtRendererClassId . portalContainer ] ??
870
- parent . __ngt_renderer__ [ NgtRendererClassId . parent ] ;
871
- }
872
-
873
- return parent ;
874
- }
875
-
876
630
private appendThreeRendererNodes ( parent : NgtRendererNode , child : NgtRendererNode ) {
877
631
// if parent and chlid are the same, skip
878
632
if ( parent === child ) {
0 commit comments