{"size": 843, "ext": "js", "max_stars_repo_path": "src/liveql/liveqlClient.js", "max_stars_repo_name": "Xavyr/LiveQL", "max_stars_repo_stars_event_min_datetime": "2018-03-10T23:32:29.000Z", "max_stars_repo_stars_event_max_datetime": "2021-03-17T03:40:15.000Z", "max_issues_repo_path": "src/liveql/liveqlClient.js", "max_issues_repo_name": "Xavyr/LiveQL", "max_issues_repo_issues_event_min_datetime": "2018-03-11T18:36:01.000Z", "max_issues_repo_issues_event_max_datetime": "2018-03-20T21:15:29.000Z", "max_forks_repo_path": "src/liveql/liveqlClient.js", "max_forks_repo_name": "Xavyr/LiveQL", "max_forks_repo_forks_event_min_datetime": "2018-03-10T23:38:33.000Z", "max_forks_repo_forks_event_max_datetime": "2019-11-07T15:58:14.000Z"}
{"max_stars_count": 75, "max_issues_count": 4, "max_forks_count": 6, "avg_line_length": 24.0857142857, "max_line_length": 61, "alphanum_fraction": 0.6975088968}
/**
 * This file contains the functions needed to set LiveQL up
 * on the client.
 */

import SocketIOClient from 'socket.io-client';

const liveqlClient = {};

// Array of live socket handles.
liveqlClient.handles = [];

liveqlClient.connect = (endpoint) => {
  liveqlClient.socket = SocketIOClient.connect(endpoint);
  liveqlClient.windowUnload();
};

liveqlClient.windowUnload = () => {
  window.addEventListener('beforeunload', () => {
    liveqlClient.socket.emit('unload', liveqlClient.handles);
  });
};

liveqlClient.on = (socketHandler, callback) => {
  if (!liveqlClient.handles.includes(socketHandler)) {
    liveqlClient.handles.push(socketHandler);
    liveqlClient.socket.on(socketHandler, (data) => {
      // Callback function for manipulating GraphQL data.
      callback(data);
    });
  }
};

module.exports = liveqlClient;
