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' ;
4
3
import { NgtrPhysicsOptions } from './types' ;
5
4
6
5
@Directive ( { standalone : true , selector : 'ngtr-frame-stepper' } )
@@ -11,39 +10,37 @@ export class NgtrFrameStepper {
11
10
type = input . required < NgtrPhysicsOptions [ 'updateLoop' ] > ( ) ;
12
11
13
12
constructor ( ) {
14
- const autoEffect = injectAutoEffect ( ) ;
13
+ const store = injectStore ( ) ;
15
14
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 ;
20
18
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
+ }
40
31
32
+ let lastFrame = 0 ;
33
+ let raf : ReturnType < typeof requestAnimationFrame > = 0 ;
34
+ const loop = ( ) => {
35
+ const now = performance . now ( ) ;
36
+ const delta = now - lastFrame ;
41
37
raf = requestAnimationFrame ( loop ) ;
38
+ stepFn ( delta ) ;
39
+ lastFrame = now ;
40
+ } ;
42
41
43
- return ( ) => {
44
- cancelAnimationFrame ( raf ) ;
45
- } ;
46
- } ) ;
42
+ raf = requestAnimationFrame ( loop ) ;
43
+ onCleanup ( ( ) => cancelAnimationFrame ( raf ) ) ;
47
44
} ) ;
48
45
}
49
46
}
0 commit comments