refactor: gardening

This commit is contained in:
cha0s 2024-06-27 04:32:31 -05:00
parent 95b666e844
commit 438a0c3be5
4 changed files with 17 additions and 21 deletions

View File

@ -1,4 +1,4 @@
import {createContext, useContext} from 'react';
import {createContext, useContext, useEffect} from 'react';
const context = createContext();
@ -7,3 +7,17 @@ export default context;
export function useClient() {
return useContext(context);
}
export function usePacket(type, fn, dependencies) {
const client = useClient();
useEffect(() => {
if (!client) {
return;
}
client.addPacketListener(type, fn);
return () => {
client.removePacketListener(type, fn);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [client, ...dependencies]);
}

View File

@ -1,17 +0,0 @@
import {useEffect} from 'react';
import {useClient} from '@/context/client.js';
export default function usePacket(type, fn, dependencies) {
const client = useClient();
useEffect(() => {
if (!client) {
return;
}
client.addPacketListener(type, fn);
return () => {
client.removePacketListener(type, fn);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [client, ...dependencies]);
}

View File

@ -2,9 +2,9 @@ import {Container} from '@pixi/react';
import {useEffect, useState} from 'react';
import {RESOLUTION} from '@/constants.js';
import {usePacket} from '@/context/client.js';
import {useEcs} from '@/context/ecs.js';
import {useMainEntity} from '@/context/main-entity.js';
import usePacket from '@/hooks/use-packet.js';
import Entities from './entities.jsx';
import TargetingGhost from './targeting-ghost.jsx';

View File

@ -2,11 +2,10 @@ import {useEffect, useState} from 'react';
import addKeyListener from '@/add-key-listener.js';
import {RESOLUTION} from '@/constants.js';
import {useClient} from '@/context/client.js';
import {useClient, usePacket} from '@/context/client.js';
import {useDebug} from '@/context/debug.js';
import {useEcs} from '@/context/ecs.js';
import {useMainEntity} from '@/context/main-entity.js';
import usePacket from '@/hooks/use-packet.js';
import Disconnected from './disconnected.jsx';
import Dom from './dom.jsx';