Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
fix: progress bar flakiness (#1042)
Browse files Browse the repository at this point in the history
* fix: fix progress bar flakiness

* feat: reuse createAddPullStream to avoid code duplication
  • Loading branch information
dryajov authored and daviddias committed Oct 20, 2017
1 parent 7fc80dc commit d7732c3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 18 deletions.
5 changes: 1 addition & 4 deletions src/cli/commands/files/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,6 @@ module.exports = {
const ipfs = argv.ipfs

let list = []
let currentBytes = 0

waterfall([
(next) => glob(path.join(inPath, '/**/*'), next),
(globResult, next) => {
Expand All @@ -216,8 +214,7 @@ module.exports = {
if (argv.progress) {
const bar = createProgressBar(totalBytes)
options.progress = function (byteLength) {
currentBytes += byteLength
bar.tick(byteLength, {progress: byteman(currentBytes, 2, 'MB')})
bar.update(byteLength / totalBytes, {progress: byteman(byteLength, 2, 'MB')})
}
}

Expand Down
21 changes: 10 additions & 11 deletions src/core/components/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ module.exports = function files (self) {
shardSplitThreshold: self._options.EXPERIMENTAL.sharding ? 1000 : Infinity
}, options)

let total = 0
let prog = opts.progress || (() => {})
const progress = (bytes) => {
total += bytes
prog(total)
}

opts.progress = progress
return pull(
pull.map(normalizeContent),
pull.flatten(),
Expand Down Expand Up @@ -65,18 +73,9 @@ module.exports = function files (self) {
return callback(new Error('Invalid arguments, data must be an object, Buffer or readable stream'))
}

let total = 0
let prog = options.progress || (() => {})
const progress = (bytes) => {
total += bytes
prog(total)
}

options.progress = progress
pull(
pull.values(normalizeContent(data)),
importer(self._ipldResolver, options),
pull.asyncMap(prepareFile.bind(null, self, options)),
pull.values([data]),
createAddPullStream(options),
sort((a, b) => {
if (a.path < b.path) return 1
if (a.path > b.path) return -1
Expand Down
4 changes: 1 addition & 3 deletions src/http/api/resources/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,8 @@ exports.add = {
})

const replyStream = pushable()
let total = 0
const progressHandler = (bytes) => {
total += bytes
replyStream.push({ Bytes: total })
replyStream.push({ Bytes: bytes })
}

const options = {
Expand Down

0 comments on commit d7732c3

Please sign in to comment.