Copy action payload before passing it to socket handlers

This commit is contained in:
Ken-Håvard Lieng 2017-07-03 00:52:16 +02:00
parent 8a2fbaca7f
commit 403f7d0942
2 changed files with 27 additions and 32 deletions

File diff suppressed because one or more lines are too long

View File

@ -3,7 +3,6 @@ import { setConnected } from 'state/app';
import { broadcast, inform, print, addMessage, addMessages } from 'state/messages'; import { broadcast, inform, print, addMessage, addMessages } from 'state/messages';
import { select } from 'state/tab'; import { select } from 'state/tab';
import { normalizeChannel } from 'util'; import { normalizeChannel } from 'util';
import { replace } from 'util/router';
function withReason(message, reason) { function withReason(message, reason) {
return message + (reason ? ` (${reason})` : ''); return message + (reason ? ` (${reason})` : '');
@ -52,12 +51,6 @@ export default function handleSocket({ socket, store: { dispatch, getState } })
dispatch(inform(`${user} joined the channel`, server, joinedChannel)); dispatch(inform(`${user} joined the channel`, server, joinedChannel));
}, },
servers(data) {
if (!data) {
dispatch(replace('/connect'));
}
},
part({ user, server, channel, reason }) { part({ user, server, channel, reason }) {
dispatch(inform(withReason(`${user} left the channel`, reason), server, channel)); dispatch(inform(withReason(`${user} left the channel`, reason), server, channel));
}, },
@ -111,15 +104,17 @@ export default function handleSocket({ socket, store: { dispatch, getState } })
}; };
socket.onMessage((type, data) => { socket.onMessage((type, data) => {
let action;
if (Array.isArray(data)) {
action = { type: socketAction(type), data: [...data] };
} else {
action = { ...data, type: socketAction(type) };
}
if (type in handlers) { if (type in handlers) {
handlers[type](data); handlers[type](data);
} }
type = socketAction(type); dispatch(action);
if (Array.isArray(data)) {
dispatch({ type, data });
} else {
dispatch({ type, ...data });
}
}); });
} }