hgouveia / node-downloader-helper

A simple http file downloader for node.js
MIT License
253 stars 54 forks source link

Hi, how can I set the progress event fired at 100 ms? #16

Closed hz0324 closed 5 years ago

hz0324 commented 5 years ago

Thank you very much for the package, it helps me a lot. But I have a problem using it.

Because my app is download very small files, usually below 3 MB. If the progress event fired 1 seconds a time, user can only see 2 or 3 numbers before it finished. It is really bad for user experience.

So, can I change the time interval? where to do it? Thanks.

hgouveia commented 5 years ago

Hello @hz0324!

This issue actually was bring up before and was fixed in this pull request https://github.com/hgouveia/node-downloader-helper/pull/9 , but the issue is that is not yet in the current master branch

i wanted to packaged more features into this version so that's why is not yet published,

you could do 2 things, get the npm package from github like this

npm i -S https://github.com/hgouveia/node-downloader-helper#dev

or modifiy the function in the dl instance in your code as a patch, like this

dl.__calculateStats = function (receivedBytes) {
        const currentTime = new Date();
        const elaspsedTime = currentTime - this.__statsEstimate.time;

        if (!receivedBytes) {
            return;
        }

        this.__downloaded += receivedBytes;
        this.__progress = (this.__downloaded / this.__total) * 100;

        // Calculate the speed every second or if finished
        if (this.__downloaded === this.__total || elaspsedTime > 1000) {
            this.__statsEstimate.time = currentTime;
            this.__statsEstimate.bytes = this.__downloaded - this.__statsEstimate.prevBytes;
            this.__statsEstimate.prevBytes = this.__downloaded;
        }

        // emit the progress
        this.emit('progress', {
            total: this.__total,
            downloaded: this.__downloaded,
            progress: this.__progress,
            speed: this.__statsEstimate.bytes
        });
 };
hz0324 commented 5 years ago

@hgouveia Thank you very much for the instant reply. I will try it out.

hz0324 commented 5 years ago

@hgouveia BTW,when will you release the new version? We are not hurry, we can wait for the new code.

hgouveia commented 5 years ago

@hgouveia BTW,when will you release the new version? We are not hurry, we can wait for the new code.

i will try to release the next month if the time is on my side, since i want to add some others fixes/enhancements requested,

i will close this issue and let you know when this happen replying to this 👍