'init'
This commit is contained in:
26
app/providers/ProtocolProvider/usePacket.ts
Normal file
26
app/providers/ProtocolProvider/usePacket.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { useEffect } from "react";
|
||||
import { useProtocol } from "./useProtocol";
|
||||
import Packet from "@/app/providers/ProtocolProvider/protocol/packet";
|
||||
|
||||
export const usePacket = (packetId : number, callback: (packet : any) => void, deps?: any[]) => {
|
||||
const {protocol} = useProtocol();
|
||||
|
||||
const waitPacket = (packetId: number, callback: (packet: Packet) => void) : number => {
|
||||
return protocol.waitPacket(packetId, callback);
|
||||
};
|
||||
|
||||
const unwaitPacket = (packetId: number, callback: (packet: Packet) => void) => {
|
||||
protocol.unwaitPacket(packetId, callback);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
let unwait = (receivedPacket: Packet) => {
|
||||
callback(receivedPacket);
|
||||
};
|
||||
waitPacket(packetId, unwait);
|
||||
return () => {
|
||||
// Cleanup function to remove the packet listener
|
||||
unwaitPacket(packetId, unwait);
|
||||
};
|
||||
}, deps);
|
||||
}
|
||||
Reference in New Issue
Block a user