• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2var util = require('util')
3var stream = require('readable-stream')
4var delegate = require('delegates')
5var Tracker = require('./tracker.js')
6
7var TrackerStream = module.exports = function (name, size, options) {
8  stream.Transform.call(this, options)
9  this.tracker = new Tracker(name, size)
10  this.name = name
11  this.id = this.tracker.id
12  this.tracker.on('change', delegateChange(this))
13}
14util.inherits(TrackerStream, stream.Transform)
15
16function delegateChange (trackerStream) {
17  return function (name, completion, tracker) {
18    trackerStream.emit('change', name, completion, trackerStream)
19  }
20}
21
22TrackerStream.prototype._transform = function (data, encoding, cb) {
23  this.tracker.completeWork(data.length ? data.length : 1)
24  this.push(data)
25  cb()
26}
27
28TrackerStream.prototype._flush = function (cb) {
29  this.tracker.finish()
30  cb()
31}
32
33delegate(TrackerStream.prototype, 'tracker')
34  .method('completed')
35  .method('addWork')
36