Skip to content

Commit 1668b85

Browse files
committed
refactor(archetype): replace direct access with getter for component data
Updated the Archetype class to use the getComponentData method instead of directly accessing componentData. This change improves encapsulation and error handling for component data retrieval. Additionally, modified related tests to ensure they throw appropriate errors when accessing non-existent component types.
1 parent 418004c commit 1668b85

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

src/archetype.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export class Archetype {
9191
// Add component data
9292
for (const componentType of this.componentTypes) {
9393
const data = componentData.get(componentType);
94-
this.componentData.get(componentType)!.push(data === undefined ? MISSING_COMPONENT : data);
94+
this.getComponentData(componentType).push(data === undefined ? MISSING_COMPONENT : data);
9595
}
9696
}
9797

@@ -113,7 +113,7 @@ export class Archetype {
113113
// Extract component data
114114
const removedData = new Map<EntityId<any>, any>();
115115
for (const componentType of this.componentTypes) {
116-
const dataArray = this.componentData.get(componentType)!;
116+
const dataArray = this.getComponentData(componentType);
117117
removedData.set(componentType, dataArray[index]);
118118
dataArray.splice(index, 1);
119119
}
@@ -164,7 +164,7 @@ export class Archetype {
164164
(relDetailed.type === "entity-relation" || relDetailed.type === "component-relation") &&
165165
relDetailed.componentId === componentId
166166
) {
167-
const dataArray = this.componentData.get(relType);
167+
const dataArray = this.getComponentData(relType);
168168
if (dataArray && dataArray[index] !== undefined) {
169169
const data = dataArray[index];
170170
relations.push([relDetailed.targetId, data === MISSING_COMPONENT ? undefined : data]);
@@ -174,7 +174,7 @@ export class Archetype {
174174

175175
return relations;
176176
} else {
177-
const data = this.componentData.get(componentType)?.[index];
177+
const data = this.getComponentData(componentType)[index]!;
178178
return data === MISSING_COMPONENT ? (undefined as T) : data;
179179
}
180180
}
@@ -193,23 +193,27 @@ export class Archetype {
193193
if (index === undefined) {
194194
throw new Error(`Entity ${entityId} is not in this archetype`);
195195
}
196-
const dataArray = this.componentData.get(componentType)!;
196+
const dataArray = this.getComponentData(componentType);
197197
dataArray[index] = data;
198198
}
199199

200200
/**
201201
* Get all entities in this archetype
202202
*/
203203
getEntities(): EntityId[] {
204-
return [...this.entities];
204+
return this.entities;
205205
}
206206

207207
/**
208208
* Get component data for all entities of a specific component type
209209
* @param componentType The component type
210210
*/
211211
getComponentData<T>(componentType: EntityId<T>): T[] {
212-
return [...(this.componentData.get(componentType) || [])];
212+
const data = this.componentData.get(componentType);
213+
if (!data) {
214+
throw new Error(`Component type ${componentType} is not in this archetype`);
215+
}
216+
return data;
213217
}
214218

215219
/**
@@ -268,7 +272,7 @@ export class Archetype {
268272

269273
return matchingRelations;
270274
} else {
271-
return this.componentData.get(compType)!; // Always exists for regular components
275+
return this.getComponentData(compType);
272276
}
273277
});
274278
});
@@ -284,12 +288,10 @@ export class Archetype {
284288
const matchingRelations = dataSource as EntityId<any>[];
285289
const relations: [EntityId<unknown>, any][] = [];
286290
for (const relType of matchingRelations) {
287-
const dataArray = this.componentData.get(relType);
288-
if (dataArray && dataArray[entityIndex] !== undefined) {
289-
const data = dataArray[entityIndex];
290-
const decodedRel = decodeRelationId(relType as RelationId<any>);
291-
relations.push([decodedRel.targetId, data === MISSING_COMPONENT ? undefined : data]);
292-
}
291+
const dataArray = this.getComponentData(relType);
292+
const data = dataArray[entityIndex];
293+
const decodedRel = decodeRelationId(relType as RelationId<any>);
294+
relations.push([decodedRel.targetId, data === MISSING_COMPONENT ? undefined : data]);
293295
}
294296
return relations;
295297
} else {
@@ -311,7 +313,7 @@ export class Archetype {
311313
for (let i = 0; i < this.entities.length; i++) {
312314
const components = new Map<EntityId<any>, any>();
313315
for (const componentType of this.componentTypes) {
314-
const data = this.componentData.get(componentType)![i];
316+
const data = this.getComponentData(componentType)[i];
315317
components.set(componentType, data === MISSING_COMPONENT ? undefined : data);
316318
}
317319
callback(this.entities[i]!, components);

src/world.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from "bun:test";
2-
import { component, createEntityId, relation, type ComponentId, type EntityId } from "./entity";
2+
import { component, createEntityId, relation, type EntityId } from "./entity";
33
import { World } from "./world";
44

55
describe("World", () => {
@@ -79,7 +79,7 @@ describe("World", () => {
7979
world.delete(entity, positionComponent);
8080
world.sync();
8181
expect(world.has(entity, positionComponent)).toBe(false);
82-
expect(world.get(entity, positionComponent)).toBeUndefined();
82+
expect(() => world.get(entity, positionComponent)).toThrow(`Component type 22 is not in this archetype`);
8383
});
8484

8585
it("should throw error when removing invalid component type", () => {

0 commit comments

Comments
 (0)