Skip to content

Commit 3ca940d

Browse files
committed
feat(soba): add isReady getter for animation api to help with type-narrow
deprecate: ready()
1 parent ea878e1 commit 3ca940d

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@ export type NgtsAnimationClips<TAnimationNames extends string> = {
1212
[Name in TAnimationNames]: Omit<NgtsAnimationClip, 'name'> & { name: Name };
1313
}[TAnimationNames];
1414
export type NgtsAnimationApi<T extends NgtsAnimationClip> = {
15+
/**
16+
* Whether or not the animations finishes initialized
17+
*
18+
* @deprecated 3.5.0 - use `isReady` getter for better type-narrow instead. Will be removed in 4.0.0
19+
*/
1520
ready: Signal<boolean>;
1621
clips: T[];
1722
mixer: AnimationMixer;
1823
names: T['name'][];
19-
actions: { [key in T['name']]: AnimationAction | null };
20-
};
24+
} & ({ get isReady(): true; actions: { [key in T['name']]: AnimationAction } } | { get isReady(): false });
2125

2226
export type NgtsAnimation<TAnimation extends NgtsAnimationClip = NgtsAnimationClip> =
2327
| TAnimation[]
@@ -30,7 +34,7 @@ export function injectAnimations<TAnimation extends NgtsAnimationClip>(
3034
animations: () => NgtsAnimation<TAnimation> | undefined | null,
3135
object: ElementRef<Object3D> | Object3D | (() => ElementRef<Object3D> | Object3D | undefined | null),
3236
{ injector }: { injector?: Injector } = {},
33-
) {
37+
): NgtsAnimationApi<TAnimation> {
3438
return assertInjector(injectAnimations, injector, () => {
3539
const mixer = new AnimationMixer(null!);
3640
injectBeforeRender(({ delta }) => {
@@ -39,7 +43,7 @@ export function injectAnimations<TAnimation extends NgtsAnimationClip>(
3943
});
4044

4145
let cached = {} as Record<string, AnimationAction>;
42-
const actions = {} as NgtsAnimationApi<TAnimation>['actions'];
46+
const actions = {} as { [key in TAnimation['name']]: AnimationAction };
4347
const clips = [] as NgtsAnimationApi<TAnimation>['clips'];
4448
const names = [] as NgtsAnimationApi<TAnimation>['names'];
4549

@@ -99,6 +103,10 @@ export function injectAnimations<TAnimation extends NgtsAnimationClip>(
99103
});
100104
});
101105

102-
return { clips, mixer, actions, names, ready };
106+
const result = { ready, clips, mixer, actions, names } as unknown as NgtsAnimationApi<TAnimation>;
107+
108+
Object.defineProperty(result, 'isReady', { get: ready });
109+
110+
return result;
103111
});
104112
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ export class BotAnimations {
3838
// that the animations are referring to.
3939
const animationsApi = injectAnimations(this.animations, host);
4040
effect((onCleanup) => {
41-
if (animationsApi.ready()) {
41+
if (animationsApi.isReady) {
4242
const actionName = this.animation();
43-
animationsApi.actions[actionName]?.reset().fadeIn(0.5).play();
43+
animationsApi.actions[actionName].reset().fadeIn(0.5).play();
4444
onCleanup(() => {
45-
animationsApi.actions[actionName]?.fadeOut(0.5);
45+
animationsApi.actions[actionName].fadeOut(0.5);
4646
});
4747
}
4848
});

0 commit comments

Comments
 (0)