tracker-stream.js 943 B

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