Return false from socket handlers to not dispatch action

This commit is contained in:
Ken-Håvard Lieng 2020-06-05 08:56:11 +02:00
parent a4fe18c0f0
commit a33157ff84
8 changed files with 141 additions and 126 deletions

View file

@ -1,5 +1,4 @@
import { socketAction } from 'state/actions';
import { setConnected } from 'state/app';
import {
print,
addMessage,
@ -31,14 +30,17 @@ export default function handleSocket({
const handlers = {
message(message) {
dispatch(addMessage(message, message.server, message.to));
return false;
},
pm(message) {
dispatch(addMessage(message, message.server, message.from));
return false;
},
messages({ messages, server, to, prepend, next }) {
dispatch(addMessages(messages, server, to, prepend, next));
return false;
},
join({ user, server, channels }) {
@ -74,6 +76,7 @@ export default function handleSocket({
server
)
);
return false;
},
whois(data) {
@ -93,15 +96,18 @@ export default function handleSocket({
tab.name
)
);
return false;
},
print(message) {
const tab = getState().tab.selected;
dispatch(addMessage(message, tab.server, tab.name));
return false;
},
error({ server, target, message }) {
dispatch(addMessage({ content: message, type: 'error' }, server, target));
return false;
},
connection_update({ server, errorType }) {
@ -135,10 +141,6 @@ export default function handleSocket({
}
})
);
},
_connected(connected) {
dispatch(setConnected(connected));
}
};
@ -160,18 +162,12 @@ export default function handleSocket({
action = { ...data, type: socketAction(type) };
}
if (type in handlers) {
handlers[type](data);
}
if (type.charAt(0) === '_') {
if (handlers[type]?.(data) === false) {
return;
}
dispatch(action);
if (type in afterHandlers) {
afterHandlers[type](data);
}
afterHandlers[type]?.(data);
});
}

View file

@ -69,6 +69,7 @@ export const socket = createSocketActions([
'channels',
'channel_forward',
'channel_search',
'connected',
'connection_update',
'error',
'features',
@ -78,6 +79,7 @@ export const socket = createSocketActions([
'nick_fail',
'nick',
'part',
'kick',
'pm',
'quit',
'search',

View file

@ -37,6 +37,10 @@ export default createReducer(initialState, {
}
},
[actions.socket.CONNECTED](state, { connected }) {
state.connected = connected;
},
[actions.UPDATE_MESSAGE_HEIGHT](state, action) {
state.wrapWidth = action.wrapWidth;
state.charWidth = action.charWidth;
@ -52,10 +56,6 @@ export function appSet(key, value) {
};
}
export function setConnected(connected) {
return appSet('connected', connected);
}
export function setCharWidth(width) {
return appSet('charWidth', width);
}

View file

@ -26,7 +26,7 @@ export default class Socket {
this.ws.onopen = () => {
this.connected = true;
this.emit('_connected', true);
this.emit('connected', { connected: true });
clearTimeout(this.timeoutConnect);
this.backoff.reset();
this.setTimeoutPing();
@ -35,7 +35,7 @@ export default class Socket {
this.ws.onclose = () => {
if (this.connected) {
this.connected = false;
this.emit('_connected', false);
this.emit('connected', { connected: false });
}
clearTimeout(this.timeoutConnect);
clearTimeout(this.timeoutPing);