Skip to content

Commit 725a0c0

Browse files
authored
Merge pull request #145 from pmndrs/refactor/remove-trait-data-class
refactor: remove TraitData class
2 parents 8255c3e + 8fe9cef commit 725a0c0

File tree

5 files changed

+45
-43
lines changed

5 files changed

+45
-43
lines changed

packages/core/src/query/query.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import { $internal } from '../common';
22
import type { Entity } from '../entity/types';
33
import { getEntityId } from '../entity/utils/pack-entity';
44
import { registerTrait, trait } from '../trait/trait';
5-
import type { TraitData } from '../trait/trait-data';
6-
import type { Trait } from '../trait/types';
5+
import type { Trait, TraitData } from '../trait/types';
76
import { SparseSet } from '../utils/sparse-set';
87
import type { World } from '../world/world';
98
import { ModifierData } from './modifier';

packages/core/src/trait/trait-data.ts

Lines changed: 0 additions & 36 deletions
This file was deleted.

packages/core/src/trait/trait.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,16 @@ import { setChanged } from '../query/modifiers/changed';
55
import { getRelationTargets, Pair, Wildcard } from '../relation/relation';
66
import { incrementWorldBitflag } from '../world/utils/increment-world-bit-flag';
77
import type { World } from '../world/world';
8-
import { TraitData } from './trait-data';
9-
import type { ConfigurableTrait, ExtractStore, Norm, Schema, Store, Trait, TraitType } from './types';
8+
import type {
9+
ConfigurableTrait,
10+
ExtractStore,
11+
Norm,
12+
Schema,
13+
Store,
14+
Trait,
15+
TraitData,
16+
TraitType,
17+
} from './types';
1018
import {
1119
createFastSetChangeFunction,
1220
createFastSetFunction,
@@ -49,13 +57,29 @@ export const trait = defineTrait;
4957

5058
export function registerTrait(world: World, trait: Trait) {
5159
const ctx = world[$internal];
52-
const data = new TraitData(world, trait);
60+
const traitCtx = trait[$internal];
61+
62+
const data: TraitData = {
63+
generationId: ctx.entityMasks.length - 1,
64+
bitflag: ctx.bitflag,
65+
trait,
66+
store: traitCtx.createStore(),
67+
queries: new Set(),
68+
notQueries: new Set(),
69+
schema: trait.schema,
70+
changeSubscriptions: new Set(),
71+
addSubscriptions: new Set(),
72+
removeSubscriptions: new Set(),
73+
};
74+
75+
// Bind a reference to the store on the trait for direct access in queries.
76+
traitCtx.stores[world.id] = data.store;
5377

5478
// Add trait to the world.
5579
ctx.traitData.set(trait, data);
5680
world.traits.add(trait);
5781

58-
// Increment the world bitflag.
82+
// Increment the bitflag used for the trait.
5983
incrementWorldBitflag(world);
6084
}
6185

packages/core/src/trait/types.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { $internal } from '../common';
2+
import { Entity } from '../entity/types';
3+
import { Query } from '../query/query';
24
import type { Relation, RelationTarget } from '../relation/types';
35
import type { IsEmpty } from '../utils/types';
46

@@ -116,3 +118,16 @@ export type ExtractStore<T extends Trait> = T extends Trait<any, infer S> ? S :
116118
export type ExtractIsTag<T extends Trait> = T extends Trait<any, any, infer Tag> ? Tag : false;
117119

118120
export type IsTag<T extends Trait> = T extends Trait<any, any, infer Tag> ? Tag : false;
121+
122+
export interface TraitData<T extends Trait = Trait, S extends Schema = ExtractSchema<T>> {
123+
generationId: number;
124+
bitflag: number;
125+
trait: Trait;
126+
store: Store<S>;
127+
queries: Set<Query>;
128+
notQueries: Set<Query>;
129+
schema: S;
130+
changeSubscriptions: Set<(entity: Entity) => void>;
131+
addSubscriptions: Set<(entity: Entity) => void>;
132+
removeSubscriptions: Set<(entity: Entity) => void>;
133+
}

packages/core/src/world/world.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import type { QueryHash, QueryParameter, QueryResult, QueryUnsubscriber } from '
88
import { createQueryHash } from '../query/utils/create-query-hash';
99
import { getTrackingCursor, setTrackingMasks } from '../query/utils/tracking-cursor';
1010
import type { RelationTarget } from '../relation/types';
11+
import type { TraitData } from '../trait/types';
1112
import { addTrait, getTrait, hasTrait, registerTrait, removeTrait, setTrait } from '../trait/trait';
12-
import type { TraitData } from '../trait/trait-data';
1313
import type {
1414
ConfigurableTrait,
1515
ExtractSchema,

0 commit comments

Comments
 (0)