all files / app/lib/ agent.js

96.36% Statements 53/55
75% Branches 9/12
88.24% Functions 15/17
96.36% Lines 53/55
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 13× 13× 13×   421×     12× 12× 12×             216× 216× 216× 216× 216× 216×   216× 216×     216× 216×   216× 209×   216× 216×   216× 251× 35× 216×   216× 203× 203×   13×   13× 13×   13×     216× 232×   216×     216×   216× 215× 215×           216× 19× 19×     216× 203×      
var path = require('path')
var socketService = require(path.join('../', 'services', 'socketService'))
var uuid = require('node-uuid')
var events = require('events')
var noop = function () {} // eslint-disable-line no-unused-vars
var watches = new events.EventEmitter()
var watch = function (hash, prop) {
  hash[ 'watched-' + prop ] = hash[ prop ]
  try {
    Object.defineProperty(hash, prop, {
      get: function () {
        return hash[ 'watched-' + prop ]
      },
      set: function (newValue) {
        hash[ 'watched-' + prop ] = newValue
        watches.emit(prop, newValue)
        return newValue
      },
      enumerable: true,
      configurable: false
    })
  } catch (e) {}
}
module.exports = function (agentName, initData, sessionId, session, user) {
  var socket = new events.EventEmitter()
  var isConnecting = false
  var isConnected = false
  var doConnect = function (fn, cb) {
    fn(socket, socket, sessionId, session, user, function () {
      isConnected = true
      // XXX shhhhh, keep this to yourself
      socket.emit('__connected__')
      cb()
    })
  }
  Eif (!sessionId) {
    sessionId = uuid()
  }
  if (!user) {
    user = {}
  }
  Eif (!session) {
    session = {}
  }
  var connect = this.connect = function (cb) {
    if (isConnected) {
      cb()
    } else Iif (isConnecting) {
      socket.once('__connected__', cb)
    } else if (socketService.agents[ agentName ]) {
      isConnecting = true
      doConnect(socketService.agents[ agentName ], cb)
    } else {
      isConnecting = true
      // Listen for the value to be set
      watches.once(agentName, function () {
        doConnect(socketService.agents[ agentName ], cb)
      })
      watch(socketService.agents, agentName)
    }
  }
  this.once = function (event, cb) {
    socket.once(agentName + ':state:' + event + ':' + initData._id, cb)
  }
  this.on = function (event, cb) {
    socket.on(agentName + ':state:' + event + ':' + initData._id, cb)
  }
  this.onError = function (cb) {
    socket.on(agentName + '::error', cb)
  }
  this.send = this.emit = function (event, data) {
    connect(function () {
      socket.emit(agentName + '::' + event, {
        agentData: initData,
        data: data
      })
    })
  }
  this.init = function () {
    connect(function () {
      socket.emit(agentName + '::init', initData)
    })
  }
  this.close = function () {
    socket.emit('close')
  }
}