index.js
function initZooKeeper(rorschach, connectionString, options) {
  var error;
  var zk = zookeeper.createClient(connectionString, options);

  rorschach.zk = zk;

  zk.connect();
  zk.on('connected', onconnected);
  zk.on('expired', setError);
  zk.on('authenticationFailed', setError);
  zk.on('disconnected', ondisconnected);
  zk.on('state', handleStateChange);

  function onconnected() {
    error = false;
  }

  function setError() {
    error = true;
  }

  function ondisconnected() {
    if (rorschach.closed) {
      return;
    }

    if (error) {
      zk.removeListener('connected', onconnected);
      zk.removeListener('disconnected', ondisconnected);
      zk.removeListener('expired', setError);
      zk.removeListener('authenticationFailed', setError);
      zk.removeListener('state', handleStateChange);
      zk.close();

      initZooKeeper(rorschach, connectionString, options);
    }
  }

  function handleStateChange(state) {
    var newState;

    if (state === State.SYNC_CONNECTED) {
      if (!rorschach.ready) {
        rorschach.ready = true;
        rorschach.emit('connected');
        newState = ConnectionState.CONNECTED;
      }
      else {
        newState = ConnectionState.RECONNECTED;
      }
    }
    else if (state === State.CONNECTED_READ_ONLY) {
      newState = ConnectionState.READ_ONLY;
    }
    else if (state === State.EXPIRED) {
      newState = ConnectionState.LOST;
    }
    else if (state === State.DISCONNECTED) {
      newState = ConnectionState.SUSPENDED;
    }
    else {
      // AUTH_FAILED and SASL_AUTHENTICATED are not handled, yep.
      return;
    }

    rorschach.state = newState;
    rorschach.emit('connectionStateChanged', newState);
  }
}
function initZooKeeper(rorschach, connectionString, options) {
  var error;
  var zk = zookeeper.createClient(connectionString, options);

  rorschach.zk = zk;

  zk.connect();
  zk.on('connected', onconnected);
  zk.on('expired', setError);
  zk.on('authenticationFailed', setError);
  zk.on('disconnected', ondisconnected);
  zk.on('state', handleStateChange);

  function onconnected() {
    error = false;
  }

  function setError() {
    error = true;
  }

  function ondisconnected() {
    if (rorschach.closed) {
      return;
    }

    if (error) {
      zk.removeListener('connected', onconnected);
      zk.removeListener('disconnected', ondisconnected);
      zk.removeListener('expired', setError);
      zk.removeListener('authenticationFailed', setError);
      zk.removeListener('state', handleStateChange);
      zk.close();

      initZooKeeper(rorschach, connectionString, options);
    }
  }

  function handleStateChange(state) {
    var newState;

    if (state === State.SYNC_CONNECTED) {
      if (!rorschach.ready) {
        rorschach.ready = true;
        rorschach.emit('connected');
        newState = ConnectionState.CONNECTED;
      }
      else {
        newState = ConnectionState.RECONNECTED;
      }
    }
    else if (state === State.CONNECTED_READ_ONLY) {
      newState = ConnectionState.READ_ONLY;
    }
    else if (state === State.EXPIRED) {
      newState = ConnectionState.LOST;
    }
    else if (state === State.DISCONNECTED) {
      newState = ConnectionState.SUSPENDED;
    }
    else {
      // AUTH_FAILED and SASL_AUTHENTICATED are not handled, yep.
      return;
    }

    rorschach.state = newState;
    rorschach.emit('connectionStateChanged', newState);
  }
}
function initZooKeeper ( rorschach , connectionString , options ) { var error ; var zk = zookeeper . createClient ( connectionString , options ) ; rorschach . zk = zk ; zk . connect ( ) ; zk . on ( 'connected' , onconnected ) ; zk . on ( 'expired' , setError ) ; zk . on ( 'authenticationFailed' , setError ) ; zk . on ( 'disconnected' , ondisconnected ) ; zk . on ( 'state' , handleStateChange ) ; function onconnected ( ) { error = false ; } function setError ( ) { error = true ; } function ondisconnected ( ) { if ( rorschach . closed ) { return ; } if ( error ) { zk . removeListener ( 'connected' , onconnected ) ; zk . removeListener ( 'disconnected' , ondisconnected ) ; zk . removeListener ( 'expired' , setError ) ; zk . removeListener ( 'authenticationFailed' , setError ) ; zk . removeListener ( 'state' , handleStateChange ) ; zk . close ( ) ; initZooKeeper ( rorschach , connectionString , options ) ; } } function handleStateChange ( state ) { var newState ; if ( state === State . SYNC_CONNECTED ) { if ( ! rorschach . ready ) { rorschach . ready = true ; rorschach . emit ( 'connected' ) ; newState = ConnectionState . CONNECTED ; } else { newState = ConnectionState . RECONNECTED ; } } else if ( state === State . CONNECTED_READ_ONLY ) { newState = ConnectionState . READ_ONLY ; } else if ( state === State . EXPIRED ) { newState = ConnectionState . LOST ; } else if ( state === State . DISCONNECTED ) { newState = ConnectionState . SUSPENDED ; } else { // AUTH_FAILED and SASL_AUTHENTICATED are not handled, yep. return ; } rorschach . state = newState ; rorschach . emit ( 'connectionStateChanged' , newState ) ; } }
Initialize connection with ZooKeeper.

@param {Rorschach} rorschach Rorschach instance
@param {String} connectionString Connection string
@param {Object} options ZooKeeper options.
Initialize connection with ZooKeeper .