Skip to content

Commit 3b1d5a7

Browse files
committed
update peers immediately and also make speed per file not global
1 parent a3e0afc commit 3b1d5a7

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

lib/monitor.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ module.exports = class Monitor extends ReadyResource {
4343
if (!this.entry && this.name) this.entry = await this.drive.entry(this.name)
4444
if (this.entry) this._setEntryInfo()
4545

46+
this.uploadSpeedometer = speedometer()
47+
this.downloadSpeedometer = speedometer()
48+
49+
this._updatePeers()
50+
4651
// Handlers
4752
this.blobs.core.on('peer-add', this._boundPeerUpdate)
4853
this.blobs.core.on('peer-remove', this._boundPeerUpdate)
@@ -71,26 +76,24 @@ module.exports = class Monitor extends ReadyResource {
7176
}
7277

7378
_onUpload (index, bytes, from) {
74-
if (!this.uploadSpeedometer) this.uploadSpeedometer = speedometer()
75-
this.uploadStats.speed = this.uploadSpeedometer(bytes)
76-
this._updateStats(this.uploadStats, index, bytes, from)
79+
this._updateStats(this.uploadSpeedometer, this.uploadStats, index, bytes, from)
7780
}
7881

7982
_onDownload (index, bytes, from) {
80-
if (!this.downloadSpeedometer) this.downloadSpeedometer = speedometer()
81-
this.downloadStats.speed = this.downloadSpeedometer(bytes)
82-
this._updateStats(this.downloadStats, index, bytes, from)
83+
this._updateStats(this.downloadSpeedometer, this.downloadStats, index, bytes, from)
8384
}
8485

8586
_updatePeers () {
8687
this.uploadStats.peers = this.downloadStats.peers = this.peers = this.blobs.core.peers.length
8788
}
8889

89-
_updateStats (stats, index, bytes, from) {
90+
_updateStats (speed, stats, index, bytes, from) {
9091
if (!this.entry || this.closing) return
9192
if (!isWithinRange(index, this.entry)) return
9293

9394
if (!stats.startTime) stats.startTime = Date.now()
95+
96+
stats.speed = speed(bytes)
9497
stats.blocks++
9598
stats.monitoringBytes += bytes
9699
stats.totalBytes += bytes

0 commit comments

Comments
 (0)