Skip to content

Commit

Permalink
Merge pull request #4 from louwers/main
Browse files Browse the repository at this point in the history
Make sure `connectPeer` rejects when peer is not available
  • Loading branch information
chidokun authored Jun 6, 2024
2 parents d93b5eb + ada1a23 commit b8c2178
Show file tree
Hide file tree
Showing 3 changed files with 1,344 additions and 1,340 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@types/react-dom": "^18.0.0",
"antd": "^5.4.2",
"js-file-download": "^0.4.12",
"peerjs": "^1.4.7",
"peerjs": "^1.5.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-redux": "^8.0.5",
Expand Down
16 changes: 15 additions & 1 deletion src/helpers/peer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Peer, {DataConnection} from "peerjs";
import Peer, {DataConnection, PeerErrorType, PeerError} from "peerjs";
import {message} from "antd";

export enum DataType {
Expand Down Expand Up @@ -63,11 +63,25 @@ export const PeerConnection = {
conn.on('open', function() {
console.log("Connect to: " + id)
connectionMap.set(id, conn)
peer?.removeListener('error', handlePeerError)
resolve()
}).on('error', function(err) {
console.log(err)
peer?.removeListener('error', handlePeerError)
reject(err)
})

// When the connection fails due to expiry, the error gets emmitted
// to the peer instead of to the connection.
// We need to handle this here to be able to fulfill the Promise.
const handlePeerError = (err: PeerError<`${PeerErrorType}`>) => {
if (err.type === 'peer-unavailable') {
const messageSplit = err.message.split(' ')
const peerId = messageSplit[messageSplit.length - 1]
if (id === peerId) reject(err)
}
}
peer.on('error', handlePeerError);
}
} catch (err) {
reject(err)
Expand Down
Loading

0 comments on commit b8c2178

Please sign in to comment.