Skip to content

Commit

Permalink
chore: add better typedocs
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeocodes committed Apr 3, 2024
1 parent eaa396d commit d143837
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,58 @@
import React from "react";
import { silence } from "./lib/contants";

/**
* React context for managing audio playback, including playing, stopping, and resuming audio tracks.
* This context provides a way to control audio playback and track the current playing audio.
*/
interface NowPlayingContext extends Partial<Omit<HTMLAudioElement, "play">> {
/**
* The HTMLAudioElement being used for playback.
*/
player: HTMLAudioElement | undefined;

/**
* Plays the given audio. If the audio is not a string, it will be converted to a blob URL.
* @param audio The audio source to play. Can be a MediaSource, Blob, or a string URL.
* @param type The MIME type of the audio. Defaults to "audio/mp3".
* @param uid An optional unique identifier for the audio source.
* @returns A promise that resolves when the audio starts playing.
*/
play: (
audio: MediaSource | Blob | string,
type?: string,
uid?: string
) => Promise<void>;

/**
* Resumes audio playback from the current position.
*/
resume: () => void;

/**
* Stops the audio playback and resets the player's currentTime to 0.
*/
stop: () => void;

/**
* An optional unique identifier for the currently playing audio source.
*/
uid: string | undefined;
}

/**
* Props for the NowPlayingContextProvider component, including children React nodes.
*/
interface NowPlayingContextInterface {
children: React.ReactNode;
}

const NowPlayingContext = React.createContext({} as NowPlayingContext);

/**
* Provides a React context for managing audio playback within the app.
* This component sets up the audio player and source elements, and provides play, stop, and resume functions.
*/
const NowPlayingContextProvider = ({
children,
}: NowPlayingContextInterface) => {
Expand Down Expand Up @@ -99,6 +133,10 @@ const NowPlayingContextProvider = ({
);
};

/**
* Custom hook to access the NowPlaying context.
* @returns The NowPlayingContext with audio playback control functions.
*/
function useNowPlaying() {
return React.useContext(NowPlayingContext);
}
Expand Down

1 comment on commit d143837

@vercel
Copy link

@vercel vercel bot commented on d143837 Apr 3, 2024

Choose a reason for hiding this comment

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

Please sign in to comment.