Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/webtorrent/webtorrent.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/file.js2
-rw-r--r--lib/peer.js16
2 files changed, 15 insertions, 3 deletions
diff --git a/lib/file.js b/lib/file.js
index caa395d..e39d5d7 100644
--- a/lib/file.js
+++ b/lib/file.js
@@ -36,7 +36,7 @@ class File extends EventEmitter {
}
get downloaded () {
- if (!this._torrent.bitfield) return 0
+ if (this._destroyed || !this._torrent.bitfield) return 0
const { pieces, bitfield, pieceLength, lastPieceLength } = this._torrent
const { _startPiece: start, _endPiece: end } = this
diff --git a/lib/peer.js b/lib/peer.js
index 2f291d8..5471773 100644
--- a/lib/peer.js
+++ b/lib/peer.js
@@ -20,8 +20,20 @@ exports.createWebRTCPeer = (conn, swarm) => {
if (peer.conn.connected) {
peer.onConnect()
} else {
- peer.conn.once('connect', () => { peer.onConnect() })
- peer.conn.once('error', err => { peer.destroy(err) })
+ const cleanup = () => {
+ peer.conn.removeListener('connect', onConnect)
+ peer.conn.removeListener('error', onError)
+ }
+ const onConnect = () => {
+ cleanup()
+ peer.onConnect()
+ }
+ const onError = err => {
+ cleanup()
+ peer.destroy(err)
+ }
+ peer.conn.once('connect', onConnect)
+ peer.conn.once('error', onError)
peer.startConnectTimeout()
}