<commit_before>import network from './network.js';
import * as monitor from '../monitoring/monitor.js';
import * as time from '../util/time.js';
import EventEmitter from 'events';

export const clients = {};
export const emitter = new EventEmitter();

export function init() {
  network.on('new-client', function(client) {
    if (monitor.isEnabled()) {
      monitor.update({layout: {
        time: time.now(),
        event: `newClient: ${client.rect.serialize()}`,
      }});
    }
    clients[client.socket.id] = client;
    emitter.emit('new-client', client);
  });

  network.on('lost-client', function(id) {
    if (id in clients) {
      if (monitor.isEnabled()) {
        const rect = clients[id].getClientInfo().rect;
        monitor.update({layout: {
          time: time.now(),
          event: `dropClient: ${rect.serialize()}`,
        }});
      }
    } else {
      if (monitor.isEnabled()) {
        monitor.update({layout: {
          time: time.now(),
          event: `dropClient: id ${id}`,
        }});
      }
    }
    delete clients[id];<commit_msg>Fire event when clients disconnect.<commit_after>import network from './network.js';
import * as monitor from '../monitoring/monitor.js';
import * as time from '../util/time.js';
import EventEmitter from 'events';

export const clients = {};
export const emitter = new EventEmitter();

export function init() {
  network.on('new-client', function(client) {
    if (monitor.isEnabled()) {
      monitor.update({layout: {
        time: time.now(),
        event: `newClient: ${client.rect.serialize()}`,
      }});
    }
    clients[client.socket.id] = client;
    emitter.emit('new-client', client);
  });

  network.on('lost-client', function(id) {
    if (id in clients) {
      if (monitor.isEnabled()) {
        const rect = clients[id].rect;
        monitor.update({layout: {
          time: time.now(),
          event: `dropClient: ${rect.serialize()}`,
        }});
      }
      emitter.emit('lost-client', clients[id]);
    } else {
      if (monitor.isEnabled()) {
        monitor.update({layout: {
          time: time.now(),
          event: `dropClient: id ${id}`,
        }});
      }
    }
    delete clients[id];
{'fraction_non_alphanumeric': 0.1734786557674841, 'fraction_numerical': 0.0, 'mean_word_length': 2.5135566188197767, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1328377', 'n_tokens_mistral': 658, 'n_tokens_neox': 656, 'n_words': 149}