refactor: gather
This commit is contained in:
parent
1492884a87
commit
0536e7b4f2
|
@ -1,7 +1,7 @@
|
||||||
import Arbitrary from '@/ecs/arbitrary.js';
|
import Arbitrary from '@/ecs/arbitrary.js';
|
||||||
import Base from '@/ecs/base.js';
|
import Base from '@/ecs/base.js';
|
||||||
import Schema from '@/ecs/schema.js';
|
import Schema from '@/ecs/schema.js';
|
||||||
import gather from '@/engine/gather.js';
|
import gather from '@/util/gather.js';
|
||||||
|
|
||||||
const specificationsAndOrDecorators = gather(
|
const specificationsAndOrDecorators = gather(
|
||||||
import.meta.glob('./*.js', {eager: true, import: 'default'}),
|
import.meta.glob('./*.js', {eager: true, import: 'default'}),
|
||||||
|
|
|
@ -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'}));
|
export default gather(import.meta.glob('./*.js', {eager: true, import: 'default'}));
|
||||||
|
|
|
@ -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);
|
|
||||||
});
|
|
|
@ -5,9 +5,6 @@ const encoder = new Encoder();
|
||||||
|
|
||||||
export default class Packet {
|
export default class Packet {
|
||||||
|
|
||||||
static id;
|
|
||||||
static type;
|
|
||||||
|
|
||||||
static decode(view) {
|
static decode(view) {
|
||||||
return decoder.decode(new DataView(view.buffer, view.byteOffset + 2, view.byteLength - 2));
|
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);
|
return new DataView(encoder.bytes.buffer, 0, encoder.pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
static gathered(id, type) {
|
|
||||||
this.id = id;
|
|
||||||
this.type = type;
|
|
||||||
}
|
|
||||||
|
|
||||||
static pack(payload) {
|
static pack(payload) {
|
||||||
return payload;
|
return payload;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 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) {
|
export function decode(packed) {
|
||||||
const view = ArrayBuffer.isView(packed) ? packed : new DataView(packed);
|
const view = ArrayBuffer.isView(packed) ? packed : new DataView(packed);
|
||||||
const id = view.getUint16(0, true);
|
const type = idToType.get(view.getUint16(0, true));
|
||||||
const Packet = Gathered[id];
|
const Packet = Gathered[type];
|
||||||
return {
|
return {
|
||||||
type: Packet.type,
|
type,
|
||||||
payload: Packet.decode(view),
|
payload: Packet.decode(view),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function encode(packet) {
|
export function encode(packet) {
|
||||||
const Packet = Gathered[packet.type];
|
const encoded = Gathered[packet.type].encode(packet.payload);
|
||||||
const encoded = Packet.encode(packet.payload);
|
encoded.setUint16(0, typeToId.get(packet.type), true);
|
||||||
encoded.setUint16(0, Packet.id, true);
|
|
||||||
return encoded;
|
return encoded;
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,17 +7,13 @@ export default function gather(imports, options = {}) {
|
||||||
mapperForPath = (path) => path.replace(/\.\/(.*)\.js/, '$1'),
|
mapperForPath = (path) => path.replace(/\.\/(.*)\.js/, '$1'),
|
||||||
} = options;
|
} = options;
|
||||||
const Gathered = {};
|
const Gathered = {};
|
||||||
let id = 1;
|
|
||||||
for (const [path, Component] of Object.entries(imports).sort(([l], [r]) => l < r ? -1 : 1)) {
|
for (const [path, Component] of Object.entries(imports).sort(([l], [r]) => l < r ? -1 : 1)) {
|
||||||
const key = mapperForPath(path)
|
Gathered[
|
||||||
.split('-')
|
mapperForPath(path)
|
||||||
.map(capitalize)
|
.split('-')
|
||||||
.join('');
|
.map(capitalize)
|
||||||
if (Component.gathered) {
|
.join('')
|
||||||
Component.gathered(id, key);
|
] = Component;
|
||||||
}
|
|
||||||
Gathered[key] = Gathered[id] = Component;
|
|
||||||
id += 1;
|
|
||||||
}
|
}
|
||||||
return Gathered;
|
return Gathered;
|
||||||
}
|
}
|
17
app/util/gather.test.js
Normal file
17
app/util/gather.test.js
Normal 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);
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user