1'use strict'
2const stream = require('stream')
3const Tracker = require('./tracker.js')
4
5class TrackerStream extends stream.Transform {
6  constructor (name, size, options) {
7    super(options)
8    this.tracker = new Tracker(name, size)
9    this.name = name
10    this.id = this.tracker.id
11    this.tracker.on('change', this.trackerChange.bind(this))
12  }
13
14  trackerChange (name, completion) {
15    this.emit('change', name, completion, this)
16  }
17
18  _transform (data, encoding, cb) {
19    this.tracker.completeWork(data.length ? data.length : 1)
20    this.push(data)
21    cb()
22  }
23
24  _flush (cb) {
25    this.tracker.finish()
26    cb()
27  }
28
29  completed () {
30    return this.tracker.completed()
31  }
32
33  addWork (work) {
34    return this.tracker.addWork(work)
35  }
36
37  finish () {
38    return this.tracker.finish()
39  }
40}
41
42module.exports = TrackerStream
43