react-native-vimeo-bridge
Version:
🎥 Easy-to-use Vimeo player for React Native with cross-platform support
45 lines (42 loc) • 1.47 kB
JavaScript
import { useEffect, useMemo, useRef } from 'react';
import VimeoPlayer from "../module/VimeoPlayer.js";
import { parseVimeoSource } from "../utils/index.js";
/**
* @param source - The source of the Vimeo video. It can be a string or an object with a `url` property.
* @param options - The options for the Vimeo player.
* @returns The Vimeo player instance.
*/
const useVimeoPlayer = (source, options) => {
const playerRef = useRef(null);
const previousSource = useRef(undefined);
const isFastRefresh = useRef(false);
const parsedSource = parseVimeoSource(source);
if (playerRef.current == null) {
playerRef.current = new VimeoPlayer(parsedSource, options);
}
// biome-ignore lint/correctness/useExhaustiveDependencies: only once
const player = useMemo(() => {
let newPlayer = playerRef.current;
if (!newPlayer || previousSource.current !== parsedSource) {
playerRef.current?.dispose();
newPlayer = new VimeoPlayer(parsedSource, options);
playerRef.current = newPlayer;
previousSource.current = parsedSource;
} else {
isFastRefresh.current = true;
}
return newPlayer;
}, [parsedSource]);
useEffect(() => {
isFastRefresh.current = false;
return () => {
if (playerRef.current && !isFastRefresh.current) {
playerRef.current?.dispose();
}
};
}, []);
return player;
};
export default useVimeoPlayer;
//# sourceMappingURL=useVimeoPlayer.js.map
;