|
1 | 1 | import { useEffect, useLayoutEffect, useRef, useState } from "react";
|
2 | 2 | import { isSameObject } from "../utils";
|
3 | 3 | import { AnimationOptions, createAnimation, createHandle } from "./core";
|
4 |
| -import type { AnimationHandle } from "./useAnimation"; |
| 4 | + |
| 5 | +export type AnimationFunctionHandle = { |
| 6 | + play: () => AnimationFunctionHandle; |
| 7 | + replay: () => AnimationFunctionHandle; |
| 8 | + reverse: () => AnimationFunctionHandle; |
| 9 | + cancel: () => AnimationFunctionHandle; |
| 10 | + finish: () => AnimationFunctionHandle; |
| 11 | + pause: () => AnimationFunctionHandle; |
| 12 | + setTime: (time: number) => AnimationFunctionHandle; |
| 13 | + setPlaybackRate: ( |
| 14 | + rate: number | ((prevRate: number) => number) |
| 15 | + ) => AnimationFunctionHandle; |
| 16 | + end: () => Promise<void>; |
| 17 | +}; |
5 | 18 |
|
6 | 19 | export type ComputedTimingContext = Required<
|
7 | 20 | {
|
@@ -31,82 +44,80 @@ const bindUpdateFunction = (
|
31 | 44 | export const useAnimationFunction = (
|
32 | 45 | onUpdate: AnimationFunction,
|
33 | 46 | options?: AnimationOptions
|
34 |
| -): AnimationHandle => { |
| 47 | +): AnimationFunctionHandle => { |
35 | 48 | const onUpdateRef = useRef(onUpdate);
|
36 | 49 | const optionsRef = useRef(options);
|
37 | 50 |
|
38 |
| - const [animation, cleanup] = useState<[AnimationHandle, () => void]>(() => { |
39 |
| - const getOnUpdate = () => onUpdateRef.current; |
40 |
| - const getOptions = () => optionsRef.current; |
| 51 | + const [animation, cleanup] = useState<[AnimationFunctionHandle, () => void]>( |
| 52 | + () => { |
| 53 | + const getOnUpdate = () => onUpdateRef.current; |
| 54 | + const getOptions = () => optionsRef.current; |
41 | 55 |
|
42 |
| - let cache: [Animation, AnimationOptions | undefined] | undefined; |
43 |
| - const initAnimation = ( |
44 |
| - options: AnimationOptions | undefined |
45 |
| - ): Animation => { |
46 |
| - if (cache) { |
47 |
| - const [prevAnimation, prevOptions] = cache; |
48 |
| - if (isSameObject(options, prevOptions)) { |
49 |
| - if (prevAnimation.playState !== "running") { |
50 |
| - bindUpdateFunction(prevAnimation, getOnUpdate); |
| 56 | + let cache: [Animation, AnimationOptions | undefined] | undefined; |
| 57 | + const initAnimation = ( |
| 58 | + options: AnimationOptions | undefined |
| 59 | + ): Animation => { |
| 60 | + if (cache) { |
| 61 | + const [prevAnimation, prevOptions] = cache; |
| 62 | + if (isSameObject(options, prevOptions)) { |
| 63 | + if (prevAnimation.playState !== "running") { |
| 64 | + bindUpdateFunction(prevAnimation, getOnUpdate); |
| 65 | + } |
| 66 | + return prevAnimation; |
51 | 67 | }
|
52 |
| - return prevAnimation; |
| 68 | + prevAnimation.cancel(); |
53 | 69 | }
|
54 |
| - prevAnimation.cancel(); |
55 |
| - } |
56 |
| - const animation = createAnimation(null, null, options); |
57 |
| - bindUpdateFunction(animation, getOnUpdate); |
58 |
| - cache = [animation, options]; |
59 |
| - return animation; |
60 |
| - }; |
61 |
| - const getAnimation = () => cache?.[0]; |
62 |
| - const handle = createHandle(); |
| 70 | + const animation = createAnimation(null, null, options); |
| 71 | + bindUpdateFunction(animation, getOnUpdate); |
| 72 | + cache = [animation, options]; |
| 73 | + return animation; |
| 74 | + }; |
| 75 | + const getAnimation = () => cache?.[0]; |
| 76 | + const handle = createHandle(); |
63 | 77 |
|
64 |
| - const externalHandle: AnimationHandle = { |
65 |
| - play: () => { |
66 |
| - handle._play(initAnimation(getOptions())); |
67 |
| - return externalHandle; |
68 |
| - }, |
69 |
| - replay: () => { |
70 |
| - handle._replay(initAnimation(getOptions())); |
71 |
| - return externalHandle; |
72 |
| - }, |
73 |
| - reverse: () => { |
74 |
| - handle._reverse(initAnimation(getOptions())); |
75 |
| - return externalHandle; |
76 |
| - }, |
77 |
| - cancel: () => { |
78 |
| - handle._cancel(getAnimation()); |
79 |
| - return externalHandle; |
80 |
| - }, |
81 |
| - finish: () => { |
82 |
| - handle._finish(getAnimation()); |
83 |
| - return externalHandle; |
84 |
| - }, |
85 |
| - pause: () => { |
86 |
| - handle._pause(getAnimation()); |
87 |
| - return externalHandle; |
88 |
| - }, |
89 |
| - commit: () => { |
90 |
| - handle._commit(getAnimation()); |
91 |
| - return externalHandle; |
92 |
| - }, |
93 |
| - setTime: (time) => { |
94 |
| - handle._setTime(getAnimation(), time); |
95 |
| - return externalHandle; |
96 |
| - }, |
97 |
| - setPlaybackRate: (rate) => { |
98 |
| - handle._setRate(getAnimation(), rate); |
99 |
| - return externalHandle; |
100 |
| - }, |
101 |
| - end: () => handle._end(getAnimation()), |
102 |
| - }; |
103 |
| - return [ |
104 |
| - externalHandle, |
105 |
| - () => { |
106 |
| - handle._cancel(getAnimation()); |
107 |
| - }, |
108 |
| - ]; |
109 |
| - })[0]; |
| 78 | + const externalHandle: AnimationFunctionHandle = { |
| 79 | + play: () => { |
| 80 | + handle._play(initAnimation(getOptions())); |
| 81 | + return externalHandle; |
| 82 | + }, |
| 83 | + replay: () => { |
| 84 | + handle._replay(initAnimation(getOptions())); |
| 85 | + return externalHandle; |
| 86 | + }, |
| 87 | + reverse: () => { |
| 88 | + handle._reverse(initAnimation(getOptions())); |
| 89 | + return externalHandle; |
| 90 | + }, |
| 91 | + cancel: () => { |
| 92 | + handle._cancel(getAnimation()); |
| 93 | + return externalHandle; |
| 94 | + }, |
| 95 | + finish: () => { |
| 96 | + handle._finish(getAnimation()); |
| 97 | + return externalHandle; |
| 98 | + }, |
| 99 | + pause: () => { |
| 100 | + handle._pause(getAnimation()); |
| 101 | + return externalHandle; |
| 102 | + }, |
| 103 | + setTime: (time) => { |
| 104 | + handle._setTime(getAnimation(), time); |
| 105 | + return externalHandle; |
| 106 | + }, |
| 107 | + setPlaybackRate: (rate) => { |
| 108 | + handle._setRate(getAnimation(), rate); |
| 109 | + return externalHandle; |
| 110 | + }, |
| 111 | + end: () => handle._end(getAnimation()), |
| 112 | + }; |
| 113 | + return [ |
| 114 | + externalHandle, |
| 115 | + () => { |
| 116 | + handle._cancel(getAnimation()); |
| 117 | + }, |
| 118 | + ]; |
| 119 | + } |
| 120 | + )[0]; |
110 | 121 |
|
111 | 122 | useLayoutEffect(() => {
|
112 | 123 | onUpdateRef.current = onUpdate;
|
|
0 commit comments