var Readable = require('stream').Readable,
  TrieNode = require('./trieNode'),
  util = require('util');

var internals = {};

exports = module.exports = internals.ReadStream = function (trie) {
  this.trie = trie;
  this.next = null;
  Readable.call(this, {
    objectMode: true
  });
};

util.inherits(internals.ReadStream, Readable);

internals.ReadStream.prototype._read = function () {
  var self = this;
  if (this.next) {
    this.next();
  } else if (!this.started) {
    this.started = true;
    this.trie._findAll(this.trie.root, [], function (val, key, onDone) {
      key = TrieNode.nibblesToBuffer(key);
      self.next = onDone;
      if (val) {
        self.push({
          key: key,
          value: val.value
        });
      }
    }, function () {
      self.push(null);
    });
  }
};

@@ -418,19 +418,57 @@
 his.next
-) %7B
+ && !this.started) %7B%0A    //unpause stream
 %0A    thi
@@ -684,26 +684,22 @@
     
-if (val) %7B%0A       
+self.started =
  sel
@@ -715,18 +715,16 @@
         
-  
 key: key
@@ -725,18 +725,16 @@
 y: key,%0A
-  
         
@@ -760,21 +760,45 @@
     
-  
 %7D);%0A
+%0A
       
-%7D
+if(self.started) onDone();
 %0A   

clean up streams