Skip to content

Commit 0e27857

Browse files
committed
fix(rapier): clean up framestepper; remove afterNextRender and autoEffect
1 parent 4d0918d commit 0e27857

File tree

1 file changed

+28
-31
lines changed

1 file changed

+28
-31
lines changed

libs/rapier/src/lib/frame-stepper.ts

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
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';
43
import { 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

Comments
 (0)