File tree Expand file tree Collapse file tree 5 files changed +20
-14
lines changed
Expand file tree Collapse file tree 5 files changed +20
-14
lines changed Original file line number Diff line number Diff line change @@ -16,27 +16,29 @@ interface Props {
1616 id : string ;
1717 className ?: string ;
1818 muted : boolean ;
19- stream : MediaStream ;
19+ stream : MediaStream | null ;
2020}
2121
2222export default function CamVideo ( props : Props ) {
2323 const { className = '' , id, muted, stream } = props ;
24- const videoRef = useRef < HTMLVideoElement > ( null ) ;
24+ const videoRef = useRef < HTMLVideoElement > ( ) ;
2525
2626 useEffect ( ( ) => {
2727 const wrapper = document . getElementById ( id ) ;
2828 const videoElement = document . createElement ( 'video' ) ;
2929 videoElement . setAttribute ( 'autoplay' , '' ) ;
30- wrapper . appendChild ( videoElement ) ;
30+ wrapper ? .appendChild ( videoElement ) ;
3131 videoRef . current = videoElement ;
3232 } , [ ] ) ;
3333
3434 useEffect ( ( ) => {
35- videoRef . current . muted = muted ;
35+ if ( videoRef . current ) {
36+ videoRef . current . muted = muted ;
37+ }
3638 } , [ muted ] ) ;
3739
3840 useEffect ( ( ) => {
39- const videoEl : HTMLVideoElement = videoRef . current ;
41+ const videoEl = videoRef . current ;
4042 if ( videoEl ) {
4143 // set video stream
4244 videoEl . srcObject = stream ;
Original file line number Diff line number Diff line change @@ -46,7 +46,7 @@ interface Props {
4646
4747export default function ChatMessages ( props : Props ) {
4848 const { className, peer, socket } = props ;
49- const myRef = useRef < HTMLDivElement > ( ) ;
49+ const myRef = useRef < HTMLDivElement > ( null ) ;
5050 const [ messages , dispatchMessage ] = useReducer (
5151 ( state : Message [ ] , action : { message : Message ; type : string } ) => {
5252 switch ( action . type ) {
@@ -63,7 +63,9 @@ export default function ChatMessages(props: Props) {
6363
6464 useEffect ( ( ) => {
6565 const elem = myRef . current ;
66- elem . scrollTop = elem . scrollHeight ;
66+ if ( elem ) {
67+ elem . scrollTop = elem . scrollHeight ;
68+ }
6769 } , [ myRef , messages . length ] ) ;
6870
6971 function addMessage ( message : string , from : string ) {
@@ -105,7 +107,7 @@ export default function ChatMessages(props: Props) {
105107 } ) ;
106108
107109 usePeer ( peer , 'error' , ( { name, error } ) => {
108- addMessage ( `${ name } - ${ error . message } ` , 'error' ) ;
110+ addMessage ( `${ name } - ${ error ! . message } ` , 'error' ) ;
109111 } ) ;
110112
111113 return (
Original file line number Diff line number Diff line change @@ -23,8 +23,8 @@ const BasicStyled = styled.div`
2323` ;
2424
2525export default function Basic ( ) {
26- const [ streamLocal , setStreamLocal ] = useState < MediaStream > ( ) ;
27- const [ streamRemote , setStreamRemote ] = useState < MediaStream > ( ) ;
26+ const [ streamLocal , setStreamLocal ] = useState < MediaStream | null > ( null ) ;
27+ const [ streamRemote , setStreamRemote ] = useState < MediaStream | null > ( null ) ;
2828 const peer1 = useCreatePeer ( ) ;
2929 const peer2 = useCreatePeer ( ) ;
3030
Original file line number Diff line number Diff line change @@ -36,18 +36,20 @@ const SignalStyled = styled.div`
3636` ;
3737
3838export default function Signal ( ) {
39- const [ streamLocal , setStreamLocal ] = useState < MediaStream > ( ) ;
40- const [ streamRemote , setStreamRemote ] = useState < MediaStream > ( ) ;
39+ const [ streamLocal , setStreamLocal ] = useState < MediaStream | null > ( null ) ;
40+ const [ streamRemote , setStreamRemote ] = useState < MediaStream | null > ( null ) ;
4141 const peer = useCreatePeer ( { enableDataChannels : true , channelName : 'messages' } ) ;
4242 const socket = useCreateSocket ( ) ;
4343
4444 // socket handlers
4545 useSocket ( socket , 'onicecandidates' , async ( { candidates } ) => {
46+ // eslint-disable-next-line
4647 const promises = candidates . map ( async ( candidate ) => peer . addIceCandidate ( candidate ) ) ;
4748 await Promise . all ( promises ) ;
4849 } ) ;
4950
5051 useSocket ( socket , 'signal' , async ( { description } ) => {
52+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
5153 if ( description . type === 'offer' ) {
5254 peer . destroy ( ) ;
5355 }
Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ export function useCreatePeer(options: PeerOptions = {}): Peer {
1212
1313 useEffect (
1414 ( ) => ( ) => {
15- peerRef . current . destroy ( ) ;
15+ peerRef . current ? .destroy ( ) ;
1616 } ,
1717 [ ]
1818 ) ;
@@ -38,7 +38,7 @@ export function useCreateSocket(): Socket {
3838
3939 useEffect (
4040 ( ) => ( ) => {
41- socketRef . current . disconnect ( ) ;
41+ socketRef . current ? .disconnect ( ) ;
4242 } ,
4343 [ ]
4444 ) ;
You can’t perform that action at this time.
0 commit comments