Skip to content

Commit

Permalink
wrapper function for socket send
Browse files Browse the repository at this point in the history
  • Loading branch information
arnnis committed Jan 6, 2020
1 parent 229e145 commit e18c274
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/actions/teams/thunks.ts
Expand Up @@ -19,7 +19,7 @@ import http from '../../utils/http';
import {RootState} from '../../reducers';
import {NavigationService} from '../../navigation/Navigator';
import {getChats} from '../chats/thunks';
import {closeSocket, init as initRTM} from '../../services/rtm';
import {_closeSocket, init as initRTM} from '../../services/rtm';
import {getCurrentUser} from '../app/thunks';
import {getMembers} from '../members/thunks';
import {SlackError} from '../../utils/errors';
Expand Down Expand Up @@ -127,7 +127,7 @@ export const getTeam = (teamId: string) => async dispatch => {
};

export const switchTeam = (teamId: string) => dispatch => {
closeSocket();
_closeSocket();
batch(() => {
dispatch(setCurrentTeam(teamId));
dispatch(initTeam());
Expand All @@ -147,7 +147,7 @@ export const logoutFromCurrentTeam = () => (dispatch, getState) => {
text: 'Logout',
onPress: () => {
let currentTeam = state.teams.currentTeam;
closeSocket();
_closeSocket();
return dispatch(logout(currentTeam));
},
},
Expand Down
26 changes: 15 additions & 11 deletions src/services/rtm/index.ts
Expand Up @@ -36,8 +36,8 @@ export const init = async () => {
connected = true;
store.dispatch(setConnectionStatus('connected'));

startPing();
stopReconnect();
_startPing();
_stopReconnect();
};

socket.onmessage = ({data}) => {
Expand Down Expand Up @@ -99,27 +99,27 @@ export const init = async () => {
store.dispatch(setConnectionStatus('disconnected'));
connected = false;

!__DEV__ && reconnect();
!__DEV__ && _reconnect();
};

socket.onerror = error => {
alert(`[error] ${error.message}`);
};
};

export const closeSocket = () => {
export const _closeSocket = () => {
if (socket) {
stopPing();
stopReconnect();
_stopPing();
_stopReconnect();
socket.close();
}
};

const startPing = () => {
const _startPing = () => {
pingInterval = setInterval(() => {
// If last ping did not get a response. close the socket.
if (lastPingId) {
closeSocket();
_closeSocket();
return;
}
let message = sendMessage({type: 'ping'});
Expand All @@ -128,23 +128,27 @@ const startPing = () => {
}, 10000);
};

const stopPing = () => {
const _stopPing = () => {
pingInterval && clearInterval(pingInterval);
pingInterval = null;
};

const reconnect = () => {
const _reconnect = () => {
reconnectInterval = setInterval(() => {
console.log('[socket] reconnecting...');
init();
isReconnect = true;
}, 5000);
};

const stopReconnect = () => {
const _stopReconnect = () => {
reconnectInterval && clearInterval(reconnectInterval);
reconnectInterval = null;
};

export const _send = (data: any) => {
socket && !socket.CONNECTING && socket.send(data);
};

export * from './message-events';
export * from './chat-events';
6 changes: 3 additions & 3 deletions src/services/rtm/members-events.ts
@@ -1,4 +1,4 @@
import {socket} from '.';
import {socket, _send} from '.';
import {PresencesQueryRequest, PresenceChangeEvent, PresenceSubscribeRequest} from './types';
import {store} from '../../App';
import {updateEntity} from '../../actions/entities';
Expand All @@ -10,7 +10,7 @@ export const queryPresences = (userIds: Array<string>) => {
type: 'presence_query',
ids: userIds,
};
socket && socket.send(JSON.stringify(data));
_send(JSON.stringify(data));
};

export const subscribePresence = (userIds: string[]) => {
Expand All @@ -19,7 +19,7 @@ export const subscribePresence = (userIds: string[]) => {
type: 'presence_sub',
ids: userIds,
};
socket && socket.send(JSON.stringify(data));
_send(JSON.stringify(data));
};

export const handleUserPresenceChange = (data: PresenceChangeEvent) => {
Expand Down
6 changes: 3 additions & 3 deletions src/services/rtm/message-events.ts
@@ -1,7 +1,7 @@
import {SendInput, PendingMessage, PingMessage} from '../../models';
import {store} from '../../App';
import {addPendingMessage, addMessageToChat, removePendingMessage} from '../../actions/messages';
import {socket} from '.';
import {socket, _send} from '.';
import {batch} from 'react-redux';
import {storeEntities, updateEntity} from '../../actions/entities';
import {RootState} from '../../reducers';
Expand Down Expand Up @@ -33,15 +33,15 @@ export const sendMessage = (input: SendInput) => {
store.dispatch(storeEntities('messages', [pendingMessage]));
store.dispatch(addPendingMessage(pendingMessage));

socket && socket.send(JSON.stringify(pendingMessage));
_send(JSON.stringify(pendingMessage));
}

if (input.type === 'ping') {
let pingMessage: PingMessage = {
id: fingerprint,
type: 'ping',
};
socket && socket.send(JSON.stringify(pingMessage));
_send(JSON.stringify(pingMessage));
}

return true;
Expand Down

0 comments on commit e18c274

Please sign in to comment.