Skip to content

Commit

Permalink
#418 - Fix eslints in Communicator
Browse files Browse the repository at this point in the history
  • Loading branch information
FedeG committed Nov 5, 2018
1 parent 11998b3 commit ff4dbc8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 28 deletions.
57 changes: 30 additions & 27 deletions eventol/front/src/utils/WsCommunicator.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,49 +34,50 @@ export default class WsCommunicator extends React.Component {
};
}

createWebSocket(url){
const websocket = new WebSocket(url);
websocket.onopen = this.onOpen.bind(this);
websocket.onmessage = this.onMessage.bind(this);
websocket.onclose = this.onClose.bind(this);
return websocket;
}

onMessage(data){
onMessage = data => {
const {onMessages} = this.state;
onMessages.map(onMessageFunction => onMessageFunction && onMessageFunction(data));
}

onOpen(data){
onOpen = data => {
const {onOpens} = this.state;
onOpens.map(onOpenFunction => onOpenFunction && onOpenFunction(data));
}

generateInterval(attempts){
const seconds = Math.min(30, (Math.pow(2, attempts) - 1));
return seconds * 1000;
}

onClose(data){
/* eslint-disable react/no-direct-mutation-state */
const {onCloses} = this.state;
onCloses.map(onCloseFunction => onCloseFunction && onCloseFunction(data, this.props.reconnect));
if (this.props.reconnect){
const time = this.generateInterval(this.state.attempts);
onClose = data => {
/* eslint-disable react/no-direct-mutation-state no-console */
const {onCloses, attempts} = this.state;
const {reconnect, wsUrl} = this.props;
onCloses.map(onCloseFunction => onCloseFunction && onCloseFunction(data, reconnect));
if (reconnect){
const time = this.generateInterval(attempts);
this.state.attempts += 1;
setTimeout(() => {
console.warn(`${gettext('Reconecting websocket, attemps')}: ${this.state.attempts}`);
this.state.websocket = this.createWebSocket(this.props.ws_url);
console.warn(`${gettext('Reconecting websocket, attemps')}: ${attempts}`);
this.state.websocket = this.createWebSocket(wsUrl);
}, time);
}
/* eslint-enable react/no-direct-mutation-state */
/* eslint-enable react/no-direct-mutation-state no-console */
}

handleNotSupportWs(){
generateInterval = attempts => {
const seconds = Math.min(30, (2 ** attempts) - 1);
return seconds * 1000;
}

handleNotSupportWs = () => {
const {handlesNotSupportWs} = this.state;
handlesNotSupportWs.map(hNotSupport => hNotSupport && hNotSupport());
}

createWebSocket(url){
const websocket = new WebSocket(url);
websocket.onopen = this.onOpen;
websocket.onmessage = this.onMessage;
websocket.onclose = this.onClose;
return websocket;
}

addOnMessage(onMessageFunction){
const {onMessages} = this.state;
onMessages.push(onMessageFunction);
Expand All @@ -98,12 +99,14 @@ export default class WsCommunicator extends React.Component {
}

checkWebSocketSupport(){
const {handleNotSupportWs} = this.props;
if (!WebSocket){
const error = gettext('websocket not supported by your browser');
if (this.props.handleNotSupportWs){
return this.props.handleNotSupportWs(error);
if (handleNotSupportWs){
return handleNotSupportWs(error);
}
throw error;
}
return null;
}
}
2 changes: 1 addition & 1 deletion eventol/front/src/views/utils/communicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import WsCommunicator from '../../utils/WsCommunicator';
/* eslint-disable-next-line import/prefer-default-export */
export const getCommunicator = properties => new WsCommunicator({
/* eslint-disable-next-line camelcase */
ws_url: `${properties.ws_protocol || 'ws'}://${window.location.host}/updates/`, // TODO: move to url utils
wsUrl: `${properties.ws_protocol || 'ws'}://${window.location.host}/updates/`, // TODO: move to url utils
reconnect: true,
});

0 comments on commit ff4dbc8

Please sign in to comment.