|
1 | | -import { computed, effect, ElementRef, Injector, isSignal, signal, untracked } from '@angular/core'; |
| 1 | +import { computed, DestroyRef, ElementRef, inject, Injector, isSignal } from '@angular/core'; |
2 | 2 | import { beforeRender, resolveRef } from 'angular-three'; |
3 | 3 | import { assertInjector } from 'ngxtension/assert-injector'; |
4 | 4 | import * as THREE from 'three'; |
@@ -58,19 +58,13 @@ export function animations<TAnimation extends NgtsAnimationClip>( |
58 | 58 | const clips = [] as NgtsAnimationApi<TAnimation>['clips']; |
59 | 59 | const names = [] as NgtsAnimationApi<TAnimation>['names']; |
60 | 60 |
|
61 | | - const actualObject = computed(() => { |
62 | | - if (isSignal(object) || typeof object === 'function') { |
63 | | - return resolveRef(object()); |
64 | | - } |
65 | | - |
66 | | - return resolveRef(object); |
67 | | - }); |
| 61 | + const actualObject = computed(() => |
| 62 | + isSignal(object) || typeof object === 'function' ? resolveRef(object()) : resolveRef(object), |
| 63 | + ); |
68 | 64 |
|
69 | | - const ready = signal(false); |
70 | | - |
71 | | - effect((onCleanup) => { |
| 65 | + const isReady = computed(() => { |
72 | 66 | const obj = actualObject() as THREE.Object3D | undefined; |
73 | | - if (!obj) return; |
| 67 | + if (!obj) return false; |
74 | 68 |
|
75 | 69 | Object.assign(mixer, { _root: obj }); |
76 | 70 |
|
@@ -101,27 +95,31 @@ export function animations<TAnimation extends NgtsAnimationClip>( |
101 | 95 | } |
102 | 96 | } |
103 | 97 |
|
104 | | - if (!untracked(ready)) { |
105 | | - ready.set(true); |
106 | | - } |
107 | | - |
108 | | - onCleanup(() => { |
109 | | - // clear cached |
110 | | - cached = {}; |
111 | | - // stop all actions |
112 | | - mixer.stopAllAction(); |
113 | | - // uncache actions |
114 | | - Object.values(actions).forEach((action) => { |
115 | | - mixer.uncacheAction(action as THREE.AnimationClip, obj); |
116 | | - }); |
117 | | - }); |
| 98 | + return true; |
118 | 99 | }); |
119 | 100 |
|
120 | | - const result = { clips, mixer, actions, names } as NgtsAnimationApi<TAnimation>; |
| 101 | + inject(DestroyRef).onDestroy(() => { |
| 102 | + const obj = actualObject() as THREE.Object3D | undefined; |
121 | 103 |
|
122 | | - Object.defineProperty(result, 'isReady', { get: ready }); |
| 104 | + // clear cached |
| 105 | + cached = {}; |
| 106 | + // stop all actions |
| 107 | + mixer.stopAllAction(); |
| 108 | + // uncache actions |
| 109 | + Object.values(actions).forEach((action) => { |
| 110 | + mixer.uncacheAction(action as THREE.AnimationClip, obj); |
| 111 | + }); |
| 112 | + }); |
123 | 113 |
|
124 | | - return result; |
| 114 | + return { |
| 115 | + clips, |
| 116 | + mixer, |
| 117 | + actions, |
| 118 | + names, |
| 119 | + get isReady() { |
| 120 | + return isReady(); |
| 121 | + }, |
| 122 | + } as NgtsAnimationApi<TAnimation>; |
125 | 123 | }); |
126 | 124 | } |
127 | 125 |
|
|
0 commit comments