diff --git a/src/App.js b/src/App.js index 25df477..a24a0be 100644 --- a/src/App.js +++ b/src/App.js @@ -1,4 +1,4 @@ -import React, { useEffect, useState, useRef } from "react"; +import React, { useState, useRef } from "react"; import { ThemeProvider, diff --git a/src/useSession.js b/src/useSession.js index 6a845aa..60a488c 100644 --- a/src/useSession.js +++ b/src/useSession.js @@ -20,11 +20,22 @@ function useSession(imgRef) { ...prevConnnections, [connection.peer]: connection })); + connection.on("open", () => { if (imgRef.current) { connection.send(imgRef.current); } }); + + function removeConnection() { + setConnections(prevConnections => { + const { [connection.peer]: old, ...rest } = prevConnections; + return rest; + }); + } + + connection.on("close", removeConnection); + connection.on("error", removeConnection); } if (!peer) { @@ -37,7 +48,7 @@ function useSession(imgRef) { peer.removeListener("open", handleOpen); peer.removeListener("connection", handleConnection); }; - }, [peer, peerId, connections]); + }, [peer, peerId, connections, imgRef]); return [peer, peerId, connections]; }