Skip to content

Commit c13b9bb

Browse files
committed
adjust inject animations
1 parent 5a3a419 commit c13b9bb

File tree

1 file changed

+20
-26
lines changed

1 file changed

+20
-26
lines changed

libs/soba/misc/src/animations/animations.ts

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DestroyRef, Injector, effect, inject, runInInjectionContext, signal, untracked } from '@angular/core';
1+
import { DestroyRef, Injector, computed, effect, inject, runInInjectionContext } from '@angular/core';
22
import { assertInjectionContext, injectBeforeRender, injectNgtRef, type NgtInjectedRef } from 'angular-three';
33
import * as THREE from 'three';
44

@@ -23,7 +23,6 @@ export function injectNgtsAnimations(
2323
}
2424

2525
const mixer = new THREE.AnimationMixer(null!);
26-
const ready = signal(false);
2726
const actions = {} as Record<string, THREE.AnimationAction>;
2827
let cached = {} as Record<string, THREE.AnimationAction>;
2928

@@ -43,36 +42,31 @@ export function injectNgtsAnimations(
4342

4443
injectBeforeRender(({ delta }) => mixer.update(delta));
4544

46-
effect(
47-
() => {
48-
const actual = actualRef.nativeElement;
49-
if (!actual) return;
50-
const animations = animationsFactory();
45+
const ready = computed(() => !!actualRef.nativeElement && !!animationsFactory().length);
5146

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();
5451

55-
names.push(clip.name);
56-
clips.push(clip);
52+
for (let i = 0; i < animations.length; i++) {
53+
const clip = animations[i];
5754

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);
6457

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+
});
6964

70-
if (!untracked(ready)) {
71-
ready.set(true);
65+
if (i === 0 && playFirstClip) {
66+
actions[clip.name].play();
7267
}
73-
},
74-
{ allowSignalWrites: true },
75-
);
68+
}
69+
});
7670

7771
return { ref: actualRef, actions, mixer, names, clips, ready };
7872
});

0 commit comments

Comments
 (0)