Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use useCallback in useAudio #414

Merged
merged 1 commit into from
Dec 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import styled from "styled-components";
import { IRadio } from "@/api/RadioApi";
import { color } from "@/constants/styles";

import useAudio from "@/utils/hooks/useAudio";
import { useAudio } from "@/utils/hooks/useAudio";
import PlayButtonProgress from "@/components/atoms/RadioCard/RadioCardPlayButtonProgress";
import SeekBar from "@/components/atoms/RadioCard/RadioCardSeekBar";

Expand Down Expand Up @@ -39,7 +39,7 @@ export const calcProgress = (times: {

export default (props: Pick<IRadio, "mp3" | "duration" | "playTime">) => {
const { mp3, duration, playTime } = props;
const { isPlay, play, pause, jump, times } = useAudio({
const { isPlaying, play, pause, jump, times } = useAudio({
url: mp3.url!,
duration
});
Expand All @@ -49,7 +49,7 @@ export default (props: Pick<IRadio, "mp3" | "duration" | "playTime">) => {
<Controller>
<PlayButtonProgressWrapper>
<ButtonWrapper>
{!isPlay ? (
{!isPlaying ? (
times.currentTime === times.duration ? (
<RePlayButton className="fas fa-redo-alt" onClick={play} />
) : (
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/atoms/RadioCard/RadioCardSeekBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import styled from "styled-components";

import { color } from "@/constants/styles";
import { IRadio } from "@/api/RadioApi";
import { IUseAudio } from "@/utils/hooks/useAudio";
import { UseAudio } from "@/utils/hooks/useAudio";

interface IProps extends Pick<IRadio, "duration">, Pick<IUseAudio, "jump"> {
interface IProps extends Pick<IRadio, "duration">, Pick<UseAudio, "jump"> {
currentTime: number;
}

Expand Down
31 changes: 18 additions & 13 deletions frontend/src/utils/hooks/useAudio.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react";
import { useState, useCallback, useEffect } from "react";

export interface IUseAudio {
isPlay: boolean;
export interface UseAudio {
isPlaying: boolean;
play: () => void;
pause: () => void;
jump: (value: number) => void;
Expand All @@ -11,17 +11,20 @@ export interface IUseAudio {
};
}

interface IProps {
interface Props {
url: string;
duration: number;
}

export default ({ url, duration }: IProps): IUseAudio => {
const [audio] = React.useState(new Audio());
const [, _forceUpdate] = React.useState(false);
const forceUpdate = () => _forceUpdate(prevState => !prevState);
export const useAudio = ({ url, duration }: Props): UseAudio => {
const [audio] = useState(new Audio());
const [, setForceUpdate] = useState(false);
const forceUpdate = useCallback(
() => setForceUpdate(prevState => !prevState),
[]
);

React.useEffect(() => {
useEffect(() => {
audio.preload = "none";
audio.src = url;

Expand All @@ -38,12 +41,14 @@ export default ({ url, duration }: IProps): IUseAudio => {
};
}, []);

const play = () => audio.play();
const pause = () => audio.pause();
const jump = (value: number) => (audio.currentTime = value);
const play = useCallback(() => audio.play(), [audio]);
const pause = useCallback(() => audio.pause(), [audio]);
const jump = useCallback((value: number) => (audio.currentTime = value), [
audio
]);

return {
isPlay: !audio.paused,
isPlaying: !audio.paused,
play,
pause,
jump,
Expand Down