UNPKG

612 BJavaScriptView Raw
1'use strict';
2const EventEmitter = require('events');
3
4const written = new WeakMap();
5
6class ProgressEmitter extends EventEmitter {
7 constructor(source, destination) {
8 super();
9 this._source = source;
10 this._destination = destination;
11 }
12
13 set written(value) {
14 written.set(this, value);
15 this.emitProgress();
16 }
17
18 get written() {
19 return written.get(this);
20 }
21
22 emitProgress() {
23 const {size, written} = this;
24 this.emit('progress', {
25 src: this._source,
26 dest: this._destination,
27 size,
28 written,
29 percent: written === size ? 1 : written / size
30 });
31 }
32}
33
34module.exports = ProgressEmitter;