zippycli
Version:
An unofficial Zippyshare CLI
150 lines (120 loc) • 2.52 kB
JavaScript
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
/**
* Progress wrapper.
*/
export class Progress extends Object {
/**
* Total amount.
*/
/**
* Current amount.
*/
/**
* Update interval.
*/
/**
* Update callback.
*/
/**
* Start time.
*/
/**
* Previous update time.
*/
/**
* Previous update total.
*/
/**
* Create Progress.
*
* @param total Total progress.
* @param current Starting progress.
*/
constructor(total, current = 0) {
super();
_defineProperty(this, "_interval", null);
_defineProperty(this, "_callback", null);
_defineProperty(this, "_start", 0);
_defineProperty(this, "_prevTime", 0);
_defineProperty(this, "_prevCurrent", 0);
this._total = total;
this._current = current;
this._prevCurrent = current;
}
/**
* Get the current time.
*
* @returns Current time.
*/
now() {
return Date.now();
}
/**
* Add to current amount.
*
* @param amount Amount to be added.
*/
add(amount) {
this._current += amount;
}
/**
* Start updater.
*
* @param interval Update interval in milliseconds.
* @param cb Progress callback.
*/
start(interval, cb) {
if (this._interval) {
throw new Error('Already started');
}
const now = this.now();
this._start = now;
this._prevTime = now;
this._callback = cb;
this._interval = setInterval(() => {
this.update();
}, interval);
this.update();
}
/**
* End updater.
*/
end() {
if (this._interval) {
clearInterval(this._interval);
this._interval = null;
}
this.update();
}
/**
* Update callback.
*/
update() {
const cb = this._callback;
if (!cb) {
return;
}
const start = this._start;
const now = this.now();
const duration = now - start;
const timeDelta = now - this._prevTime;
this._prevTime = now;
const total = this._total;
const current = this._current;
const remaining = total - current;
const totalDelta = current - this._prevCurrent;
this._prevCurrent = current;
cb({
start,
now,
duration,
delta: timeDelta
}, {
total,
current,
remaining,
delta: totalDelta
});
}
}
//# sourceMappingURL=progress.mjs.map