@rtcio/react
Version:
A wrapper around the @rtcio/core library for React integration
24 lines • 960 B
JavaScript
import { useContext, useEffect, useRef } from "react";
import { p2pContext } from "../Provider";
export function createUseRtcListener() {
return function useRtcListener(event, callback) {
const ctx = useContext(p2pContext);
if (!ctx) {
throw new Error("useRtcListener must be called in a P2PProvider");
}
const { rtc } = ctx;
const callbackRef = useRef(callback);
useEffect(() => {
callbackRef.current = callback;
}, [callback]);
useEffect(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const handler = (...args) => callbackRef.current(...args);
rtc.inspect(({ rtc: manager }) => manager.on(event, handler));
return () => {
rtc.inspect(({ rtc: manager }) => manager.off(event, handler));
};
}, [event, rtc]);
};
}
//# sourceMappingURL=useRtcListener.js.map