Skip to content

Commit a7f3ac1

Browse files
authored
Merge pull request #117 from r04423/fix/reset-query-bit-mask
Reset bit masks after executing tracking queries
2 parents c9f739f + a645eb2 commit a7f3ac1

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

packages/core/src/query/query-result.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,16 @@ export function createQueryResult<T extends QueryParameter[]>(
2323
): QueryResult<T> {
2424
query.commitRemovals(world);
2525
const entities = query.entities.dense.slice() as Entity[];
26+
2627
// Clear so it can accumulate again.
27-
if (query.isTracking) query.entities.clear();
28+
if (query.isTracking) {
29+
query.entities.clear();
30+
31+
// @todo: Need to improve the performance of this loop.
32+
for (const eid of entities) {
33+
query.resetTrackingBitmasks(eid);
34+
}
35+
}
2836

2937
const stores: Store<any>[] = [];
3038
const traits: Trait[] = [];

packages/core/src/query/query.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,13 @@ export class Query {
290290
const result = this.entities.dense.slice();
291291

292292
// Clear so it can accumulate again.
293-
if (this.isTracking) this.entities.clear();
293+
if (this.isTracking) {
294+
this.entities.clear();
295+
296+
for (const eid of result) {
297+
this.resetTrackingBitmasks(eid);
298+
}
299+
}
294300

295301
return result as Entity[];
296302
}

packages/core/tests/query-modifiers.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ describe('Query modifiers', () => {
378378
entityA.remove(Foo);
379379
entityA.add(Foo);
380380
entities = world.query(Added(Foo), Removed(Bar));
381-
expect(entities[0]).toBe(entityA);
381+
expect(entities.length).toBe(0);
382382

383383
// Add Foo to entityB and remove Bar.
384384
// This entity should now match the query.
@@ -460,7 +460,7 @@ describe('Query modifiers', () => {
460460
expect(entities2.length).toBe(1);
461461
});
462462

463-
it.fails('should only update a Changed query when the tracked trait is changed', () => {
463+
it('should only update a Changed query when the tracked trait is changed', () => {
464464
const entity = world.spawn(Foo, Bar);
465465

466466
const Changed = createChanged();
@@ -476,7 +476,7 @@ describe('Query modifiers', () => {
476476
});
477477

478478
// @see https://github.com/pmndrs/koota/issues/115
479-
it.fails('should not trigger Changed query when removing a different trait', () => {
479+
it('should not trigger Changed query when removing a different trait', () => {
480480
const Changed = createChanged();
481481
const entity = world.spawn(Position);
482482

0 commit comments

Comments
 (0)