react-native-vimeo-bridge
Version:
🎥 Easy-to-use Vimeo player for React Native with cross-platform support
66 lines (61 loc) • 2.34 kB
JavaScript
;
import { useEffect, useRef, useState } from 'react';
/**
* @param player - The Vimeo player instance.
* @param eventType - The type of event to subscribe to.
* @param callback - The callback to call when the event is triggered.
* @param deps - The dependencies to watch for changes.
* @returns void
*/
/**
* @param player - The Vimeo player instance.
* @param eventType - `timeupdate` event only.
* @param throttleMs - The throttle time in milliseconds (default 250ms).
* @returns The event data.
*/
/**
* @param player - The Vimeo player instance.
* @param eventType - The type of event to subscribe to. `timeupdate` event is not supported.
* @returns The event data.
*/
/**
* @param player - The Vimeo player instance.
* @param eventType - The type of event to subscribe to.
* @param callbackOrThrottle - The callback to call when the event is triggered. If it is a number, it will be used as the throttle time in milliseconds for `timeupdate` event.
* @param deps - The dependencies to watch for changes.
* @returns The event data. If it is a callback, it will return `undefined`.
*/
function useVimeoEvent(player, eventType, callbackOrThrottle, deps) {
const isCallback = typeof callbackOrThrottle === 'function';
const throttleMs = typeof callbackOrThrottle === 'number' ? callbackOrThrottle : undefined;
const callbackRef = useRef(isCallback ? callbackOrThrottle : null);
const [data, setData] = useState(null);
const lastUpdateRef = useRef(0);
useEffect(() => {
if (isCallback) {
callbackRef.current = callbackOrThrottle;
}
}, [callbackOrThrottle, isCallback, ...(deps ?? [])]);
useEffect(() => {
const unsubscribe = player.subscribe(eventType, eventData => {
if (isCallback && callbackRef.current) {
callbackRef.current(eventData);
return;
}
if (!isCallback) {
if (eventType === 'timeupdate' && throttleMs) {
const now = Date.now();
if (now - lastUpdateRef.current < throttleMs) {
return;
}
lastUpdateRef.current = now;
}
setData(eventData);
}
});
return unsubscribe;
}, [player, eventType, isCallback, throttleMs]);
return isCallback ? undefined : data;
}
export default useVimeoEvent;
//# sourceMappingURL=useVimeoEvent.js.map