avocado-old/packages/net/packet/registry.js

21 lines
570 B
JavaScript
Raw Normal View History

2019-04-11 15:26:13 -05:00
let eventId = 0;
const idToPacketMap = new Map();
2020-06-12 16:27:35 -05:00
const keytoIdMap = new Map();
const packetToIdMap = new Map();
2019-04-11 15:26:13 -05:00
export function allPackets() {
return Array.from(idToPacketMap.values());
}
export function idFromPacket(Packet) {
return packetToIdMap.get(Packet);
}
export function packetFromId(id) {
return idToPacketMap.get(id);
}
2020-06-12 16:27:35 -05:00
export function registerPacket({default: Packet}, key) {
const id = keytoIdMap.has(key) ? keytoIdMap.get(key) : eventId++;
2019-04-11 15:26:13 -05:00
idToPacketMap.set(id, Packet);
2020-06-12 16:27:35 -05:00
keytoIdMap.set(key, id);
packetToIdMap.set(Packet, id);
2019-04-11 15:26:13 -05:00
return id;
}