UNPKG

@theoplayer/react-native-analytics-comscore

Version:
42 lines (35 loc) 1.37 kB
import { PlayerEventType, THEOplayer } from 'react-native-theoplayer'; import { RefObject, useEffect, useRef } from 'react'; import { ComscoreConfiguration, ComscoreConnector, ComscoreMetadata } from '@theoplayer/react-native-analytics-comscore'; export function useComscore( metadata: ComscoreMetadata, config: ComscoreConfiguration, ): [RefObject<ComscoreConnector | undefined>, (player: THEOplayer | undefined) => void] { const connector = useRef<ComscoreConnector | undefined>(undefined); const theoPlayer = useRef<THEOplayer | undefined>(undefined); const initialize = (player: THEOplayer | undefined) => { // Optionally destroy existent connector onDestroy(); theoPlayer.current = player; if (player) { connector.current = new ComscoreConnector(player, metadata, config); player.addEventListener(PlayerEventType.DESTROY, onDestroy); } else { throw new Error('Invalid THEOplayer instance'); } }; const onDestroy = () => { if (connector.current) { if (!theoPlayer.current) { throw new Error('Invalid THEOplayer instance'); } theoPlayer.current.removeEventListener(PlayerEventType.DESTROY, onDestroy); connector.current.destroy(); connector.current = undefined; } }; useEffect(() => { return onDestroy; }, []); return [connector, initialize]; }