Skip to content
This repository was archived by the owner on Jan 7, 2022. It is now read-only.

Commit c7e94a9

Browse files
authored
fix progress bar being wrong when deletes / overwrites happen (#176)
1 parent f3a484d commit c7e94a9

2 files changed

Lines changed: 15 additions & 6 deletions

File tree

lib/stats.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ var assert = require('assert')
22
var EventEmitter = require('events').EventEmitter
33
var each = require('stream-each')
44
var networkSpeed = require('hyperdrive-network-speed')
5+
var bitfield = require('sparse-bitfield')
56

67
module.exports = function (archive) {
78
assert.ok(archive, 'lib/stats archive required')
89
var stats = new EventEmitter()
10+
var counted = bitfield()
11+
912
var count = {
1013
files: 0,
1114
byteLength: 0,
@@ -52,17 +55,22 @@ module.exports = function (archive) {
5255
var feed = archive.content
5356
count.downloaded = 0
5457
for (var i = 0; i < feed.length; i++) {
55-
if (feed.has(i)) count.downloaded++
58+
if (feed.has(i) && counted.set(i, true)) count.downloaded++
5659
}
5760
stats.emit('update')
5861

5962
archive.content.on('download', countDown)
60-
archive.once('syncing', function () {
61-
archive.content.removeListener('download', countDown)
62-
downloadStats() // recount after update
63-
})
63+
archive.content.on('clear', checkDownloaded)
64+
65+
function checkDownloaded (start, end) {
66+
for (; start < end; start++) {
67+
if (counted.set(start, false)) count.downloaded--
68+
}
69+
stats.emit('update')
70+
}
71+
6472
function countDown (index, data) {
65-
count.downloaded++
73+
if (counted.set(index, true)) count.downloaded++
6674
stats.emit('update')
6775
}
6876
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"multicb": "^1.2.1",
3333
"random-access-file": "^1.7.2",
3434
"random-access-memory": "^2.3.0",
35+
"sparse-bitfield": "^3.0.3",
3536
"speedometer": "^1.0.0",
3637
"stream-each": "^1.2.0",
3738
"untildify": "^3.0.2",

0 commit comments

Comments
 (0)