chore: lint project (#3279)

* chore: update eslint config
* chore: lint lib files
This commit is contained in:
Krzysztof Moch
2023-10-07 12:56:35 +02:00
committed by GitHub
parent e6e8f621fe
commit 067adde124
12 changed files with 521 additions and 354 deletions

View File

@@ -6,17 +6,21 @@ import React, {
forwardRef,
useImperativeHandle,
type ComponentRef,
} from "react";
import {
View,
StyleSheet,
Image,
Platform,
} from "react-native";
import NativeVideoComponent, { RCTVideoConstants } from "./VideoNativeComponent";
import type { NativeVideoResizeMode, OnAudioFocusChangedData, OnAudioTracksData, OnPlaybackStateChangedData, OnTextTracksData, OnTimedMetadataData, OnVideoErrorData, OnVideoTracksData } from './VideoNativeComponent'
} from 'react';
import {View, StyleSheet, Image, Platform} from 'react-native';
import NativeVideoComponent, {RCTVideoConstants} from './VideoNativeComponent';
import type {
NativeVideoResizeMode,
OnAudioFocusChangedData,
OnAudioTracksData,
OnPlaybackStateChangedData,
OnTextTracksData,
OnTimedMetadataData,
OnVideoErrorData,
OnVideoTracksData,
} from './VideoNativeComponent';
import type { StyleProp, ImageStyle, NativeSyntheticEvent } from "react-native";
import type {StyleProp, ImageStyle, NativeSyntheticEvent} from 'react-native';
import {
type VideoComponentType,
type OnLoadData,
@@ -30,9 +34,9 @@ import {
type OnExternalPlaybackChangeData,
type OnReceiveAdEventData,
VideoManager,
} from "./VideoNativeComponent";
import type { ReactVideoProps } from "./types/video";
import { getReactTag, resolveAssetSourceForVideo } from "./utils";
} from './VideoNativeComponent';
import type {ReactVideoProps} from './types/video';
import {getReactTag, resolveAssetSourceForVideo} from './utils';
export interface VideoRef {
seek: (time: number, tolerance?: number) => void;
@@ -40,7 +44,9 @@ export interface VideoRef {
pause: () => void;
presentFullscreenPlayer: () => void;
dismissFullscreenPlayer: () => void;
restoreUserInterfaceForPictureInPictureStopCompleted: (restore: boolean) => void;
restoreUserInterfaceForPictureInPictureStopCompleted: (
restore: boolean,
) => void;
}
const Video = forwardRef<VideoRef, ReactVideoProps>(
@@ -84,7 +90,7 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
onVideoTracks,
...rest
},
ref
ref,
) => {
const nativeRef = useRef<ComponentRef<VideoComponentType>>(null);
const [showPoster, setShowPoster] = useState(!!poster);
@@ -98,25 +104,31 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
() => ({
...StyleSheet.absoluteFillObject,
resizeMode:
posterResizeMode && posterResizeMode !== "none"
posterResizeMode && posterResizeMode !== 'none'
? posterResizeMode
: "contain",
: 'contain',
}),
[posterResizeMode]
[posterResizeMode],
);
const src = useMemo(() => {
if (!source) return undefined;
if (!source) {
return undefined;
}
const resolvedSource = resolveAssetSourceForVideo(source);
let uri = resolvedSource.uri || "";
if (uri && uri.match(/^\//)) uri = `file://${uri}`;
if (!uri) console.warn("Trying to load empty source");
let uri = resolvedSource.uri || '';
if (uri && uri.match(/^\//)) {
uri = `file://${uri}`;
}
if (!uri) {
console.warn('Trying to load empty source');
}
const isNetwork = !!(uri && uri.match(/^https?:/));
const isAsset = !!(
uri &&
uri.match(
/^(assets-library|ipod-library|file|content|ms-appx|ms-appdata):/
/^(assets-library|ipod-library|file|content|ms-appx|ms-appdata):/,
)
);
@@ -125,22 +137,22 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
isNetwork,
isAsset,
shouldCache: resolvedSource.shouldCache || false,
type: resolvedSource.type || "",
type: resolvedSource.type || '',
mainVer: resolvedSource.mainVer || 0,
patchVer: resolvedSource.patchVer || 0,
requestHeaders: resolvedSource?.headers || {},
startTime: resolvedSource.startTime || 0,
endTime: resolvedSource.endTime
endTime: resolvedSource.endTime,
};
}, [source]);
const _resizeMode: NativeVideoResizeMode = useMemo(() => {
switch (resizeMode) {
case "contain":
case 'contain':
return RCTVideoConstants.ScaleAspectFit;
case "cover":
case 'cover':
return RCTVideoConstants.ScaleAspectFill;
case "stretch":
case 'stretch':
return RCTVideoConstants.ScaleToFill;
default:
return RCTVideoConstants.ScaleNone;
@@ -148,7 +160,9 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
}, [resizeMode]);
const _drm = useMemo(() => {
if (!drm) return;
if (!drm) {
return;
}
return {
drmType: drm.type,
licenseServer: drm.licenseServer,
@@ -160,59 +174,64 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
};
}, [drm]);
const _selectedTextTrack = useMemo(() => {
if (!selectedTextTrack) return;
if (typeof selectedTextTrack?.value === 'number') return {
seletedTextType: selectedTextTrack?.type,
index: selectedTextTrack?.value,
if (!selectedTextTrack) {
return;
}
if (typeof selectedTextTrack?.value === 'number') {
return {
selectedTextType: selectedTextTrack?.type,
index: selectedTextTrack?.value,
};
}
return {
selectedTextType: selectedTextTrack?.type,
value: selectedTextTrack?.value,
}
};
}, [selectedTextTrack]);
const _selectedAudioTrack = useMemo(() => {
if (!selectedAudioTrack) return;
if (typeof selectedAudioTrack?.value === 'number') return {
selectedAudioType: selectedAudioTrack?.type,
index: selectedAudioTrack?.value,
if (!selectedAudioTrack) {
return;
}
if (typeof selectedAudioTrack?.value === 'number') {
return {
selectedAudioType: selectedAudioTrack?.type,
index: selectedAudioTrack?.value,
};
}
return {
selectedAudioType: selectedAudioTrack?.type,
value: selectedAudioTrack?.value,
}
};
}, [selectedAudioTrack]);
const seek = useCallback(async (time: number, tolerance?: number) => {
if (isNaN(time)) {
throw new Error('Specified time is not a number');
}
const seek = useCallback(
async (time: number, tolerance?: number) => {
if (isNaN(time)) throw new Error("Specified time is not a number");
if (!nativeRef.current) {
console.warn('Video Component is not mounted');
return;
}
if (!nativeRef.current) {
console.warn("Video Component is not mounted");
return;
}
Platform.select({
ios: () => {
nativeRef.current?.setNativeProps({
seek: {
time,
tolerance,
},
});
},
default: () => {
nativeRef.current?.setNativeProps({
seek: time,
});
},
})();
},
[]
);
Platform.select({
ios: () => {
nativeRef.current?.setNativeProps({
seek: {
time,
tolerance,
},
});
},
default: () => {
nativeRef.current?.setNativeProps({
seek: time,
});
},
})();
}, []);
const presentFullscreenPlayer = useCallback(() => {
setIsFullscreen(true);
@@ -238,79 +257,93 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
(restored: boolean) => {
setRestoreUserInterfaceForPIPStopCompletionHandler(restored);
},
[setRestoreUserInterfaceForPIPStopCompletionHandler]
[setRestoreUserInterfaceForPIPStopCompletionHandler],
);
const onVideoLoadStart = useCallback(
(e: NativeSyntheticEvent<OnLoadStartData>) => {
onLoadStart?.(e.nativeEvent);
},
[onLoadStart]
[onLoadStart],
);
const onVideoLoad = useCallback(
(e: NativeSyntheticEvent<OnLoadData>) => {
if (Platform.OS === "windows") setShowPoster(false);
if (Platform.OS === 'windows') {
setShowPoster(false);
}
onLoad?.(e.nativeEvent);
},
[onLoad, setShowPoster]
[onLoad, setShowPoster],
);
const onVideoError = useCallback(
(e: NativeSyntheticEvent<OnVideoErrorData>) => {
onError?.(e.nativeEvent);
},
[onError]
[onError],
);
const onVideoProgress = useCallback(
(e: NativeSyntheticEvent<OnProgressData>) => {
onProgress?.(e.nativeEvent);
},
[onProgress]
[onProgress],
);
const onVideoSeek = useCallback(
(e: NativeSyntheticEvent<OnSeekData>) => {
onSeek?.(e.nativeEvent);
},
[onSeek]
[onSeek],
);
// android only
const onVideoPlaybackStateChanged = useCallback((e: NativeSyntheticEvent<OnPlaybackStateChangedData>) => {
onPlaybackStateChanged?.(e.nativeEvent);
}, [onPlaybackStateChanged])
const onVideoPlaybackStateChanged = useCallback(
(e: NativeSyntheticEvent<OnPlaybackStateChangedData>) => {
onPlaybackStateChanged?.(e.nativeEvent);
},
[onPlaybackStateChanged],
);
// android only
const onVideoIdle = useCallback(() => {
onIdle?.()
}, [onIdle])
onIdle?.();
}, [onIdle]);
const _onTimedMetadata = useCallback(
(e: NativeSyntheticEvent<OnTimedMetadataData>) => {
onTimedMetadata?.(e.nativeEvent);
},
[onTimedMetadata]
[onTimedMetadata],
);
const _onAudioTracks = useCallback((e: NativeSyntheticEvent<OnAudioTracksData>) => {
onAudioTracks?.(e.nativeEvent)
}, [onAudioTracks])
const _onAudioTracks = useCallback(
(e: NativeSyntheticEvent<OnAudioTracksData>) => {
onAudioTracks?.(e.nativeEvent);
},
[onAudioTracks],
);
const _onTextTracks = useCallback((e: NativeSyntheticEvent<OnTextTracksData>) => {
onTextTracks?.(e.nativeEvent)
}, [onTextTracks])
const _onTextTracks = useCallback(
(e: NativeSyntheticEvent<OnTextTracksData>) => {
onTextTracks?.(e.nativeEvent);
},
[onTextTracks],
);
const _onVideoTracks = useCallback((e: NativeSyntheticEvent<OnVideoTracksData>) => {
onVideoTracks?.(e.nativeEvent)
}, [onVideoTracks])
const _onVideoTracks = useCallback(
(e: NativeSyntheticEvent<OnVideoTracksData>) => {
onVideoTracks?.(e.nativeEvent);
},
[onVideoTracks],
);
const _onPlaybackRateChange = useCallback(
(e: NativeSyntheticEvent<Readonly<{ playbackRate: number }>>) => {
(e: NativeSyntheticEvent<Readonly<{playbackRate: number}>>) => {
onPlaybackRateChange?.(e.nativeEvent);
},
[onPlaybackRateChange]
[onPlaybackRateChange],
);
const _onReadyForDisplay = useCallback(() => {
@@ -322,51 +355,91 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
(e: NativeSyntheticEvent<OnPictureInPictureStatusChangedData>) => {
onPictureInPictureStatusChanged?.(e.nativeEvent);
},
[onPictureInPictureStatusChanged]
[onPictureInPictureStatusChanged],
);
const _onAudioFocusChanged = useCallback((e: NativeSyntheticEvent<OnAudioFocusChangedData>) => {
onAudioFocusChanged?.(e.nativeEvent)
}, [onAudioFocusChanged])
const _onAudioFocusChanged = useCallback(
(e: NativeSyntheticEvent<OnAudioFocusChangedData>) => {
onAudioFocusChanged?.(e.nativeEvent);
},
[onAudioFocusChanged],
);
const onVideoBuffer = useCallback((e: NativeSyntheticEvent<OnBufferData>) => {
onBuffer?.(e.nativeEvent);
}, [onBuffer]);
const onVideoBuffer = useCallback(
(e: NativeSyntheticEvent<OnBufferData>) => {
onBuffer?.(e.nativeEvent);
},
[onBuffer],
);
const onVideoExternalPlaybackChange = useCallback((e: NativeSyntheticEvent<OnExternalPlaybackChangeData>) => {
onExternalPlaybackChange?.(e.nativeEvent);
}, [onExternalPlaybackChange])
const onVideoExternalPlaybackChange = useCallback(
(e: NativeSyntheticEvent<OnExternalPlaybackChangeData>) => {
onExternalPlaybackChange?.(e.nativeEvent);
},
[onExternalPlaybackChange],
);
const _onBandwidthUpdate = useCallback((e: NativeSyntheticEvent<OnBandwidthUpdateData>) => {
onBandwidthUpdate?.(e.nativeEvent);
}, [onBandwidthUpdate]);
const _onBandwidthUpdate = useCallback(
(e: NativeSyntheticEvent<OnBandwidthUpdateData>) => {
onBandwidthUpdate?.(e.nativeEvent);
},
[onBandwidthUpdate],
);
const _onReceiveAdEvent = useCallback((e: NativeSyntheticEvent<OnReceiveAdEventData>) => {
onReceiveAdEvent?.(e.nativeEvent);
}, [onReceiveAdEvent]);
const _onReceiveAdEvent = useCallback(
(e: NativeSyntheticEvent<OnReceiveAdEventData>) => {
onReceiveAdEvent?.(e.nativeEvent);
},
[onReceiveAdEvent],
);
const onGetLicense = useCallback(
(event: NativeSyntheticEvent<OnGetLicenseData>) => {
if (drm && drm.getLicense instanceof Function) {
const data = event.nativeEvent;
if (data && data.spcBase64) {
const getLicenseOverride = drm.getLicense(data.spcBase64, data.contentId, data.licenseUrl);
const getLicenseOverride = drm.getLicense(
data.spcBase64,
data.contentId,
data.licenseUrl,
);
const getLicensePromise = Promise.resolve(getLicenseOverride); // Handles both scenarios, getLicenseOverride being a promise and not.
getLicensePromise.then((result => {
if (result !== undefined) {
nativeRef.current && VideoManager.setLicenseResult(result, data.licenseUrl, getReactTag(nativeRef));
} else {
nativeRef.current && VideoManager.setLicenseResultError('Empty license result', data.licenseUrl, getReactTag(nativeRef));
}
})).catch(() => {
nativeRef.current && VideoManager.setLicenseResultError('fetch error', data.licenseUrl, getReactTag(nativeRef));
});
getLicensePromise
.then((result) => {
if (result !== undefined) {
nativeRef.current &&
VideoManager.setLicenseResult(
result,
data.licenseUrl,
getReactTag(nativeRef),
);
} else {
nativeRef.current &&
VideoManager.setLicenseResultError(
'Empty license result',
data.licenseUrl,
getReactTag(nativeRef),
);
}
})
.catch(() => {
nativeRef.current &&
VideoManager.setLicenseResultError(
'fetch error',
data.licenseUrl,
getReactTag(nativeRef),
);
});
} else {
VideoManager.setLicenseResultError('No spc received', data.licenseUrl, getReactTag(nativeRef));
VideoManager.setLicenseResultError(
'No spc received',
data.licenseUrl,
getReactTag(nativeRef),
);
}
}
},
[drm]
},
[drm],
);
useImperativeHandle(
@@ -388,7 +461,7 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
pause,
resume,
restoreUserInterfaceForPictureInPictureStopCompleted,
]
],
);
return (
@@ -438,12 +511,12 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
onReceiveAdEvent={_onReceiveAdEvent}
/>
{showPoster ? (
<Image style={posterStyle} source={{ uri: poster }} />
<Image style={posterStyle} source={{uri: poster}} />
) : null}
</View>
);
}
},
);
Video.displayName = "Video";
export default Video;
Video.displayName = 'Video';
export default Video;