UNPKG

@theoplayer/react-native-ui

Version:

A React Native UI for @theoplayer/react-native

24 lines (23 loc) 1.06 kB
import { useCallback, useContext, useSyncExternalStore } from 'react'; import { PlayerContext } from '@theoplayer/react-native-ui'; import { PlayerEventType } from 'react-native-theoplayer'; const PAUSED_CHANGE_EVENTS = [PlayerEventType.PLAY, PlayerEventType.PLAYING, PlayerEventType.PAUSE]; /** * Returns {@link react-native-theoplayer!THEOplayer.pause | the player's paused state}, automatically updating whenever it changes. * * 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 usePaused = () => { const { player } = useContext(PlayerContext); const subscribe = useCallback(callback => { player?.addEventListener(PAUSED_CHANGE_EVENTS, callback); return () => player?.removeEventListener(PAUSED_CHANGE_EVENTS, callback); }, [player]); return useSyncExternalStore(subscribe, () => player ? player.paused : true); }; //# sourceMappingURL=usePaused.js.map