mirror of
https://github.com/arthur-pbty/portfolio2023.git
synced 2026-06-05 13:52:25 +02:00
36 lines
708 B
JavaScript
36 lines
708 B
JavaScript
'use strict';
|
|
const EventEmitter = require('events');
|
|
|
|
const writtenBytes = new WeakMap();
|
|
|
|
class ProgressEmitter extends EventEmitter {
|
|
constructor(sourcePath, destinationPath) {
|
|
super();
|
|
this._sourcePath = sourcePath;
|
|
this._destinationPath = destinationPath;
|
|
}
|
|
|
|
get writtenBytes() {
|
|
return writtenBytes.get(this);
|
|
}
|
|
|
|
set writtenBytes(value) {
|
|
writtenBytes.set(this, value);
|
|
this.emitProgress();
|
|
}
|
|
|
|
emitProgress() {
|
|
const {size, writtenBytes} = this;
|
|
|
|
this.emit('progress', {
|
|
sourcePath: this._sourcePath,
|
|
destinationPath: this._destinationPath,
|
|
size,
|
|
writtenBytes,
|
|
percent: writtenBytes === size ? 1 : writtenBytes / size
|
|
});
|
|
}
|
|
}
|
|
|
|
module.exports = ProgressEmitter;
|