-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Expand file tree
/
Copy pathtracker-stream.js
More file actions
38 lines (32 loc) · 882 Bytes
/
tracker-stream.js
File metadata and controls
38 lines (32 loc) · 882 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
'use strict'
const stream = require('readable-stream')
const delegate = require('delegates')
const Tracker = require('./tracker.js')
class TrackerStream extends stream.Transform {
constructor (name, size, options) {
super(options)
this.tracker = new Tracker(name, size)
this.name = name
this.id = this.tracker.id
this.tracker.on('change', delegateChange(this))
}
_transform (data, encoding, cb) {
this.tracker.completeWork(data.length ? data.length : 1)
this.push(data)
cb()
}
_flush (cb) {
this.tracker.finish()
cb()
}
}
function delegateChange (trackerStream) {
return function (name, completion, tracker) {
trackerStream.emit('change', name, completion, trackerStream)
}
}
delegate(TrackerStream.prototype, 'tracker')
.method('completed')
.method('addWork')
.method('finish')
module.exports = TrackerStream