all files / dist/ tree-stream.js

31.82% Statements 7/22
0% Branches 0/10
0% Functions 0/3
31.82% Lines 7/22
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 39 40 41 42                                                                     
'use strict';
 
var Readable = require('readable-stream').Readable;
var isNull = require('lodash.isnull');
var inherits = require('inherits');
 
var TreeStream = module.exports = function () {
  if (!(this instanceof TreeStream)) {
    return new TreeStream();
  }
 
  Readable.call(this, {
    objectMode: true
  });
 
  this._holding = [];
  this._waiting = false;
};
 
inherits(TreeStream, Readable);
 
TreeStream.prototype.writeTree = function (tree) {
  this._holding.push(tree);
  this._read(this._readableState.highWaterMark, true);
};
 
TreeStream.prototype._read = function (size, tryit) {
  if (!this._holding.length) {
    this._waiting = true;
    return;
  }
 
  if (this._holding.length > this._readableState.highWaterMark && tryit) {
    return;
  }
 
  this._waiting = false;
 
  if (!this.push(this._holding.shift())) {
    this._read();
  }
};