All files / controllers/realtime index.js

6.98% Statements 3/43
0% Branches 0/28
0% Functions 0/10
6.98% Lines 3/43

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  1x 1x                                                                                                                                                                                                                         1x  
const
  Room = require('./room'),
  _kuzzle = Symbol();
 
class RealTimeController {
 
  /**
   * @param {Kuzzle} kuzzle
   */
  constructor (kuzzle) {
    this[_kuzzle] = kuzzle;
 
    this.subscriptions = {
      filters: {},
      channels: {}
    };
  }
 
  get kuzzle () {
    return this[_kuzzle];
  }
 
  count (roomId, options = {}) {
    if (!roomId) {
      throw new Error('Kuzzle.realtime.count: roomId is required');
    }
 
    return this.kuzzle.query({
      controller: 'realtime',
      action: 'count',
      body: {roomId}
    }, options)
      .then(response => response.result.count);
  }
 
  publish (index, collection, message, options = {}) {
    if (!index) {
      throw new Error('Kuzzle.realtime.publish: index is required');
    }
    if (!collection) {
      throw new Error('Kuzzle.realtime.publish: collection is required');
    }
    if (!message) {
      throw new Error('Kuzzle.realtime.publish: message is required');
    }
 
    const request = {
      index,
      collection,
      body: message,
      controller: 'realtime',
      action: 'publish'
    };
 
    return this.kuzzle.query(request, options)
      .then(response => response.result.published);
  }
 
  subscribe (index, collection, filters, callback, options = {}) {
    if (!index) {
      throw new Error('Kuzzle.realtime.subscribe: index is required');
    }
    if (!collection) {
      throw new Error('Kuzzle.realtime.subscribe: collection is required');
    }
    if (!filters) {
      throw new Error('Kuzzle.realtime.subscribe: filters is required');
    }
    if (!callback || typeof callback !== 'function') {
      throw new Error('Kuzzle.realtime.subscribe: a callback function is required');
    }
 
    const room = new Room(this.kuzzle, 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 = {}) {
    if (!roomId) {
      throw new Error('Kuzzle.realtime.unsubscribe: roomId is required');
    }
 
    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.kuzzle.query({
      controller: 'realtime',
      action: 'unsubscribe',
      body: {roomId}
    }, options)
      .then(response => {
        return response.result;
      });
  }
}
 
module.exports = RealTimeController;