var GithubApi = require('github'),
    config = require('../config'),
    Promise = require('promise'),
    _ = require('underscore');

var sockets = [];
var pulls = [];

   function sendInitialData(socket) {
      socket.emit('allPulls', _.invoke(pulls, 'toObject'));
   }

   // TODO: pass along full Pull object to the client, so that CRs, QAs,
   // build status, etc. can be shown.
   function notifyAboutPullStateChange(pull) {
      sockets.forEach(function(socket) {
         socket.emit('pullChange', pull.toObject());
      });
   }

   /**
    * Removes the given socket from the collection
    */
   function removeSocket(socket) {
      var index = sockets.indexOf(socket);
      if (index !== null) {
         sockets.splice(index);
      }
   }

   function getPull(number) {
      return _.find(pulls, function(pull) {
         return pull.data.number == number;
      });
   }

module.exports = {
   addSocket: function(socket) {
      sockets.push(socket);
      sendInitialData(socket);
      socket.on('disconnect', function() {
         removeSocket(socket);
      });
   },

   addPull: function(pull) {
      console.log(pull.data);
      pulls.push(pull);
      notifyAboutPullStateChange(pull);
   },

   updatePull: function(updatedPull) {
      var pull = getPull(updatedPull.data.number);
      if (!pull) {
         this.addPull(updatedPull);
         return;
      }

      _.extend(pull, updatedPull);
      notifyAboutPullStateChange(pull);
   }
};

var GithubApi = require('github'),
    config = require('../config'),
    Promise = require('promise'),
    _ = require('underscore');

var sockets = [];
var pulls = [];

   function sendInitialData(socket) {
      socket.emit('allPulls', _.invoke(pulls, 'toObject'));
   }

   // TODO: pass along full Pull object to the client, so that CRs, QAs,
   // build status, etc. can be shown.
   function notifyAboutPullStateChange(pull) {
      sockets.forEach(function(socket) {
         socket.emit('pullChange', pull.toObject());
      });
   }

   /**
    * Removes the given socket from the collection
    */
   function removeSocket(socket) {
      var index = sockets.indexOf(socket);
      if (index !== null) {
         sockets.splice(index);
      }
   }

   function getPull(number) {
      return _.find(pulls, function(pull) {
         return pull.data.number === number;
      });
   }

module.exports = {
   addSocket: function(socket) {
      sockets.push(socket);
      sendInitialData(socket);
      socket.on('disconnect', function() {
         removeSocket(socket);
      });
   },

   addPull: function(pull) {
      console.log(pull.data);
      pulls.push(pull);
      notifyAboutPullStateChange(pull);
   },

   updatePull: function(updatedPull) {
      var pull = getPull(updatedPull.data.number);
      if (!pull) {
         this.addPull(updatedPull);
         return;
      }

      _.extend(pull, updatedPull);
      notifyAboutPullStateChange(pull);
   }
};

Use triple ===