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

File diff suppressed because one or more lines are too long

View File

@ -4,6 +4,7 @@
"env": {
"browser": true
},
"plugins": ["babel"],
"rules": {
"consistent-return": 0,
"jsx-a11y/click-events-have-key-events": 0,
@ -19,7 +20,10 @@
"react/jsx-props-no-spreading": 0,
"react/prop-types": 0,
"react/state-in-constructor": 0,
"react/static-property-placement": 0
"react/static-property-placement": 0,
"no-unused-expressions": 0,
"babel/no-unused-expressions": 2
},
"settings": {
"import/resolver": {

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);

View File

@ -35,6 +35,7 @@
"eslint-config-prettier": "^6.11.0",
"eslint-import-resolver-webpack": "^0.12.1",
"eslint-loader": "^4.0.2",
"eslint-plugin-babel": "^5.3.0",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-react": "^7.20.0",

View File

@ -3966,6 +3966,13 @@ eslint-module-utils@^2.4.1:
debug "^2.6.9"
pkg-dir "^2.0.0"
eslint-plugin-babel@^5.3.0:
version "5.3.0"
resolved "https://registry.yarnpkg.com/eslint-plugin-babel/-/eslint-plugin-babel-5.3.0.tgz#2e7f251ccc249326da760c1a4c948a91c32d0023"
integrity sha512-HPuNzSPE75O+SnxHIafbW5QB45r2w78fxqwK3HmjqIUoPfPzVrq6rD+CINU3yzoDSzEhUkX07VUphbF73Lth/w==
dependencies:
eslint-rule-composer "^0.3.0"
eslint-plugin-import@^2.20.2:
version "2.20.2"
resolved "https://registry.yarnpkg.com/eslint-plugin-import/-/eslint-plugin-import-2.20.2.tgz#91fc3807ce08be4837141272c8b99073906e588d"
@ -4018,6 +4025,11 @@ eslint-plugin-react@^7.20.0:
string.prototype.matchall "^4.0.2"
xregexp "^4.3.0"
eslint-rule-composer@^0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/eslint-rule-composer/-/eslint-rule-composer-0.3.0.tgz#79320c927b0c5c0d3d3d2b76c8b4a488f25bbaf9"
integrity sha512-bt+Sh8CtDmn2OajxvNO+BX7Wn4CIWMpTRm3MaiKPCQcnnlm0CS2mhui6QaoeQugs+3Kj2ESKEEGJUdVafwhiCg==
eslint-scope@^4.0.3:
version "4.0.3"
resolved "https://registry.yarnpkg.com/eslint-scope/-/eslint-scope-4.0.3.tgz#ca03833310f6889a3264781aa82e63eb9cfe7848"