refactor: gather

This commit is contained in:
cha0s 2024-06-22 22:33:34 -05:00
parent 1492884a87
commit 0536e7b4f2
9 changed files with 34 additions and 45 deletions

View File

@ -1,7 +1,7 @@
import Arbitrary from '@/ecs/arbitrary.js';
import Base from '@/ecs/base.js';
import Schema from '@/ecs/schema.js';
import gather from '@/engine/gather.js';
import gather from '@/util/gather.js';
const specificationsAndOrDecorators = gather(
import.meta.glob('./*.js', {eager: true, import: 'default'}),

View File

@ -1,3 +1,3 @@
import gather from '@/engine/gather.js';
import gather from '@/util/gather.js';
export default gather(import.meta.glob('./*.js', {eager: true, import: 'default'}));

View File

@ -1,18 +0,0 @@
import {expect, test} from 'vitest';
import gather from './gather.js';
test('gathers', async () => {
const Gathered = gather(
import.meta.glob('./test/*.js', {eager: true, import: 'default'}),
{mapperForPath: (path) => path.replace(/\.\/test\/(.*)\.js/, '$1')},
);
expect(Gathered.First.key)
.to.equal('First');
expect(Gathered[1].id)
.to.equal(1);
expect(Gathered.Second.key)
.to.equal('Second');
expect(Gathered[2].id)
.to.equal(2);
});

View File

@ -5,9 +5,6 @@ const encoder = new Encoder();
export default class Packet {
static id;
static type;
static decode(view) {
return decoder.decode(new DataView(view.buffer, view.byteOffset + 2, view.byteLength - 2));
}
@ -18,11 +15,6 @@ export default class Packet {
return new DataView(encoder.bytes.buffer, 0, encoder.pos);
}
static gathered(id, type) {
this.id = id;
this.type = type;
}
static pack(payload) {
return payload;
}

View File

@ -1,20 +1,22 @@
import gather from '@/engine/gather.js';
import gather from '@/util/gather.js';
const Gathered = 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]));
export function decode(packed) {
const view = ArrayBuffer.isView(packed) ? packed : new DataView(packed);
const id = view.getUint16(0, true);
const Packet = Gathered[id];
const type = idToType.get(view.getUint16(0, true));
const Packet = Gathered[type];
return {
type: Packet.type,
type,
payload: Packet.decode(view),
};
}
export function encode(packet) {
const Packet = Gathered[packet.type];
const encoded = Packet.encode(packet.payload);
encoded.setUint16(0, Packet.id, true);
const encoded = Gathered[packet.type].encode(packet.payload);
encoded.setUint16(0, typeToId.get(packet.type), true);
return encoded;
}

View File

@ -7,17 +7,13 @@ export default function gather(imports, options = {}) {
mapperForPath = (path) => path.replace(/\.\/(.*)\.js/, '$1'),
} = options;
const Gathered = {};
let id = 1;
for (const [path, Component] of Object.entries(imports).sort(([l], [r]) => l < r ? -1 : 1)) {
const key = mapperForPath(path)
.split('-')
.map(capitalize)
.join('');
if (Component.gathered) {
Component.gathered(id, key);
}
Gathered[key] = Gathered[id] = Component;
id += 1;
Gathered[
mapperForPath(path)
.split('-')
.map(capitalize)
.join('')
] = Component;
}
return Gathered;
}

17
app/util/gather.test.js Normal file
View File

@ -0,0 +1,17 @@
import {expect, test} from 'vitest';
import gather from './gather.js';
import First from './gather-test/first.js';
import Second from './gather-test/second.js';
test('gathers', async () => {
const Gathered = gather(
import.meta.glob('./gather-test/*.js', {eager: true, import: 'default'}),
{mapperForPath: (path) => path.replace(/\.\/gather-test\/(.*)\.js/, '$1')},
);
expect(Gathered.First)
.to.equal(First);
expect(Gathered.Second)
.to.equal(Second);
});