avocado-old/packages/net/packet/registry.js
2019-04-24 18:01:17 -05:00

22 lines
504 B
JavaScript

let eventId = 0;
const packetToIdMap = new Map();
const idToPacketMap = new Map();
export function allPackets() {
return Array.from(idToPacketMap.values());
}
export function idFromPacket(Packet) {
return packetToIdMap.get(Packet);
}
export function packetFromId(id) {
return idToPacketMap.get(id);
}
export function registerPacket(Packet) {
if (packetToIdMap.has(Packet)) {
return;
}
const id = eventId++;
packetToIdMap.set(Packet, id);
idToPacketMap.set(id, Packet);
return id;
}