All files / src/networkWrapper/protocols/abstract realtime.js

94.12% Statements 32/34
83.33% Branches 15/18
100% Functions 9/9
94.12% Lines 32/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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      1x         64x   64x 64x   64x         64x 64x 64x       28x       21x       48x 48x 8x               18x   18x 18x 18x             4x                 10x 10x 2x     10x 10x   10x 10x 3x 3x 3x 3x     7x         6x       1x  
'use strict';
 
const
  AbstractWrapper = require('./common');
 
class RTWrapper extends AbstractWrapper {
 
  constructor (options = {}) {
    super(options);
 
    this._autoReconnect = typeof options.autoReconnect === 'boolean' ? options.autoReconnect : true;
    this._reconnectionDelay = typeof options.reconnectionDelay === 'number' ? options.reconnectionDelay : 1000;
 
    Iif (options.offlineMode === 'auto' && this.autoReconnect) {
      this.autoQueue = true;
      this.autoReplay = true;
    }
 
    this.wasConnected = false;
    this.stopRetryingToConnect = false;
    this.retrying = false;
  }
 
  get autoReconnect () {
    return this._autoReconnect;
  }
 
  get reconnectionDelay () {
    return this._reconnectionDelay;
  }
 
  connect() {
    this.state = 'connecting';
    if (this.autoQueue) {
      this.startQueuing();
    }
  }
 
  /**
   * Called when the client's connection is established
   */
  clientConnected() {
    super.clientConnected('connected', this.wasConnected);
 
    this.state = 'connected';
    this.wasConnected = true;
    this.stopRetryingToConnect = false;
  }
 
  /**
   * Called when the client's connection is closed
   */
  clientDisconnected() {
    this.emit('disconnect');
  }
 
  /**
   * Called when the client's connection is closed with an error state
   *
   * @param {Error} error
   */
  clientNetworkError(error) {
    this.state = 'offline';
    if (this.autoQueue) {
      this.startQueuing();
    }
 
    const connectionError = new Error(`Unable to connect to kuzzle server at ${this.host}:${this.port}`);
    connectionError.internal = error;
 
    this.emit('networkError', connectionError);
    if (this.autoReconnect && !this.retrying && !this.stopRetryingToConnect) {
      this.retrying = true;
      setTimeout(() => {
        this.retrying = false;
        this.connect(this.host);
      }, this.reconnectionDelay);
    } else {
      this.emit('disconnect');
    }
  }
 
  isReady() {
    return this.state === 'connected';
  }
}
 
module.exports = RTWrapper;