import { stringify } from 'jsan';
import socketCluster from 'socketcluster-client';
import socketOptions from '../constants/socketOptions';

let socket;
let channel;

export function dispatchRemotely(type, action, id, bareState) {
  let state = bareState;
  if (type !== 'IMPORT') state = stringify(state);
  socket.emit(
    id ? 'sc-' + id : 'respond',
    { type, action, state }
  );
}

export function dispatchSync(state, id) {
  socket.emit(
    'respond',
    { type: 'SYNC', id, state: stringify(state) }
  );
}

export function subscribe(subscriber, options = socketOptions, onInstancesChanged) {
  if (channel) channel.unwatch();
  if (socket) socket.disconnect();
  try {
    socket = socketCluster.connect(options);
  } catch (error) {
    // FIXME: show in a dialog component
    /* eslint-disable no-alert */
    alert(error.message);
    /* eslint-enable */
    return false;
  }

  function watch(req) {
    let data;
    if (req.data) {
      data = req.data;
      data.id = req.id;
    } else data = req;

    if (data.type === 'DISCONNECTED') {
      onInstancesChanged(req.id, undefined, true);
      return;
    }
    subscriber(data);
  }

  socket.on('error', function (err) {
    console.error(err);
  });

  socket.on('connect', function () {
    socket.emit('login', {}, (err, channelName) => {
      if (err) { console.error(err); return; }
      channel = socket.subscribe(channelName);
      channel.watch(watch);
      dispatchRemotely('START');
    });
  });
}

@@ -215,96 +215,16 @@
 id, 
-bareState) %7B%0A  let state = bareState;%0A  if (type !== 'IMPORT') state = stringify(state);
+state) %7B
 %0A  s

Fix double serialization