Skip to content

Commit

Permalink
🔀 Merge pull request #44 from pmndrs:fix/update-each-instability
Browse files Browse the repository at this point in the history
🐛 core: fix updateEach instability
  • Loading branch information
krispya authored Jan 3, 2025
2 parents 2211efa + ed66a15 commit abe6bf5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
10 changes: 6 additions & 4 deletions packages/core/src/query/query-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,16 @@ export function createQueryResult<T extends QueryParameter[]>(
const trait = traits[index];
const ctx = trait[$internal];
const newValue = state[index];
const store = stores[index];

let changed = false;
if (ctx.type === 'aos') {
changed = ctx.fastSetWithChangeDetection(eid, stores[j], newValue);
changed = ctx.fastSetWithChangeDetection(eid, store, newValue);
if (!changed) {
changed = !shallowEqual(newValue, atomicSnapshots[j]);
changed = !shallowEqual(newValue, atomicSnapshots[index]);
}
} else {
changed = ctx.fastSetWithChangeDetection(eid, stores[j], newValue);
changed = ctx.fastSetWithChangeDetection(eid, store, newValue);
}

// Collect changed traits.
Expand All @@ -100,7 +101,8 @@ export function createQueryResult<T extends QueryParameter[]>(
const index = untrackedIndices[j];
const trait = traits[index];
const ctx = trait[$internal];
ctx.fastSet(eid, stores[index], state[index]);
const store = stores[index];
ctx.fastSet(eid, store, state[index]);
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/core/tests/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -715,8 +715,9 @@ describe('Query', () => {

// This has changes tracked automatically.
// Here we test that mixing tracked and untracked traits works.
// We do it would of order to catch misaligned indices internally.
world.spawn(Position, Name);
world.query(Position, Name).updateEach(([position]) => {
world.query(Name, Position).updateEach(([name, position]) => {
position.x = 1;
});

Expand Down

0 comments on commit abe6bf5

Please sign in to comment.