From d545a853764f9c9d8e2f22083a76843d6d806ad7 Mon Sep 17 00:00:00 2001 From: nartc Date: Sun, 29 Sep 2024 17:14:43 -0500 Subject: [PATCH] fix(soba)!: inject animations generics --- libs/soba/misc/src/lib/animations.ts | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/libs/soba/misc/src/lib/animations.ts b/libs/soba/misc/src/lib/animations.ts index 353854f4..1e7fe86b 100644 --- a/libs/soba/misc/src/lib/animations.ts +++ b/libs/soba/misc/src/lib/animations.ts @@ -1,24 +1,22 @@ -import { computed, effect, ElementRef, Injector, isSignal, signal, untracked } from '@angular/core'; +import { computed, effect, ElementRef, Injector, isSignal, Signal, signal, untracked } from '@angular/core'; import { injectBeforeRender, resolveRef } from 'angular-three'; import { assertInjector } from 'ngxtension/assert-injector'; import { AnimationAction, AnimationClip, AnimationMixer, Object3D } from 'three'; -type NgtsAnimationApi = { +type NgtsAnimationApi = { clips: AnimationClip[]; mixer: AnimationMixer; - names: T['name'][]; - actions: { [key in T['name']]: AnimationAction | null }; + names: T[]; + actions: { [key in T]: AnimationAction | null }; }; -export type NgtsAnimation = - | TAnimation[] - | { animations: TAnimation[] }; +export type NgtsAnimation = AnimationClip[] | { animations: AnimationClip[] }; /** * Use afterNextRender */ -export function injectAnimations( - animations: () => NgtsAnimation | undefined | null, +export function injectAnimations( + animations: () => NgtsAnimation | undefined | null, object: ElementRef | Object3D | (() => ElementRef | Object3D | undefined | null), { injector }: { injector?: Injector } = {}, ) { @@ -30,9 +28,9 @@ export function injectAnimations( }); let cached = {} as Record; - const actions = {} as NgtsAnimationApi['actions']; - const clips = [] as NgtsAnimationApi['clips']; - const names = [] as NgtsAnimationApi['names']; + const actions = {} as NgtsAnimationApi['actions']; + const clips = [] as NgtsAnimationApi['clips']; + const names = [] as NgtsAnimationApi['names']; const actualObject = computed(() => { if (isSignal(object) || typeof object === 'function') { @@ -57,10 +55,10 @@ export function injectAnimations( for (let i = 0; i < animationClips.length; i++) { const clip = animationClips[i]; - names.push(clip.name); + names.push(clip.name as TAnimations); clips.push(clip); - if (!actions[clip.name as TAnimation['name']]) { + if (!actions[clip.name as TAnimations]) { Object.defineProperty(actions, clip.name, { enumerable: true, get: () => { @@ -92,6 +90,6 @@ export function injectAnimations( }); }); - return { clips, mixer, actions, names, ready }; + return { clips, mixer, actions, names, ready } as NgtsAnimationApi & { ready: Signal }; }); }