refactor: name

This commit is contained in:
cha0s 2024-11-10 18:16:34 -06:00
parent 3df5266ba3
commit 500ada03ff

View File

@ -1,14 +1,14 @@
import gather from '@/lib/gather.js';
const Gathered = gather(import.meta.glob('./*.js', {eager: true, import: 'default'}));
const Packets = gather(import.meta.glob('./*.js', {eager: true, import: 'default'}));
const typeToId = new Map(Object.entries(Gathered).map(([type], id) => [type, id]));
const idToType = new Map(Object.entries(Gathered).map(([type], id) => [id, type]));
const typeToId = new Map(Object.entries(Packets).map(([type], id) => [type, id]));
const idToType = new Map(Object.entries(Packets).map(([type], id) => [id, type]));
export function decode(packed) {
const view = ArrayBuffer.isView(packed) ? packed : new DataView(packed);
const type = idToType.get(view.getUint16(0, true));
const Packet = Gathered[type];
const Packet = Packets[type];
return {
type,
payload: Packet.decode(view),
@ -16,7 +16,7 @@ export function decode(packed) {
}
export function encode(packet) {
const encoded = Gathered[packet.type].encode(packet.payload);
const encoded = Packets[packet.type].encode(packet.payload);
encoded.setUint16(0, typeToId.get(packet.type), true);
return encoded;
}