refactor: back with latus for HMR smoothness

This commit is contained in:
cha0s 2021-03-25 02:44:44 -05:00
parent c4bde402c0
commit 5eb0523389
2 changed files with 7 additions and 6 deletions

View File

@ -6,13 +6,10 @@ import {
import {useSocket} from '@latus/socket'; import {useSocket} from '@latus/socket';
export default () => { export default () => {
const [room, setRoom] = useState();
const latus = useLatus(); const latus = useLatus();
const [room, setRoom] = useState(latus.get('%room'));
const socket = useSocket(); const socket = useSocket();
const synchronizer = latus.get('%synchronizer'); const synchronizer = latus.get('%synchronizer');
if (latus.get('%room') !== room) {
setRoom(latus.get('%room'));
}
useEffect(() => { useEffect(() => {
if (!room) { if (!room) {
return undefined; return undefined;

View File

@ -1,5 +1,6 @@
import { import {
useEffect, useEffect,
useLatus,
useState, useState,
} from '@latus/react'; } from '@latus/react';
import {useSelector} from '@latus/redux'; import {useSelector} from '@latus/redux';
@ -8,14 +9,16 @@ import {selfEntitySelector} from '../state';
import useRoom from './use-room'; import useRoom from './use-room';
export default () => { export default () => {
const latus = useLatus();
const room = useRoom(); const room = useRoom();
const selfEntity = useSelector(selfEntitySelector); const selfEntity = useSelector(selfEntitySelector);
const [entity, setEntity] = useState(); const [entity, setEntity] = useState(latus.get('%selfEntity'));
useEffect(() => { useEffect(() => {
if (!room) { if (!room) {
return; return;
} }
const onDestroying = () => { const onDestroying = () => {
latus.set('%selfEntity', undefined);
setEntity(undefined); setEntity(undefined);
}; };
const augmentSelfEntity = async (entity) => { const augmentSelfEntity = async (entity) => {
@ -25,9 +28,10 @@ export default () => {
}); });
entity.on('destroying', onDestroying); entity.on('destroying', onDestroying);
} }
latus.set('%selfEntity', entity);
setEntity(entity); setEntity(entity);
}; };
augmentSelfEntity(room.findEntity(selfEntity)); augmentSelfEntity(room.findEntity(selfEntity));
}, [room, selfEntity]); }, [latus, room, selfEntity]);
return entity; return entity;
}; };