Skip to content

Commit

Permalink
reshape progress events
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadim Demedes committed Jun 1, 2017
1 parent 90ba674 commit a504bc0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 35 deletions.
36 changes: 12 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,8 @@ function requestAsEventEmitter(opts) {

ee.emit('uploadProgress', {
percent: 1,
size: {
transferred: uploaded,
total: uploadBodySize
}
transferred: uploaded,
total: uploadBodySize
});

const statusCode = res.statusCode;
Expand Down Expand Up @@ -170,10 +168,8 @@ function requestAsEventEmitter(opts) {
if (percent < 1) {
ee.emit('downloadProgress', {
percent,
size: {
transferred: downloaded,
total: downloadBodySize
}
transferred: downloaded,
total: downloadBodySize
});
}

Expand All @@ -183,10 +179,8 @@ function requestAsEventEmitter(opts) {
flush(callback) {
ee.emit('downloadProgress', {
percent: 1,
size: {
transferred: downloaded,
total: downloadBodySize
}
transferred: downloaded,
total: downloadBodySize
});

callback();
Expand All @@ -213,10 +207,8 @@ function requestAsEventEmitter(opts) {

ee.emit('downloadProgress', {
percent: 0,
size: {
transferred: 0,
total: downloadBodySize
}
transferred: 0,
total: downloadBodySize
});

res.pipe(progressStream);
Expand All @@ -239,10 +231,8 @@ function requestAsEventEmitter(opts) {
ee.on('request', req => {
ee.emit('uploadProgress', {
percent: 0,
size: {
transferred: 0,
total: uploadBodySize
}
transferred: 0,
total: uploadBodySize
});

req.connection.on('connect', () => {
Expand All @@ -261,10 +251,8 @@ function requestAsEventEmitter(opts) {

ee.emit('uploadProgress', {
percent: uploadBodySize ? uploaded / uploadBodySize : 0,
size: {
transferred: uploaded,
total: uploadBodySize
}
transferred: uploaded,
total: uploadBodySize
});
}, 150);
});
Expand Down
18 changes: 7 additions & 11 deletions test/progress.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ const checkEvents = (t, events, bodySize = null) => {

events.forEach((event, index) => {
if (hasBodySize) {
t.is(event.percent, event.size.transferred / bodySize);
t.is(event.percent, event.transferred / bodySize);
t.true(event.percent > lastEvent.percent);
} else {
const isLastEvent = index === events.length - 1;
t.is(event.percent, isLastEvent ? 1 : 0);
}

t.true(event.size.transferred >= lastEvent.size.transferred);
t.is(event.size.total, bodySize);
t.true(event.transferred >= lastEvent.transferred);
t.is(event.total, bodySize);

lastEvent = event;
});
Expand Down Expand Up @@ -181,17 +181,13 @@ test('upload progress - no body', async t => {
t.deepEqual(events, [
{
percent: 0,
size: {
transferred: 0,
total: 0
}
transferred: 0,
total: 0
},
{
percent: 1,
size: {
transferred: 0,
total: 0
}
transferred: 0,
total: 0
}
]);
});
Expand Down

0 comments on commit a504bc0

Please sign in to comment.