Skip to content

Commit 029c9e1

Browse files
committed
refactor(entity): replace decodeRelationId with specific getters
1 parent ff1f915 commit 029c9e1

File tree

3 files changed

+13
-15
lines changed

3 files changed

+13
-15
lines changed

src/archetype.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import type { EntityId, RelationId, WildcardRelationId } from "./entity";
22
import {
3-
decodeRelationId,
3+
getComponentIdFromRelationId,
44
getDetailedIdType,
55
getIdType,
6+
getTargetIdFromRelationId,
67
isDontFragmentComponent,
78
isWildcardRelationId,
89
} from "./entity";
@@ -278,8 +279,7 @@ export class Archetype {
278279
}
279280

280281
if (isWildcardRelationId(componentType)) {
281-
const decoded = decodeRelationId(componentType);
282-
const componentId = decoded.componentId;
282+
const componentId = getComponentIdFromRelationId(componentType);
283283
const relations: [EntityId<unknown>, any][] = [];
284284

285285
// Check regular archetype components
@@ -507,14 +507,13 @@ export class Archetype {
507507
for (const relType of matchingRelations) {
508508
const dataArray = this.getComponentData(relType);
509509
const data = dataArray[entityIndex];
510-
const decodedRel = decodeRelationId(relType as RelationId<any>);
511-
relations.push([decodedRel.targetId, data === MISSING_COMPONENT ? undefined : data]);
510+
const targetId = getTargetIdFromRelationId(relType)!;
511+
relations.push([targetId, data === MISSING_COMPONENT ? undefined : data]);
512512
}
513513

514514
// Add dontFragment relations for this entity
515515
// Get the component ID from the wildcard relation type
516-
const wildcardDecoded = decodeRelationId(wildcardRelationType);
517-
const targetComponentId = wildcardDecoded.componentId;
516+
const targetComponentId = getComponentIdFromRelationId(wildcardRelationType);
518517

519518
const dontFragmentData = this.dontFragmentRelations.get(entityId);
520519
if (dontFragmentData) {
@@ -533,9 +532,9 @@ export class Archetype {
533532
// If no relations found and not optional, this entity doesn't match
534533
if (relations.length === 0) {
535534
if (!optional) {
536-
const wildcardDecoded = decodeRelationId(wildcardRelationType);
535+
const componentId = getComponentIdFromRelationId(wildcardRelationType);
537536
throw new Error(
538-
`No matching relations found for mandatory wildcard relation component ${wildcardDecoded.componentId} on entity ${entityId}`,
537+
`No matching relations found for mandatory wildcard relation component ${componentId} on entity ${entityId}`,
539538
);
540539
}
541540
// For optional, return undefined when there are no relations

src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export {
1818
isEntityId,
1919
isRelationId,
2020
isWildcardRelationId,
21-
decodeRelationId,
2221
getComponentIdByName,
2322
getComponentNameById,
2423
} from "./entity";

src/query-filter.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Archetype } from "./archetype";
22
import type { EntityId } from "./entity";
3-
import { decodeRelationId, getDetailedIdType, isRelationId } from "./entity";
3+
import { getComponentIdFromRelationId, getDetailedIdType, isRelationId } from "./entity";
44

55
/**
66
* Filter options for queries
@@ -29,8 +29,8 @@ export function matchesComponentTypes(archetype: Archetype, componentTypes: Enti
2929
// For wildcard relations, check if archetype contains the component relation
3030
return archetype.componentTypes.some((archetypeType) => {
3131
if (!isRelationId(archetypeType)) return false;
32-
const decoded = decodeRelationId(archetypeType);
33-
return decoded.componentId === detailedType.componentId;
32+
const componentId = getComponentIdFromRelationId(archetypeType);
33+
return componentId === detailedType.componentId;
3434
});
3535
} else {
3636
// For regular components, check direct inclusion
@@ -50,8 +50,8 @@ export function matchesFilter(archetype: Archetype, filter: QueryFilter): boolea
5050
// For wildcard relations in negative filter, exclude archetypes that contain ANY relation with the same component
5151
return !archetype.componentTypes.some((archetypeType) => {
5252
if (!isRelationId(archetypeType)) return false;
53-
const decoded = decodeRelationId(archetypeType);
54-
return decoded.componentId === detailedType.componentId;
53+
const componentId = getComponentIdFromRelationId(archetypeType);
54+
return componentId === detailedType.componentId;
5555
});
5656
} else {
5757
// For regular components, check direct exclusion

0 commit comments

Comments
 (0)