-
Notifications
You must be signed in to change notification settings - Fork 6
feat: add useError hook and use it in UiContainer #117
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import { useContext, useEffect, useState } from 'react'; | ||
| import { type ErrorEvent, type PlayerError, type PlayerEventMap, PlayerEventType } from 'react-native-theoplayer'; | ||
| import { PlayerContext } from '../barrel'; | ||
|
|
||
| const ERROR_CHANGE_EVENTS = [PlayerEventType.ERROR, PlayerEventType.SOURCE_CHANGE, PlayerEventType.LOAD_START] satisfies ReadonlyArray<keyof PlayerEventMap>; | ||
|
|
||
| type ErrorChangeEventType = (typeof ERROR_CHANGE_EVENTS)[number]; | ||
|
|
||
| /** | ||
| * Returns the current player error, or `undefined` if there is no error. | ||
| * Automatically updates when an error occurs, and resets when a new source is loaded. | ||
| * | ||
| * This hook must only be used in a component mounted inside a {@link THEOplayerDefaultUi} or {@link UiContainer}, | ||
| * or alternatively any other component that provides a {@link PlayerContext}. | ||
| * | ||
| * @group Hooks | ||
| */ | ||
| export const useError = (): PlayerError | undefined => { | ||
| const [error, setError] = useState<PlayerError | undefined>(undefined); | ||
| const { player } = useContext(PlayerContext); | ||
|
|
||
| useEffect(() => { | ||
| if (!player) return; | ||
| const onEvent = (event: { type: ErrorChangeEventType }) => { | ||
| if (event.type === PlayerEventType.ERROR) { | ||
| setError((event as ErrorEvent).error); | ||
| } else { | ||
| setError(undefined); | ||
| } | ||
| }; | ||
|
|
||
| player.addEventListener(ERROR_CHANGE_EVENTS, onEvent); | ||
| return () => { | ||
| player.removeEventListener(ERROR_CHANGE_EVENTS, onEvent); | ||
| }; | ||
| }, [player]); | ||
|
Comment on lines
+18
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚩 Error state persists across player prop changes in useError hook When the Was this helpful? React with 👍 or 👎 to provide feedback. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a regression — matches the previous |
||
|
|
||
| return error; | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.