1- import { afterNextRender , Directive , input } from '@angular/core' ;
2- import { injectBeforeRender } from 'angular-three' ;
3- import { injectAutoEffect } from 'ngxtension/auto-effect' ;
1+ import { Directive , effect , input } from '@angular/core' ;
2+ import { injectStore } from 'angular-three' ;
43import { NgtrPhysicsOptions } from './types' ;
54
65@Directive ( { standalone : true , selector : 'ngtr-frame-stepper' } )
@@ -11,39 +10,37 @@ export class NgtrFrameStepper {
1110 type = input . required < NgtrPhysicsOptions [ 'updateLoop' ] > ( ) ;
1211
1312 constructor ( ) {
14- const autoEffect = injectAutoEffect ( ) ;
13+ const store = injectStore ( ) ;
1514
16- afterNextRender ( ( ) => {
17- autoEffect ( ( injector ) => {
18- const ready = this . ready ( ) ;
19- if ( ! ready ) return ;
15+ effect ( ( onCleanup ) => {
16+ const ready = this . ready ( ) ;
17+ if ( ! ready ) return ;
2018
21- const [ type , updatePriority , stepFn ] = [ this . type ( ) , this . updatePriority ( ) , this . stepFn ( ) ] ;
22- if ( type === 'follow' ) {
23- return injectBeforeRender (
24- ( { delta } ) => {
25- stepFn ( delta ) ;
26- } ,
27- { priority : updatePriority , injector } ,
28- ) ;
29- }
30-
31- let lastFrame = 0 ;
32- let raf : ReturnType < typeof requestAnimationFrame > = 0 ;
33- const loop = ( ) => {
34- const now = performance . now ( ) ;
35- const delta = now - lastFrame ;
36- raf = requestAnimationFrame ( loop ) ;
37- stepFn ( delta ) ;
38- lastFrame = now ;
39- } ;
19+ const [ type , updatePriority , stepFn ] = [ this . type ( ) , this . updatePriority ( ) , this . stepFn ( ) ] ;
20+ if ( type === 'follow' ) {
21+ const cleanup = store . snapshot . internal . subscribe (
22+ ( { delta } ) => {
23+ stepFn ( delta ) ;
24+ } ,
25+ updatePriority ,
26+ store ,
27+ ) ;
28+ onCleanup ( ( ) => cleanup ( ) ) ;
29+ return ;
30+ }
4031
32+ let lastFrame = 0 ;
33+ let raf : ReturnType < typeof requestAnimationFrame > = 0 ;
34+ const loop = ( ) => {
35+ const now = performance . now ( ) ;
36+ const delta = now - lastFrame ;
4137 raf = requestAnimationFrame ( loop ) ;
38+ stepFn ( delta ) ;
39+ lastFrame = now ;
40+ } ;
4241
43- return ( ) => {
44- cancelAnimationFrame ( raf ) ;
45- } ;
46- } ) ;
42+ raf = requestAnimationFrame ( loop ) ;
43+ onCleanup ( ( ) => cancelAnimationFrame ( raf ) ) ;
4744 } ) ;
4845 }
4946}
0 commit comments