Use # for private fields
This commit is contained in:
@@ -4,39 +4,46 @@ const stream = require('stream'),
|
||||
TransformStream = stream.Transform;
|
||||
|
||||
class ProgressStream extends TransformStream {
|
||||
#options;
|
||||
#transferred;
|
||||
#delta;
|
||||
#started;
|
||||
#startTime;
|
||||
#interval;
|
||||
|
||||
constructor(options) {
|
||||
super();
|
||||
this._options = Object.assign({ interval: 10 * 1000 }, options);
|
||||
this._transferred = 0;
|
||||
this._delta = 0;
|
||||
this._started = false;
|
||||
this._startTime = null;
|
||||
this._interval = null;
|
||||
this.#options = Object.assign({ interval: 10 * 1000 }, options);
|
||||
this.#transferred = 0;
|
||||
this.#delta = 0;
|
||||
this.#started = false;
|
||||
this.#startTime = null;
|
||||
this.#interval = null;
|
||||
}
|
||||
|
||||
stats() {
|
||||
const totalSecs = Math.round((Date.now() - this._startTime)/1000);
|
||||
return { startTime: this._startTime, totalSecs, transferred: this._transferred };
|
||||
const totalSecs = Math.round((Date.now() - this.#startTime)/1000);
|
||||
return { startTime: this.#startTime, totalSecs, transferred: this.#transferred };
|
||||
}
|
||||
|
||||
_start() {
|
||||
this._startTime = Date.now();
|
||||
this._started = true;
|
||||
this._interval = setInterval(() => {
|
||||
const speed = this._delta * 1000 / this._options.interval;
|
||||
this._delta = 0;
|
||||
this.emit('progress', { speed, transferred: this._transferred });
|
||||
}, this._options.interval);
|
||||
this.#startTime = Date.now();
|
||||
this.#started = true;
|
||||
this.#interval = setInterval(() => {
|
||||
const speed = this.#delta * 1000 / this.#options.interval;
|
||||
this.#delta = 0;
|
||||
this.emit('progress', { speed, transferred: this.#transferred });
|
||||
}, this.#options.interval);
|
||||
}
|
||||
|
||||
_stop() {
|
||||
clearInterval(this._interval);
|
||||
clearInterval(this.#interval);
|
||||
}
|
||||
|
||||
_transform(chunk, encoding, callback) {
|
||||
if (!this._started) this._start();
|
||||
this._transferred += chunk.length;
|
||||
this._delta += chunk.length;
|
||||
if (!this.#started) this._start();
|
||||
this.#transferred += chunk.length;
|
||||
this.#delta += chunk.length;
|
||||
callback(null, chunk);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user