all files / node-zoom2/lib/ read-stream.js

85.92% Statements 61/71
76.67% Branches 23/30
100% Functions 10/10
85.92% Lines 61/71
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127                                                                                                                                   
'use strict';
 
var util = require('util');
var Readable = require('stream').Readable;
var Record = require('./record');
 
module.exports = ReadStream;
 
util.inherits(ReadStream, Readable);
 
var stream = ReadStream.prototype;
 
function ReadStream(conn, options) {
  options || (options = {});
  options.objectMode = true;
  Readable.call(this, options);
 
  this._zoomState = {
    conn: conn,
    index: 0,
    start: options.start | 0,
    chunk: (options.chunk || 20) | 0,
    limit: options.limit | 0,
    total: 0,
    resultset: null,
    records: null,
    waiting: false,
    destroyed: false
  };
}
 
stream._read = function () {
  var state = this._zoomState;
 
  Iif (state.destroyed) {
    return;
  }
 
  if (!state.conn._connected) {
    this._connect();
    return state.waiting = true;
  }
 
  if (!state.resultset) {
    this._getResultset();
    return state.waiting = true;
  }
 
  if (!(state.records && state.records.hasNext())) {
    this._moreRecords();
    return state.waiting = true;
  }
 
  var record = state.records.next();
  this.push(record && new Record(record));
 
  state.index += 1;
 
  Eif ((state.start + state.index) >= state.total
    || state.index >= state.limit) {
    this.push(null);
    this.destroy();
  }
};
 
stream.destroy = function () {
  var state = this._zoomState;
  state.destroyed = true;
  delete this._zoomState;
  this.emit('close');
};
 
stream._getResultset = function () {
  var state = this._zoomState;
  var conn = state.conn;
 
  conn._conn.search(state.conn._query, function (err, resultset) {
    Iif (err) {
      this.emit('error', err);
      this.destroy();
      return;
    }
    state.resultset = resultset;
    state.total = resultset.size();
    this._zoomReady();
  }.bind(this));
};
 
stream._moreRecords = function () {
  var state = this._zoomState;
  var resultset = state.resultset;
  var start = state.start + state.index;
  var count = state.chunk;
 
  resultset.getRecords(start, count, function (err, records) {
    Iif (err) {
      this.emit('error', err);
      this.destroy();
      return;
    }
    state.records = records;
    this._zoomReady();
  }.bind(this));
};
 
stream._zoomReady = function () {
  var state = this._zoomState;
 
  Eif (state.waiting && !state.destroyed) {
    state.waiting = false;
    this._read();
  }
};
 
stream._connect = function () {
  var state = this._zoomState;
 
  state.conn.connect(function (err) {
    Iif (err) {
      this.emit('error', err);
      this.destroy();
      return;
    }
    this._zoomReady();
  }.bind(this));
};