Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: pmndrs/drei
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v9.80.1
Choose a base ref
...
head repository: pmndrs/drei
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v9.80.2
Choose a head ref
  • 2 commits
  • 1 file changed
  • 1 contributor

Commits on Aug 10, 2023

  1. 1

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    d91d980 View commit details
  2. 1

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    7217da8 View commit details
Showing with 7 additions and 9 deletions.
  1. +7 −9 src/core/useAnimations.tsx
16 changes: 7 additions & 9 deletions src/core/useAnimations.tsx
Original file line number Diff line number Diff line change
@@ -18,9 +18,12 @@ export function useAnimations<T extends AnimationClip>(
const [actualRef] = React.useState(() => (root ? (root instanceof Object3D ? { current: root } : root) : ref))
// eslint-disable-next-line prettier/prettier
const [mixer] = React.useState(() => new AnimationMixer(undefined as unknown as Object3D))
React.useLayoutEffect(() => void ((mixer as any)._root = actualRef.current), [mixer, root])
React.useLayoutEffect(() => {
actualRef.current = root && (root instanceof Object3D ? root : root.current)
;(mixer as any)._root = actualRef.current
}, [root])
const lazyActions = React.useRef({})
const [api] = React.useState<Api<T>>(() => {
const api = React.useMemo<Api<T>>(() => {
const actions = {} as { [key in T['name']]: AnimationAction | null }
clips.forEach((clip) =>
Object.defineProperty(actions, clip.name, {
@@ -37,13 +40,14 @@ export function useAnimations<T extends AnimationClip>(
})
)
return { ref: actualRef, clips, actions, names: clips.map((c) => c.name), mixer }
})
}, [clips])
useFrame((state, delta) => mixer.update(delta))
React.useEffect(() => {
const currentRoot = actualRef.current
return () => {
// Clean up only when clips change, wipe out lazy actions and uncache clips
lazyActions.current = {}
mixer.stopAllAction()
Object.values(api.actions).forEach((action) => {
if (currentRoot) {
mixer.uncacheAction(action as AnimationClip, currentRoot)
@@ -52,11 +56,5 @@ export function useAnimations<T extends AnimationClip>(
}
}, [clips])

React.useEffect(() => {
return () => {
mixer.stopAllAction()
}
}, [mixer])

return api
}