1
- import { DestroyRef , Injector , effect , inject , runInInjectionContext , signal , untracked } from '@angular/core' ;
1
+ import { DestroyRef , Injector , computed , effect , inject , runInInjectionContext } from '@angular/core' ;
2
2
import { assertInjectionContext , injectBeforeRender , injectNgtRef , type NgtInjectedRef } from 'angular-three' ;
3
3
import * as THREE from 'three' ;
4
4
@@ -23,7 +23,6 @@ export function injectNgtsAnimations(
23
23
}
24
24
25
25
const mixer = new THREE . AnimationMixer ( null ! ) ;
26
- const ready = signal ( false ) ;
27
26
const actions = { } as Record < string , THREE . AnimationAction > ;
28
27
let cached = { } as Record < string , THREE . AnimationAction > ;
29
28
@@ -43,36 +42,31 @@ export function injectNgtsAnimations(
43
42
44
43
injectBeforeRender ( ( { delta } ) => mixer . update ( delta ) ) ;
45
44
46
- effect (
47
- ( ) => {
48
- const actual = actualRef . nativeElement ;
49
- if ( ! actual ) return ;
50
- const animations = animationsFactory ( ) ;
45
+ const ready = computed ( ( ) => ! ! actualRef . nativeElement && ! ! animationsFactory ( ) . length ) ;
51
46
52
- for ( let i = 0 ; i < animations . length ; i ++ ) {
53
- const clip = animations [ i ] ;
47
+ effect ( ( ) => {
48
+ const actual = actualRef . nativeElement ;
49
+ if ( ! actual ) return ;
50
+ const animations = animationsFactory ( ) ;
54
51
55
- names . push ( clip . name ) ;
56
- clips . push ( clip ) ;
52
+ for ( let i = 0 ; i < animations . length ; i ++ ) {
53
+ const clip = animations [ i ] ;
57
54
58
- Object . defineProperty ( actions , clip . name , {
59
- enumerable : true ,
60
- get : ( ) => {
61
- return cached [ clip . name ] || ( cached [ clip . name ] = mixer . clipAction ( clip , actual ) ) ;
62
- } ,
63
- } ) ;
55
+ names . push ( clip . name ) ;
56
+ clips . push ( clip ) ;
64
57
65
- if ( i === 0 && playFirstClip ) {
66
- actions [ clip . name ] . play ( ) ;
67
- }
68
- }
58
+ Object . defineProperty ( actions , clip . name , {
59
+ enumerable : true ,
60
+ get : ( ) => {
61
+ return cached [ clip . name ] || ( cached [ clip . name ] = mixer . clipAction ( clip , actual ) ) ;
62
+ } ,
63
+ } ) ;
69
64
70
- if ( ! untracked ( ready ) ) {
71
- ready . set ( true ) ;
65
+ if ( i === 0 && playFirstClip ) {
66
+ actions [ clip . name ] . play ( ) ;
72
67
}
73
- } ,
74
- { allowSignalWrites : true } ,
75
- ) ;
68
+ }
69
+ } ) ;
76
70
77
71
return { ref : actualRef , actions, mixer, names, clips, ready } ;
78
72
} ) ;
0 commit comments