import * as React from 'react' import {StyleSheet} from 'react-native' import {GifViewProps} from './GifView.types' export class GifView extends React.PureComponent { private readonly videoPlayerRef: React.RefObject = React.createRef() private isLoaded = false constructor(props: GifViewProps | Readonly) { super(props) } componentDidUpdate(prevProps: Readonly) { if (prevProps.autoplay !== this.props.autoplay) { if (this.props.autoplay) { this.playAsync() } else { this.pauseAsync() } } } static async prefetchAsync(_: string[]): Promise { console.warn('prefetchAsync is not supported on web') } private firePlayerStateChangeEvent = () => { this.props.onPlayerStateChange?.({ nativeEvent: { isPlaying: !this.videoPlayerRef.current?.paused, isLoaded: this.isLoaded, }, }) } private onLoad = () => { // Prevent multiple calls to onLoad because onCanPlay will fire after each loop if (this.isLoaded) { return } this.isLoaded = true this.firePlayerStateChangeEvent() } async playAsync(): Promise { this.videoPlayerRef.current?.play() } async pauseAsync(): Promise { this.videoPlayerRef.current?.pause() } async toggleAsync(): Promise { if (this.videoPlayerRef.current?.paused) { await this.playAsync() } else { await this.pauseAsync() } } render() { return (