refactor: useSocket

This commit is contained in:
cha0s 2020-12-13 04:49:23 -06:00
parent 6b5fbd540c
commit 14a6b80a44
2 changed files with 49 additions and 54 deletions

View File

@ -28,7 +28,8 @@ export default function Chat() {
const hasChannel = !!useSelector((state) => channelSelector(state, channel)); const hasChannel = !!useSelector((state) => channelSelector(state, channel));
const historyLength = useSelector(historyLengthSelector); const historyLength = useSelector(historyLengthSelector);
const renderedChannel = renderChannel(channel); const renderedChannel = renderChannel(channel);
useSocket((socket) => { const socket = useSocket();
useEffect(() => {
let handle; let handle;
const load = throttle(async () => { const load = throttle(async () => {
setActivity(await socket.send(['Activity'])); setActivity(await socket.send(['Activity']));
@ -41,7 +42,7 @@ export default function Chat() {
clearInterval(handle); clearInterval(handle);
} }
}; };
}, [channel]); }, [channel, socket]);
const isAllowed = user.id > 0 || !!channel; const isAllowed = user.id > 0 || !!channel;
useEffect(() => { useEffect(() => {
if (!isAllowed) { if (!isAllowed) {

View File

@ -1,6 +1,6 @@
import {useDispatch, useSelector} from 'react-redux'; import {useDispatch, useSelector} from 'react-redux';
import {useSocket} from '@latus/socket/client'; import {useSocketPacket} from '@latus/socket/client';
import { import {
addMessage, addMessage,
joined, joined,
@ -20,58 +20,52 @@ import {
export default function Dispatcher() { export default function Dispatcher() {
const dispatch = useDispatch(); const dispatch = useDispatch();
const id = useSelector(idSelector); const id = useSelector(idSelector);
useSocket((socket) => { useSocketPacket((packet) => {
const onPacket = (packet) => { switch (packet.constructor.name) {
switch (packet.constructor.name) { case 'AddFriend': {
case 'AddFriend': { const {adderId, addeeId, name} = packet.data;
const {adderId, addeeId, name} = packet.data; dispatch(addFriendship({
dispatch(addFriendship({ addeeId,
addeeId, adderId,
adderId, status: 'pending',
status: 'pending', }));
})); dispatch(setUsernames({[id === adderId ? addeeId : adderId]: name}));
dispatch(setUsernames({[id === adderId ? addeeId : adderId]: name})); break;
break;
}
case 'Block': {
dispatch(block(packet.data));
break;
}
case 'ConfirmFriend': {
dispatch(confirmFriendship(packet.data));
break;
}
case 'Message': {
dispatch(addMessage(packet.data));
break;
}
case 'Join': {
dispatch(joined(packet.data));
break;
}
case 'Leave': {
dispatch(left(packet.data));
break;
}
case 'RemoveFriend': {
dispatch(removeFriendship(packet.data));
break;
}
case 'RemoveFavorite': {
dispatch(removeFromFavorites(packet.data));
break;
}
case 'Unblock': {
dispatch(unblock(packet.data));
break;
}
default:
} }
}; case 'Block': {
socket.on('packet', onPacket); dispatch(block(packet.data));
socket.on('disconnect', () => socket.off('packet', onPacket)); break;
socket.on('reconnect', () => socket.on('packet', onPacket)); }
return () => socket.off('packet', onPacket); case 'ConfirmFriend': {
dispatch(confirmFriendship(packet.data));
break;
}
case 'Message': {
dispatch(addMessage(packet.data));
break;
}
case 'Join': {
dispatch(joined(packet.data));
break;
}
case 'Leave': {
dispatch(left(packet.data));
break;
}
case 'RemoveFriend': {
dispatch(removeFriendship(packet.data));
break;
}
case 'RemoveFavorite': {
dispatch(removeFromFavorites(packet.data));
break;
}
case 'Unblock': {
dispatch(unblock(packet.data));
break;
}
default:
}
}, []); }, []);
return null; return null;
} }