fix: async

This commit is contained in:
cha0s 2024-06-27 10:17:47 -05:00
parent 2b4c5f5a8e
commit c6557bee39
2 changed files with 19 additions and 22 deletions

View File

@ -231,8 +231,8 @@ test('schedules entities to be deleted when ticking systems', () => {
.to.be.undefined;
});
test('adds components to and remove components from entities when ticking systems', () => {
let addLength, removeLength;
test('adds components to and remove components from entities when ticking systems', async () => {
let promise;
const ecs = new Ecs({
Components: {Foo: wrapProperties('Foo', {bar: {type: 'uint8'}})},
Systems: {
@ -243,10 +243,7 @@ test('adds components to and remove components from entities when ticking system
};
}
tick() {
this.insertComponents(1, {Foo: {}});
}
finalize() {
addLength = Array.from(this.select('default')).length;
promise = this.insertComponents(1, {Foo: {}});
}
},
RemoveComponent: class extends System {
@ -256,10 +253,9 @@ test('adds components to and remove components from entities when ticking system
};
}
tick() {
console.log(ecs.get(1).Foo)
this.removeComponents(1, ['Foo']);
}
finalize() {
removeLength = Array.from(this.select('default')).length;
console.log(ecs.get(1).Foo)
}
},
},
@ -267,20 +263,21 @@ test('adds components to and remove components from entities when ticking system
ecs.system('AddComponent').active = true;
ecs.create();
ecs.tick(1);
expect(addLength)
await promise;
expect(Array.from(ecs.system('AddComponent').select('default')).length)
.to.equal(1);
expect(ecs.get(1).Foo)
.to.not.be.undefined;
ecs.system('AddComponent').active = false;
ecs.system('RemoveComponent').active = true;
ecs.tick(1);
expect(removeLength)
expect(Array.from(ecs.system('RemoveComponent').select('default')).length)
.to.equal(0);
expect(ecs.get(1).Foo)
.to.be.undefined;
});
test('generates coalesced diffs for entity creation', async () => {
test('generates diffs for entity creation', async () => {
const ecs = new Ecs();
let entity;
entity = await ecs.create();
@ -288,10 +285,10 @@ test('generates coalesced diffs for entity creation', async () => {
.to.deep.equal({[entity]: {}});
});
test('generates diffs for adding and removing components', () => {
test('generates diffs for adding and removing components', async () => {
const ecs = new Ecs({Components: {Position}});
let entity;
entity = ecs.create();
entity = await ecs.create();
ecs.setClean();
ecs.insert(entity, {Position: {x: 64}});
expect(ecs.diff)
@ -409,7 +406,7 @@ test('calculates entity size', () => {
.to.equal(30);
});
test('serializes and deserializes', () => {
test('serializes and deserializes', async () => {
const ecs = new Ecs({Components: {Empty, Name, Position}});
ecs.createSpecific(1, {Empty: {}, Position: {x: 64}});
ecs.createSpecific(16, {Name: {name: 'foobar'}, Position: {x: 128}});
@ -422,7 +419,7 @@ test('serializes and deserializes', () => {
systems: [],
});
const view = Ecs.serialize(ecs);
const deserialized = Ecs.deserialize(
const deserialized = await Ecs.deserialize(
new Ecs({Components: {Empty, Name, Position}}),
view,
);
@ -442,12 +439,12 @@ test('serializes and deserializes', () => {
.to.equal('foobar');
});
test('deserializes from compatible ECS', () => {
test('deserializes from compatible ECS', async () => {
const ecs = new Ecs({Components: {Empty, Name, Position}});
ecs.createSpecific(1, {Empty: {}, Position: {x: 64}});
ecs.createSpecific(16, {Name: {name: 'foobar'}, Position: {x: 128}});
const view = Ecs.serialize(ecs);
const deserialized = Ecs.deserialize(
const deserialized = await Ecs.deserialize(
new Ecs({Components: {Empty, Name}}),
view,
);

View File

@ -47,12 +47,12 @@ export default class System {
finalize() {}
insertComponents(entityId, components) {
this.ecs.insert(entityId, components);
async insertComponents(entityId, components) {
return this.ecs.insert(entityId, components);
}
insertManyComponents(components) {
this.ecs.insertMany(components);
async insertManyComponents(components) {
return this.ecs.insertMany(components);
}
static get priority() {