1- import { DestroyRef , Injector , computed , effect , inject , runInInjectionContext } from '@angular/core' ;
1+ import { Injector , computed , effect , runInInjectionContext } from '@angular/core' ;
22import { assertInjectionContext , injectBeforeRender , injectNgtRef , type NgtRef } from 'angular-three' ;
33import * as THREE from 'three' ;
44
@@ -30,24 +30,11 @@ export function injectNgtsAnimations(
3030 const clips = [ ] as THREE . AnimationClip [ ] ;
3131 const names = [ ] as string [ ] ;
3232
33- inject ( DestroyRef ) . onDestroy ( ( ) => {
34- // clear cached
35- cached = { } ;
36- // uncache actions
37- Object . values ( actions ) . forEach ( ( action ) => {
38- mixer . uncacheAction ( action as unknown as THREE . AnimationClip , object ! ) ;
39- } ) ;
40- // stop all actions
41- mixer . stopAllAction ( ) ;
42-
43- object = null ;
44- } ) ;
45-
4633 injectBeforeRender ( ( { delta } ) => mixer . update ( delta ) ) ;
4734
4835 const ready = computed ( ( ) => ! ! actualRef . nativeElement && ! ! animationsFactory ( ) . length ) ;
4936
50- effect ( ( ) => {
37+ effect ( ( onCleanup ) => {
5138 const actual = actualRef . nativeElement ;
5239 if ( ! actual ) return ;
5340 object = actual ;
@@ -70,6 +57,19 @@ export function injectNgtsAnimations(
7057 actions [ clip . name ] . play ( ) ;
7158 }
7259 }
60+
61+ onCleanup ( ( ) => {
62+ // clear cached
63+ cached = { } ;
64+ // stop all actions
65+ mixer . stopAllAction ( ) ;
66+ // uncache actions
67+ Object . values ( actions ) . forEach ( ( action ) => {
68+ mixer . uncacheAction ( action as unknown as THREE . AnimationClip , object ! ) ;
69+ } ) ;
70+
71+ object = null ;
72+ } ) ;
7373 } ) ;
7474
7575 return { ref : actualRef , actions, mixer, names, clips, ready } ;
0 commit comments