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

fix(hooks): useProgress & usePlayback hooks #1723

Merged
merged 2 commits into from
Sep 14, 2022
Merged
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
19 changes: 12 additions & 7 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import { useEffect, useRef, useState } from 'react';
import { Event, EventsPayloadByEvent, Progress, State } from './interfaces';
import TrackPlayer from './trackPlayer';

/** Get current playback state and subsequent updates */
export const usePlaybackState = () => {
const [state, setState] = useState(State.None);
const usePlaybackStateWithoutInitialValue = () => {
const [state, setState] = useState<State | undefined>(undefined);
useEffect(() => {
let mounted = true;

Expand Down Expand Up @@ -35,6 +34,12 @@ export const usePlaybackState = () => {
return state;
};

/** Get current playback state and subsequent updates */
export const usePlaybackState = () => {
const state = usePlaybackStateWithoutInitialValue();
return state ?? State.None;
};

/**
* Attaches a handler to the given TrackPlayer events and performs cleanup on unmount
* @param events - TrackPlayer events to subscribe to
Expand Down Expand Up @@ -88,11 +93,11 @@ export function useProgress(updateInterval = 1000) {
duration: 0,
buffered: 0,
});
const playerState = usePlaybackState();

const playerState = usePlaybackStateWithoutInitialValue();
const isNone = playerState === State.None;
useEffect(() => {
let mounted = true;
if (playerState === State.None) {
if (isNone) {
setState({ position: 0, duration: 0, buffered: 0 });
return;
}
Expand Down Expand Up @@ -131,7 +136,7 @@ export function useProgress(updateInterval = 1000) {
return () => {
mounted = false;
};
}, [playerState, updateInterval]);
}, [isNone, updateInterval]);

return state;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@puckey would doing a:

return state || State.None;

Allow us to avoid the breaking change here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not in this way, but there is slightly more convoluted way to fix this.. Will take a look at it later

}