Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions client/src/components/BuddyMatcher.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ import {
NEW_EVENT_CHAT_RESTORE,
NEW_EVENT_CLOSED,
NEW_EVENT_INACTIVE,
NEW_EVENT_JOIN,
NEW_EVENT_JOINED,
} from '../../../constants.json';
import { connectWithId, socket } from 'src/lib/socketConnection';
import useBuddyUtils from 'src/lib/buddySocket';
import { useCallback, useEffect, useRef, useState } from 'react';

import Anonymous from 'components/Anonymous';
import { createBrowserNotification } from 'src/lib/browserNotification';
import { isExplicitDisconnection } from 'src/lib/utils';
Expand All @@ -28,6 +27,7 @@ const BuddyMatcher = () => {
const { createChat, closeChat, closeAllChats } = useChat();
const { startSearch, endSearch, app } = useApp();
const { setLoadingText, startNewSearch } = useCloseChat();
const { joinSearch, setupSocketListeners } = useBuddyUtils(socket);

const [disconnected, setDisconnected] = useState(false);
const reconnectAttempts = useRef(0);
Expand Down Expand Up @@ -73,7 +73,7 @@ const BuddyMatcher = () => {
const onConnect = useCallback(() => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

basically everything that has a useCallback, like onConnect for example

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so i move everything that has a useCallback to BuddySocket. let me do that now

// Here server will be informed that user is searching for
// another user
socket.emit(NEW_EVENT_JOIN, {
joinSearch({
loginId: authState.loginId,
email: authState.email,
});
Expand Down Expand Up @@ -132,6 +132,16 @@ const BuddyMatcher = () => {
};

setupSocket();
const cleanupListeners = setupSocketListeners({
onConnectHandler: onConnect,
onUserJoinedHandler: onUserJoined,
onRestoreChatHandler: onRestoreChat,
onCloseHandler: onClose,
onInactiveHandler: onInactive,
onDisconnectHandler: onDisconnect,
onReconnectAttemptHandler: onReconnectAttempt,
onReconnectErrorHandler: onReconnectError,
});

socket.on('connect', onConnect);
socket.on(NEW_EVENT_CLOSED, onClose);
Expand All @@ -143,18 +153,7 @@ const BuddyMatcher = () => {
socket.io.on('reconnect_error', onReconnectError);

return () => {
socket
.off('connect', onConnect)
.off(NEW_EVENT_JOINED, onUserJoined)
.off(NEW_EVENT_CHAT_RESTORE, onRestoreChat)
.off(NEW_EVENT_CLOSED, onClose)
.off(NEW_EVENT_INACTIVE, onInactive)
.off('disconnect', onDisconnect);

socket.io
.off('reconnect_attempt', onReconnectAttempt)
.off('reconnect_error', onReconnectError);

cleanupListeners();
socket.disconnect();
};
}, [app.currentChatId]);
Expand Down
70 changes: 70 additions & 0 deletions client/src/lib/buddySocket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* @typedef {import('socket.io-client').Socket} Socket
*/
import {
NEW_EVENT_CHAT_RESTORE,
NEW_EVENT_CLOSED,
NEW_EVENT_INACTIVE,
NEW_EVENT_JOIN,
NEW_EVENT_JOINED,
} from '../../../constants.json';

/**
*
* @param {Socket} socket
*/
export default function useBuddyUtils(socket) {
function joinSearch({ loginId, email }) {
return new Promise((resolve, reject) => {
if (!socket.connected) {
reject(null);
return;
}

socket.timeout(30000).emit(NEW_EVENT_JOIN, { loginId, email }, (err, response) => {
if (err) {
reject(err);
return;
}

resolve(response);
});
});
}

function setupSocketListeners({
onConnectHandler,
onUserJoinedHandler,
onRestoreChatHandler,
onCloseHandler,
onInactiveHandler,
onDisconnectHandler,
onReconnectAttemptHandler,
onReconnectErrorHandler,
}) {
socket.on('connect', onConnectHandler);
socket.on(NEW_EVENT_JOINED, onUserJoinedHandler);
socket.on(NEW_EVENT_CHAT_RESTORE, onRestoreChatHandler);
socket.on(NEW_EVENT_CLOSED, onCloseHandler);
socket.on(NEW_EVENT_INACTIVE, onInactiveHandler);
socket.on('disconnect', onDisconnectHandler);
socket.io.on('reconnect_attempt', onReconnectAttemptHandler);
socket.io.on('reconnect_error', onReconnectErrorHandler);

return () => {
socket.off('connect', onConnectHandler);
socket.off(NEW_EVENT_JOINED, onUserJoinedHandler);
socket.off(NEW_EVENT_CHAT_RESTORE, onRestoreChatHandler);
socket.off(NEW_EVENT_CLOSED, onCloseHandler);
socket.off(NEW_EVENT_INACTIVE, onInactiveHandler);
socket.off('disconnect', onDisconnectHandler);
socket.io.off('reconnect_attempt', onReconnectAttemptHandler);
socket.io.off('reconnect_error', onReconnectErrorHandler);
};
}

return {
joinSearch,
setupSocketListeners,
};
}
Loading