Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3d93781

Browse files
committed
refactor(soba/misc): use computed for animations API instead of effect
1 parent 771d55f commit 3d93781

File tree

1 file changed

+27
-29
lines changed

1 file changed

+27
-29
lines changed

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

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { computed, effect, ElementRef, Injector, isSignal, signal, untracked } from '@angular/core';
1+
import { computed, DestroyRef, ElementRef, inject, Injector, isSignal } from '@angular/core';
22
import { beforeRender, resolveRef } from 'angular-three';
33
import { assertInjector } from 'ngxtension/assert-injector';
44
import * as THREE from 'three';
@@ -58,19 +58,13 @@ export function animations<TAnimation extends NgtsAnimationClip>(
5858
const clips = [] as NgtsAnimationApi<TAnimation>['clips'];
5959
const names = [] as NgtsAnimationApi<TAnimation>['names'];
6060

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+
);
6864

69-
const ready = signal(false);
70-
71-
effect((onCleanup) => {
65+
const isReady = computed(() => {
7266
const obj = actualObject() as THREE.Object3D | undefined;
73-
if (!obj) return;
67+
if (!obj) return false;
7468

7569
Object.assign(mixer, { _root: obj });
7670

@@ -101,27 +95,31 @@ export function animations<TAnimation extends NgtsAnimationClip>(
10195
}
10296
}
10397

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;
11899
});
119100

120-
const result = { clips, mixer, actions, names } as NgtsAnimationApi<TAnimation>;
101+
inject(DestroyRef).onDestroy(() => {
102+
const obj = actualObject() as THREE.Object3D | undefined;
121103

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

124-
return result;
114+
return {
115+
clips,
116+
mixer,
117+
actions,
118+
names,
119+
get isReady() {
120+
return isReady();
121+
},
122+
} as NgtsAnimationApi<TAnimation>;
125123
});
126124
}
127125

0 commit comments

Comments
 (0)