<commit_before>var sio = require('socket.io');
var rdb = require('../dbapi/rdb');
var io = null;

var connected_users = [];

exports.configSio = (http)=>{
  if (!io) {
    io = sio(http);
  }

  io.on('connection', (socket)=>{
    //console.log('a user connected');

    socket.on('usr_reg', function(msg) {
      console.log(msg, 'registered!');
      //console.log(msg.role);

      connected_users.push({userinfo: msg, s: socket});
      //console.log(connected_users);
    });

    socket.on('disconnect', ()=>{
      console.log('user disconnected');
    });
  });

};

exports.watch = ()=> {

    rdb.r.table('games').changes({includeTypes: true}).run(rdb.conn, (err, cursor)=>{
      if (!err) {
        cursor.each((err, row)=>{
          //console.log(row);
          if (row.type == 'change') {
            // updated
          } else if (row.type == 'add') {
            // inserted
            var connected_user = connected_users.filter((u)=>{
      //        console.log(u.userinfo.userid);
        //      console.log(row.new_val.userid);
              return u.userinfo.userid === row.new_val.userid;
            });
            if (connected_user) {
          //    console.log(connected_user);
              connected_user[0].s.emit('mygame', {content: row.new_val.content})
            }
          } else if (row.type == 'remove') {
            // deleted
          }
        });
      }<commit_msg>delete cached object when user disconnect.<commit_after>var sio = require('socket.io');
var rdb = require('../dbapi/rdb');
var io = null;

var connected_users = [];

exports.configSio = (http)=>{
  if (!io) {
    io = sio(http);
  }

  io.on('connection', (socket)=>{
    //console.log('a user connected');

    socket.on('usr_reg', function(msg) {
      console.log(msg, 'registered!');
      //console.log(msg.role);

      connected_users.push({userinfo: msg, s: socket});
      //console.log(connected_users);
    });

    socket.on('disconnect', ()=>{
      console.log('user disconnected');

      var cu = connected_users.filter((u)=>{
        return u.s === socket;
      });

      if (cu && cu.length > 0) {
        connected_users.splice(connected_users.indexOf(cu[0]), 1);
        console.log(connected_users);
      }

    });
  });

};

exports.watch = ()=> {

    rdb.r.table('games').changes({includeTypes: true}).run(rdb.conn, (err, cursor)=>{
      if (!err) {
        cursor.each((err, row)=>{
          //console.log(row);
          if (row.type == 'change') {
            // updated
          } else if (row.type == 'add') {
            // inserted
            var connected_user = connected_users.filter((u)=>{
      //        console.log(u.userinfo.userid);
        //      console.log(row.new_val.userid);
              return u.userinfo.userid === row.new_val.userid;
            });
            if (connected_user) {
          //    console.log(connected_user);
              connected_user[0].s.emit('mygame', {content: row.new_val.content})
            }
          } else if (row.type == 'remove') {
            // deleted
          }
        });
      }
{'fraction_non_alphanumeric': 0.18548387096774194, 'fraction_numerical': 0.0016129032258064516, 'mean_word_length': 1.9703065134099618, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1143114', 'n_tokens_mistral': 1010, 'n_tokens_neox': 977, 'n_words': 164}