All files / src/protocols/abstract realtime.js

100% Statements 36/36
93.33% Branches 14/15
100% Functions 12/12
100% Lines 34/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 90 91 92 93 94 95 96      1x       71x   71x 71x   71x 71x 71x       19x       14x       31x             9x   9x 9x 9x             2x 2x                 10x 10x   10x 10x   10x   10x 7x   7x       2x     1x 1x     2x     5x 4x 4x       3x         6x       1x  
'use strict';
 
const
  KuzzleAbstractProtocol = require('./common');
 
class RTWrapper extends KuzzleAbstractProtocol {
  constructor (host, options = {}) {
    super(host, options);
 
    this._autoReconnect = typeof options.autoReconnect === 'boolean' ? options.autoReconnect : true;
    this._reconnectionDelay = typeof options.reconnectionDelay === 'number' ? options.reconnectionDelay : 1000;
 
    this.wasConnected = false;
    this.stopRetryingToConnect = false;
    this.retrying = false;
  }
 
  get autoReconnect () {
    return this._autoReconnect;
  }
 
  get reconnectionDelay () {
    return this._reconnectionDelay;
  }
 
  connect() {
    this.state = 'connecting';
  }
 
  /**
   * 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.clear();
    this.emit('disconnect');
  }
 
  /**
   * Called when the client's connection is closed with an error state
   *
   * @param {Error} error
   */
  clientNetworkError (error) {
    this.state = 'offline';
    this.clear();
 
    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;
 
      if ( typeof window === 'object'
        && typeof window.navigator === 'object'
        && window.navigator.onLine === false
      ) {
        window.addEventListener(
          'online',
          () => {
            this.retrying = false;
            this.connect(this.host).catch(err => this.clientNetworkError(err));
          },
          { once: true });
        return;
      }
 
      setTimeout(() => {
        this.retrying = false;
        this.connect(this.host).catch(err => this.clientNetworkError(err));
      }, this.reconnectionDelay);
    }
    else {
      this.emit('disconnect');
    }
  }
 
  isReady () {
    return this.state === 'connected';
  }
}
 
module.exports = RTWrapper;