7
7
CUSTOM_ELEMENTS_SCHEMA ,
8
8
DestroyRef ,
9
9
Directive ,
10
+ effect ,
10
11
EmbeddedViewRef ,
11
12
inject ,
12
13
Injector ,
@@ -17,13 +18,11 @@ import {
17
18
viewChild ,
18
19
ViewContainerRef ,
19
20
} from '@angular/core' ;
20
- import { injectAutoEffect } from 'ngxtension/auto-effect' ;
21
21
import { Camera , Object3D , Raycaster , Scene , Vector2 , Vector3 } from 'three' ;
22
22
import { getLocalState , prepare } from './instance' ;
23
23
import { SPECIAL_INTERNAL_ADD_COMMENT } from './renderer/constants' ;
24
24
import { injectStore , provideStore } from './store' ;
25
25
import { NgtComputeFunction , NgtSize , NgtState } from './types' ;
26
- import { injectBeforeRender } from './utils/before-render' ;
27
26
import { is } from './utils/is' ;
28
27
import { signalStore } from './utils/signal-store' ;
29
28
import { updateCamera } from './utils/update' ;
@@ -45,30 +44,34 @@ export class NgtPortalBeforeRender {
45
44
parentCamera = input . required < Camera > ( ) ;
46
45
47
46
constructor ( ) {
48
- injectAutoEffect ( ) ( ( injector ) => {
47
+ effect ( ( onCleanup ) => {
49
48
// track state
50
- this . portalStore . state ( ) ;
51
- const priority = this . renderPriority ( ) ;
49
+ const [ renderPriority , { internal } ] = [ this . renderPriority ( ) , this . portalStore . state ( ) ] ;
52
50
53
- let oldClear : boolean ;
54
- return injectBeforeRender (
55
- ( ) => {
56
- const { gl, scene, camera } = this . portalStore . snapshot ;
57
- oldClear = gl . autoClear ;
58
- if ( this . renderPriority ( ) === 1 ) {
51
+ let oldClean : boolean ;
52
+
53
+ const cleanup = internal . subscribe (
54
+ ( { gl, scene, camera } ) => {
55
+ const [ parentScene , parentCamera ] = [ untracked ( this . parentScene ) , untracked ( this . parentCamera ) ] ;
56
+ oldClean = gl . autoClear ;
57
+ if ( renderPriority === 1 ) {
59
58
// clear scene and render with default
60
59
gl . autoClear = true ;
61
- gl . render ( this . parentScene ( ) , this . parentCamera ( ) ) ;
60
+ gl . render ( parentScene , parentCamera ) ;
62
61
}
62
+
63
63
// disable cleaning
64
64
gl . autoClear = false ;
65
65
gl . clearDepth ( ) ;
66
66
gl . render ( scene , camera ) ;
67
67
// restore
68
- gl . autoClear = oldClear ;
68
+ gl . autoClear = oldClean ;
69
69
} ,
70
- { priority, injector } ,
70
+ renderPriority ,
71
+ this . portalStore ,
71
72
) ;
73
+
74
+ onCleanup ( ( ) => cleanup ( ) ) ;
72
75
} ) ;
73
76
}
74
77
@@ -168,10 +171,9 @@ export class NgtPortal {
168
171
private portalView ?: EmbeddedViewRef < unknown > ;
169
172
170
173
constructor ( ) {
171
- const autoEffect = injectAutoEffect ( ) ;
172
-
173
174
const parentState = this . parentStore . select ( ) ;
174
175
176
+ // NOTE: we run this in afterNextRender for inputs to resolve
175
177
afterNextRender ( ( ) => {
176
178
const previousState = this . parentStore . snapshot ;
177
179
@@ -200,25 +202,28 @@ export class NgtPortal {
200
202
this . portalStore . update ( ( state ) => ( { ...state , events : { ...state . events , ...events } } ) ) ,
201
203
} ) ;
202
204
203
- autoEffect ( ( ) => {
204
- const state = this . state ( ) ;
205
- const _parentState = parentState ( ) ;
206
- this . portalStore . update ( ( prev ) => this . inject ( _parentState , prev , state , untracked ( this . container ) ) ) ;
207
- untracked ( ( ) => {
208
- if ( this . portalView ) {
205
+ effect (
206
+ ( ) => {
207
+ const state = this . state ( ) ;
208
+ const _parentState = parentState ( ) ;
209
+ this . portalStore . update ( ( prev ) => this . inject ( _parentState , prev , state , untracked ( this . container ) ) ) ;
210
+ untracked ( ( ) => {
211
+ if ( this . portalView ) {
212
+ this . portalView . detectChanges ( ) ;
213
+ return ;
214
+ }
215
+
216
+ this . portalView = this . portalAnchor ( ) . createEmbeddedView (
217
+ this . portalContent ( ) ,
218
+ { container : this . container ( ) , injector : this . injector } ,
219
+ { injector : this . injector } ,
220
+ ) ;
209
221
this . portalView . detectChanges ( ) ;
210
- return ;
211
- }
212
-
213
- this . portalView = this . portalAnchor ( ) . createEmbeddedView (
214
- this . portalContent ( ) ,
215
- { container : this . container ( ) , injector : this . injector } ,
216
- { injector : this . injector } ,
217
- ) ;
218
- this . portalView . detectChanges ( ) ;
219
- this . portalRendered . set ( true ) ;
220
- } ) ;
221
- } ) ;
222
+ this . portalRendered . set ( true ) ;
223
+ } ) ;
224
+ } ,
225
+ { injector : this . injector } ,
226
+ ) ;
222
227
} ) ;
223
228
224
229
inject ( DestroyRef ) . onDestroy ( ( ) => {
0 commit comments