<commit_before>import dispatcher from '../dispatcher';
import { advance } from '../actions/AdvanceActionCreators';
import { receive as chatReceive } from '../actions/ChatActionCreators';
import { join as userJoin, leave as userLeave } from '../actions/UserActionCreators';
import WebSocket from 'ReconnectingWebSocket';

const debug = require('debug')('uwave:websocket');

let dispatchToken = null;
export { dispatchToken };

let socket = null;
let queue = [];

function sendRaw(message) {
  if (socket.readyState === WebSocket.OPEN) {
    socket.send(message);
  } else {
    queue.push(message);
  }
}

function send(command, data) {
  sendRaw(JSON.stringify({ command, data }));
}

function onMessage(json) {
  const { command, data } = JSON.parse(json);
  debug(command, data);
  // convert between server & client message formats
  switch (command) {
  case 'chatMessage':
    chatReceive({
      _id: data._id + '-' + data.timestamp,
      userID: data._id,
      text: data.message,
      timestamp: data.timestamp
    });
    break;
  case 'advance':
    advance(data);
    break;

  case 'join':
    userJoin(data);
    break;
  case 'leave':
    userLeave(data);
    break;
  default:
    debug('!unknown socket message type');
  }
}

export function connect(url = location.href.replace(/^http(s)?:/, 'ws$1:')) {
  socket = new WebSocket(url);
  socket.onmessage = pack => {
    onMessage(pack.data);
  };
  socket.onopen = () => {
    const messages = queue;
    queue = [];
    messages.forEach(sendRaw);
  };

  dispatchToken = dispatcher.register(({ type, payload }) => {
    switch (type) {
    case 'loginComplete':
      sendRaw(payload.jwt);
      break;
    case 'chatSend':<commit_msg>waitlist: update waitlist on join/leave socket events<commit_after>import dispatcher from '../dispatcher';
import { advance } from '../actions/AdvanceActionCreators';
import { receive as chatReceive } from '../actions/ChatActionCreators';
import { join as userJoin, leave as userLeave } from '../actions/UserActionCreators';
import { joinedWaitlist, leftWaitlist } from '../actions/WaitlistActionCreators';
import WebSocket from 'ReconnectingWebSocket';

const debug = require('debug')('uwave:websocket');

let dispatchToken = null;
export { dispatchToken };

let socket = null;
let queue = [];

function sendRaw(message) {
  if (socket.readyState === WebSocket.OPEN) {
    socket.send(message);
  } else {
    queue.push(message);
  }
}

function send(command, data) {
  sendRaw(JSON.stringify({ command, data }));
}

function onMessage(json) {
  const { command, data } = JSON.parse(json);
  debug(command, data);
  // convert between server & client message formats
  switch (command) {
  case 'chatMessage':
    chatReceive({
      _id: data._id + '-' + data.timestamp,
      userID: data._id,
      text: data.message,
      timestamp: data.timestamp
    });
    break;
  case 'advance':
    advance(data);
    break;

  case 'waitlistJoin':
    joinedWaitlist(data);
    break;
  case 'waitlistLeave':
    leftWaitlist(data);
    break;

  case 'join':
    userJoin(data);
    break;
  case 'leave':
    userLeave(data);
    break;
  default:
    debug('!unknown socket message type');
  }
}

export function connect(url = location.href.replace(/^http(s)?:/, 'ws$1:')) {
  socket = new WebSocket(url);
  socket.onmessage = pack => {
    onMessage(pack.data);
  };
  socket.onopen = () => {
    const messages = queue;
    queue = [];
    messages.forEach(sendRaw);
  };

  dispatchToken = dispatcher.register(({ type, payload }) => {
    switch (type) {
    case 'loginComplete':
      sendRaw(payload.jwt);
      break;
    case 'chatSend':
{'fraction_non_alphanumeric': 0.14676889375684557, 'fraction_numerical': 0.000547645125958379, 'mean_word_length': 3.4767156862745097, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '4403279', 'n_tokens_mistral': 1130, 'n_tokens_neox': 1123, 'n_words': 276}