11cb0ef41Sopenharmony_ci'use strict'
21cb0ef41Sopenharmony_ciconst stream = require('stream')
31cb0ef41Sopenharmony_ciconst Tracker = require('./tracker.js')
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciclass TrackerStream extends stream.Transform {
61cb0ef41Sopenharmony_ci  constructor (name, size, options) {
71cb0ef41Sopenharmony_ci    super(options)
81cb0ef41Sopenharmony_ci    this.tracker = new Tracker(name, size)
91cb0ef41Sopenharmony_ci    this.name = name
101cb0ef41Sopenharmony_ci    this.id = this.tracker.id
111cb0ef41Sopenharmony_ci    this.tracker.on('change', this.trackerChange.bind(this))
121cb0ef41Sopenharmony_ci  }
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci  trackerChange (name, completion) {
151cb0ef41Sopenharmony_ci    this.emit('change', name, completion, this)
161cb0ef41Sopenharmony_ci  }
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci  _transform (data, encoding, cb) {
191cb0ef41Sopenharmony_ci    this.tracker.completeWork(data.length ? data.length : 1)
201cb0ef41Sopenharmony_ci    this.push(data)
211cb0ef41Sopenharmony_ci    cb()
221cb0ef41Sopenharmony_ci  }
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci  _flush (cb) {
251cb0ef41Sopenharmony_ci    this.tracker.finish()
261cb0ef41Sopenharmony_ci    cb()
271cb0ef41Sopenharmony_ci  }
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci  completed () {
301cb0ef41Sopenharmony_ci    return this.tracker.completed()
311cb0ef41Sopenharmony_ci  }
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  addWork (work) {
341cb0ef41Sopenharmony_ci    return this.tracker.addWork(work)
351cb0ef41Sopenharmony_ci  }
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci  finish () {
381cb0ef41Sopenharmony_ci    return this.tracker.finish()
391cb0ef41Sopenharmony_ci  }
401cb0ef41Sopenharmony_ci}
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_cimodule.exports = TrackerStream
43