Skip to content

Commit b64622a

Browse files
committed
Refactor
1 parent e76d31a commit b64622a

File tree

3 files changed

+48
-32
lines changed

3 files changed

+48
-32
lines changed

src/core/waapi.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,32 +106,28 @@ export const _play = (animation: Animation, opts: PlayOptions = {}) => {
106106
/**
107107
* @internal
108108
*/
109-
export const _reverse = (animation: Animation | undefined) => {
110-
if (!animation) return;
109+
export const _reverse = (animation: Animation) => {
111110
animation.reverse();
112111
};
113112

114113
/**
115114
* @internal
116115
*/
117-
export const _cancel = (animation: Animation | undefined) => {
118-
if (!animation) return;
116+
export const _cancel = (animation: Animation) => {
119117
animation.cancel();
120118
};
121119

122120
/**
123121
* @internal
124122
*/
125-
export const _finish = (animation: Animation | undefined) => {
126-
if (!animation) return;
123+
export const _finish = (animation: Animation) => {
127124
animation.finish();
128125
};
129126

130127
/**
131128
* @internal
132129
*/
133-
export const _pause = (animation: Animation | undefined) => {
134-
if (!animation) return;
130+
export const _pause = (animation: Animation) => {
135131
animation.pause();
136132
};
137133
// /**
@@ -159,10 +155,9 @@ export const _pause = (animation: Animation | undefined) => {
159155
* @internal
160156
*/
161157
export const _setTime = (
162-
animation: Animation | undefined,
158+
animation: Animation,
163159
arg: number | ((endTime: number) => number)
164160
) => {
165-
if (!animation) return;
166161
animation.currentTime =
167162
typeof arg === "function"
168163
? arg(animation.effect!.getComputedTiming().endTime! as number)
@@ -173,10 +168,9 @@ export const _setTime = (
173168
* @internal
174169
*/
175170
export const _setRate = (
176-
animation: Animation | undefined,
171+
animation: Animation,
177172
arg: number | ((prevRate: number) => number)
178173
) => {
179-
if (!animation) return;
180174
animation.updatePlaybackRate(
181175
typeof arg === "function" ? arg(animation.playbackRate) : arg
182176
);

src/react/hooks/useAnimation.ts

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ export const useAnimation = <Args = void>(
111111
let element: Element | null = null;
112112
let active: AnimationObject | undefined;
113113

114+
const currentAnimation = () => active && getAnimation(active);
114115
const init = (args: Args) => {
115116
if (!element) return;
116117
const [keyframe, _options = {}] = argsRef.current;
@@ -149,31 +150,35 @@ export const useAnimation = <Args = void>(
149150
return externalHandle;
150151
},
151152
reverse: () => {
152-
if (active) {
153-
_reverse(getAnimation(active));
153+
const animation = currentAnimation();
154+
if (animation) {
155+
_reverse(animation);
154156
}
155157
return externalHandle;
156158
},
157159
cancel: () => {
158-
if (active) {
159-
_cancel(getAnimation(active));
160+
const animation = currentAnimation();
161+
if (animation) {
162+
_cancel(animation);
160163
}
161164
return externalHandle;
162165
},
163166
finish: () => {
164-
if (active) {
165-
_finish(getAnimation(active));
167+
const animation = currentAnimation();
168+
if (animation) {
169+
_finish(animation);
166170
}
167171
return externalHandle;
168172
},
169173
pause: () => {
170-
if (active) {
171-
_pause(getAnimation(active));
174+
const animation = currentAnimation();
175+
if (animation) {
176+
_pause(animation);
172177
}
173178
return externalHandle;
174179
},
175180
setTime: (time) => {
176-
let animation = active && getAnimation(active);
181+
let animation = currentAnimation();
177182
if (!animation) {
178183
const [keyframe] = argsRef.current;
179184
if (typeof keyframe === "function") {
@@ -182,20 +187,22 @@ export const useAnimation = <Args = void>(
182187
// Init animation in setTime to start animation without calling play
183188
animation = init(undefined!);
184189
}
185-
_setTime(animation, time);
190+
if (animation) {
191+
_setTime(animation, time);
192+
}
186193

187194
return externalHandle;
188195
},
189196
setPlaybackRate: (rate) => {
190-
if (active) {
191-
_setRate(getAnimation(active), rate);
197+
const animation = currentAnimation();
198+
if (animation) {
199+
_setRate(animation, rate);
192200
}
193201
return externalHandle;
194202
},
195203
waitFor: (event) => {
196-
return _waitFor(active && getAnimation(active), event).then(
197-
() => externalHandle
198-
);
204+
const animation = currentAnimation();
205+
return _waitFor(animation, event).then(() => externalHandle);
199206
},
200207
} satisfies BaseAnimationHandle<Args>
201208
);

src/react/hooks/useAnimationFunction.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,23 +108,38 @@ export const useAnimationFunction = <Args = void>(
108108
return externalHandle;
109109
},
110110
cancel: () => {
111-
_cancel(getAnimation());
111+
const animation = getAnimation();
112+
if (animation) {
113+
_cancel(animation);
114+
}
112115
return externalHandle;
113116
},
114117
finish: () => {
115-
_finish(getAnimation());
118+
const animation = getAnimation();
119+
if (animation) {
120+
_finish(animation);
121+
}
116122
return externalHandle;
117123
},
118124
pause: () => {
119-
_pause(getAnimation());
125+
const animation = getAnimation();
126+
if (animation) {
127+
_pause(animation);
128+
}
120129
return externalHandle;
121130
},
122131
setTime: (time) => {
123-
_setTime(getAnimation(), time);
132+
const animation = getAnimation();
133+
if (animation) {
134+
_setTime(animation, time);
135+
}
124136
return externalHandle;
125137
},
126138
setPlaybackRate: (rate) => {
127-
_setRate(getAnimation(), rate);
139+
const animation = getAnimation();
140+
if (animation) {
141+
_setRate(animation, rate);
142+
}
128143
return externalHandle;
129144
},
130145
waitFor: (event: WaitingAnimationEventName) =>

0 commit comments

Comments
 (0)