refactor: gardening
This commit is contained in:
parent
95b666e844
commit
438a0c3be5
|
@ -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]);
|
||||
}
|
|
@ -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]);
|
||||
}
|
|
@ -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';
|
||||
|
|
|
@ -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';
|
||||
|
|
Loading…
Reference in New Issue
Block a user