refactor: useSocket
This commit is contained in:
parent
6b5fbd540c
commit
14a6b80a44
|
@ -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) {
|
||||||
|
|
|
@ -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,8 +20,7 @@ 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;
|
||||||
|
@ -67,11 +66,6 @@ export default function Dispatcher() {
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
};
|
|
||||||
socket.on('packet', onPacket);
|
|
||||||
socket.on('disconnect', () => socket.off('packet', onPacket));
|
|
||||||
socket.on('reconnect', () => socket.on('packet', onPacket));
|
|
||||||
return () => socket.off('packet', onPacket);
|
|
||||||
}, []);
|
}, []);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user