
function setServerEvent (state, serverEvent) {
  state.serverEvent = serverEvent;
}

function clearServerEvent (state) {
  state.serverEvent = null;
}

function setServerConnectionState (state, isConnected) {
  state.serverConnected = !!isConnected;
}

function REGISTER_HANDLER(state, { table, handler }) {
  if (!state.handlers[table]) {
    state.handlers[table] = [];
  }
  if (!state.handlers[table].includes(handler)) {
    state.handlers[table].push(handler);
  }
}

function UNREGISTER_HANDLER(state, { table, handler }) {
  if (state.handlers[table]) {
    const handlerIndex = state.handlers[table].findIndex(el => el === handler);
    if (handlerIndex != -1) {
      state.handlers[table].splice(handlerIndex, 1);
    }
  }
}


export default {
  setServerEvent,
  clearServerEvent,
  setServerConnectionState,
  REGISTER_HANDLER,
  UNREGISTER_HANDLER
};
