Skip to content

Commit

Permalink
fix download event too many listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
joehand committed Apr 29, 2017
1 parent 4911f28 commit 909ce01
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function runDat (state, bus) {
}

function trackProgress (state, bus) {
bus.on('archive:content', function () {
bus.once('archive:content', function () {
if (state.archive.metadata.writable) trackImport()
else trackDownload()
})
Expand All @@ -73,12 +73,19 @@ function trackProgress (state, bus) {
state.downloading = true
state.modified = false

var feed = state.archive.content
state.downloaded = 0
for (var i = 0; i < feed.length; i++) {
if (feed.has(i)) state.downloaded++
}

state.archive.content.on('clear', function () {
state.modified = true
})

state.archive.content.on('download', function (index, data) {
state.modified = true
state.downloaded += 1
})

state.archive.on('sync', function () {
Expand Down Expand Up @@ -225,17 +232,6 @@ function downloadUI (state) {
if (!state.stats.get().blocksTotal) {
return '' // no metadata yet
}
if (!state.downloaded) {
var feed = state.archive.content
state.downloaded = 0
for (var i = 0; i < feed.length; i++) {
if (feed.has(i)) state.downloaded++
}
state.archive.content.on('download', function () {
state.downloaded += 1
})
}

if (!state.downloadBar) {
makeBar()
state.archive.metadata.update(makeBar)
Expand Down

0 comments on commit 909ce01

Please sign in to comment.