Skip to content

Commit

Permalink
Change end method to waitFor and support waiting reverseFinish
Browse files Browse the repository at this point in the history
  • Loading branch information
inokawa committed Nov 24, 2022
1 parent 9e364e8 commit b7f9efd
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 16 deletions.
21 changes: 18 additions & 3 deletions src/core/waapi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { CSSProperties } from "react";
import { getKeys, getStyle, noop, toArray, uniqBy } from "./utils";
import { getKeys, getStyle, toArray, uniqBy } from "./utils";

export type AnimatableCSSProperties = Omit<
CSSProperties,
Expand Down Expand Up @@ -125,7 +125,22 @@ export const _setRate = (
typeof arg === "function" ? arg(animation.playbackRate) : arg
);
};
export const _end = (animation: Animation | undefined) => {

export type WaitingAnimationEventName = "finish" | "reverseFinish";
export const _waitFor = (
animation: Animation | undefined,
name: WaitingAnimationEventName
): Promise<void> => {
if (!animation) return Promise.resolve();
return animation.finished.then(noop);

return new Promise<void>((resolve) => {
animation.onfinish = () => {
if (
(name === "finish" && animation.playbackRate > 0) ||
(name === "reverseFinish" && animation.playbackRate < 0)
) {
resolve();
}
};
});
};
8 changes: 5 additions & 3 deletions src/react/hooks/useAnimation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import {
ReverseOptions,
TypedKeyframe,
_cancel,
_end,
_waitFor,
_finish,
_pause,
_play,
_reverse,
_setRate,
_setTime,
WaitingAnimationEventName,
} from "../../core/waapi";
import { useIsomorphicLayoutEffect } from "./useIsomorphicLayoutEffect";

Expand All @@ -39,7 +40,7 @@ export type AnimationHandle<Args = void> = {
setPlaybackRate: (
rate: number | ((prevRate: number) => number)
) => AnimationHandle<Args>;
end: () => Promise<AnimationHandle<Args>>;
waitFor: (event: WaitingAnimationEventName) => Promise<AnimationHandle<Args>>;
};

export const useAnimation = <Args = void>(
Expand Down Expand Up @@ -129,7 +130,8 @@ export const useAnimation = <Args = void>(
_setRate(getAnimation(), rate);
return externalHandle;
},
end: () => _end(getAnimation()).then(() => externalHandle),
waitFor: (event: WaitingAnimationEventName) =>
_waitFor(getAnimation(), event).then(() => externalHandle),
}
);

Expand Down
10 changes: 7 additions & 3 deletions src/react/hooks/useAnimationFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import {
PlayOptions,
ReverseOptions,
_cancel,
_end,
_waitFor,
_finish,
_pause,
_play,
_reverse,
_setRate,
_setTime,
WaitingAnimationEventName,
} from "../../core/waapi";
import { useIsomorphicLayoutEffect } from "./useIsomorphicLayoutEffect";

Expand All @@ -35,7 +36,9 @@ export type AnimationFunctionHandle<Args = void> = {
setPlaybackRate: (
rate: number | ((prevRate: number) => number)
) => AnimationFunctionHandle<Args>;
end: () => Promise<AnimationFunctionHandle<Args>>;
waitFor: (
event: WaitingAnimationEventName
) => Promise<AnimationFunctionHandle<Args>>;
};

export type ComputedTimingContext = Required<
Expand Down Expand Up @@ -137,7 +140,8 @@ export const useAnimationFunction = <Args = void>(
_setRate(getAnimation(), rate);
return externalHandle;
},
end: () => _end(getAnimation()).then(() => externalHandle),
waitFor: (event: WaitingAnimationEventName) =>
_waitFor(getAnimation(), event).then(() => externalHandle),
};
return [
externalHandle,
Expand Down
2 changes: 1 addition & 1 deletion src/react/hooks/useTransitionAnimation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export const useTransitionAnimation = (keyframes: {

animationsRef.current[currentState]
?.play()
.end()
.waitFor("finish")
.then(() => {
if (currentState === "exit") {
notify(EXITED);
Expand Down
12 changes: 6 additions & 6 deletions stories/hooks/useAnimation.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -688,9 +688,9 @@ export const Sequence: StoryObj = {

const onClickAll = useCallback(async () => {
try {
await animate.play({ args: "red" }).end();
await animate.play({ args: "blue" }).end();
await animate.play({ args: "green" }).end();
await animate.play({ args: "red" }).waitFor("finish");
await animate.play({ args: "blue" }).waitFor("finish");
await animate.play({ args: "green" }).waitFor("finish");
} catch (e) {
// ignore uncaught promise error
}
Expand Down Expand Up @@ -911,11 +911,11 @@ const Block = ({ i, length: n }: { i: number; length: number }) => {
three.cancel();
const run = async () => {
try {
await one.play().end();
await one.play().waitFor("finish");
one.cancel();
await two.play().end();
await two.play().waitFor("finish");
two.cancel();
await three.play().end();
await three.play().waitFor("finish");
three.cancel();
run();
} catch (e) {
Expand Down

0 comments on commit b7f9efd

Please sign in to comment.