<commit_before>import { advance } from '../actions/BoothActionCreators';
import { receive as chatReceive } from '../actions/ChatActionCreators';
import { join as userJoin, leave as userLeave } from '../actions/UserActionCreators';
import { joinedWaitlist, leftWaitlist, updatedWaitlist } from '../actions/WaitlistActionCreators';
import WebSocket from 'ReconnectingWebSocket';

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

let socket = null;
let sentJWT = false;
let queue = [];

function maybeAuthenticateOnConnect(state) {
  const jwt = state.auth.jwt;
  debug('open', jwt);
  if (jwt) {
    socket.send(jwt);
    sentJWT = jwt;
  } else {
    sentJWT = false;
  }
}

function send(command, data) {
  if (socket.readyState === WebSocket.OPEN) {
    socket.send(JSON.stringify({ command, data }));
  } else {
    queue.push({ command, data });
  }
}

function drainQueuedMessages() {
  const messages = queue;
  queue = [];
  messages.forEach(msg => send(msg.command, msg.data));
}

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

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

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

export function auth(jwt) {
  if (!sentJWT && socket.readyState === WebSocket.OPEN) {
    socket.send(jwt);
  }
}

export function sendMessage(chatMessage) {
  send('sendChat', chatMessage.payload.message);
}

export function connect(store, url = location.href.replace(/^http(s)?:/, 'ws$1:')) {
  socket = new WebSocket(url);
  socket.onmessage = pack => {
    onMessage(store.dispatch, pack.data);
  };
  socket.onopen = () => {
    maybeAuthenticateOnConnect(store.getState());
    drainQueuedMessages();
  };
}
<commit_msg>socket: require() WebSocket constructor lazily for easier prerendering<commit_after>import { advance } from '../actions/BoothActionCreators';
import { receive as chatReceive } from '../actions/ChatActionCreators';
import { join as userJoin, leave as userLeave } from '../actions/UserActionCreators';
import { joinedWaitlist, leftWaitlist, updatedWaitlist } from '../actions/WaitlistActionCreators';

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

let socket = null;
let sentJWT = false;
let queue = [];

function maybeAuthenticateOnConnect(state) {
  const jwt = state.auth.jwt;
  debug('open', jwt);
  if (jwt) {
    socket.send(jwt);
    sentJWT = jwt;
  } else {
    sentJWT = false;
  }
}

function send(command, data) {
  if (socket.readyState === WebSocket.OPEN) {
    socket.send(JSON.stringify({ command, data }));
  } else {
    queue.push({ command, data });
  }
}

function drainQueuedMessages() {
  const messages = queue;
  queue = [];
  messages.forEach(msg => send(msg.command, msg.data));
}

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

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

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

export function auth(jwt) {
  if (!sentJWT && socket.readyState === WebSocket.OPEN) {
    socket.send(jwt);
  }
}

export function sendMessage(chatMessage) {
  send('sendChat', chatMessage.payload.message);
}

export function connect(store, url = location.href.replace(/^http(s)?:/, 'ws$1:')) {
  const WebSocket = require('ReconnectingWebSocket');
  socket = new WebSocket(url);
  socket.onmessage = pack => {
    onMessage(store.dispatch, pack.data);
  };
  socket.onopen = () => {
    maybeAuthenticateOnConnect(store.getState());
    drainQueuedMessages();
  };
}

{'fraction_non_alphanumeric': 0.14797639123102868, 'fraction_numerical': 0.00042158516020236085, 'mean_word_length': 3.8418367346938775, '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': '725730', 'n_tokens_mistral': 1512, 'n_tokens_neox': 1506, 'n_words': 336}