All files / src/controllers/realtime index.js

97.83% Statements 45/46
71.43% Branches 10/14
92.86% Functions 13/14
97.78% Lines 44/45

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 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  3x 3x               117x   117x 117x       1x       1x       1x             1x 1x       3x   3x   3x 2x   3x 3x         4x   4x 1x     3x 6x   3x   3x         3x           7x 2x 3x   3x 1x 1x   1x       2x                 22x 2x 3x 2x   3x   3x       2x               3x 10x     3x 3x         3x  
const
  BaseController = require('../base'),
  Room = require('./room');
 
 
class RealTimeController extends BaseController {
  /**
   * @param {Kuzzle} kuzzle
   */
  constructor (kuzzle) {
    super(kuzzle, 'realtime');
 
    this.subscriptions = {};
    this.subscriptionsOff = {};
  }
 
  count (roomId, options = {}) {
    return this.query({
      action: 'count',
      body: {roomId}
    }, options)
      .then(response => response.result.count);
  }
 
  publish (index, collection, message, options = {}) {
    const request = {
      index,
      collection,
      body: message,
      action: 'publish'
    };
 
    return this.query(request, options)
      .then(response => response.result.published);
  }
 
  subscribe (index, collection, filters, callback, options = {}) {
    const room = new Room(this, index, collection, filters, callback, options);
 
    return room.subscribe()
      .then(() => {
        if (!this.subscriptions[room.id]) {
          this.subscriptions[room.id] = [];
        }
        this.subscriptions[room.id].push(room);
        return room.id;
      });
  }
 
  unsubscribe (roomId, options = {}) {
    const rooms = this.subscriptions[roomId];
 
    if (!rooms) {
      return Promise.reject(new Error(`not subscribed to ${roomId}`));
    }
 
    for (const room of rooms) {
      room.removeListeners();
    }
    delete this.subscriptions[roomId];
 
    return this.query({
      action: 'unsubscribe',
      body: {roomId}
    }, options)
      .then(response => {
        return response.result;
      });
  }
 
  // called on network error or disconnection
  disconnected () {
    for (const roomId of Object.keys(this.subscriptions)) {
      for (const room of this.subscriptions[roomId]) {
        room.removeListeners();
 
        if (room.autoResubscribe) {
          Eif (!this.subscriptionsOff[roomId]) {
            this.subscriptionsOff[roomId] = [];
          }
          this.subscriptionsOff[roomId].push(room);
        }
      }
 
      delete this.subscriptions[roomId];
    }
  }
 
  /**
   * Called on kuzzle reconnection.
   * Resubscribe to eligible disabled rooms.
   */
  reconnected () {
    for (const roomId of Object.keys(this.subscriptionsOff)) {
      for (const room of this.subscriptionsOff[roomId]) {
        if (!this.subscriptions[roomId]) {
          this.subscriptions[roomId] = [];
        }
        this.subscriptions[roomId].push(room);
 
        room.subscribe()
          .catch(() => this.kuzzle.emit('discarded', {request: room.request}));
      }
 
      delete this.subscriptionsOff[roomId];
    }
  }
 
  /**
   * Removes all subscriptions.
   */
  tokenExpired() {
    for (const roomId of Object.keys(this.subscriptions)) {
      this.subscriptions[roomId].forEach(room => room.removeListeners());
    }
 
    this.subscriptions = {};
    this.subscriptionsOff = {};
  }
 
}
 
module.exports = RealTimeController;