Skip to content

Commit 3763072

Browse files
committed
refactor(world): unify lifecycle hook registration methods
- Renamed `registerComponentLifecycleHook` and `unregisterComponentLifecycleHook` to `registerLifecycleHook` and `unregisterLifecycleHook` for consistency. - Merged component and wildcard relation lifecycle hooks into a single `lifecycleHooks` map. - Updated related code in the `World` class and tests to reflect these changes.
1 parent e72705e commit 3763072

3 files changed

Lines changed: 41 additions & 61 deletions

File tree

examples/simple/demo.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ function main() {
5252
world.addComponent(entity2, VelocityId, { x: -0.5, y: 1 });
5353

5454
// 注册组件钩子
55-
world.registerComponentLifecycleHook(PositionId, {
55+
world.registerLifecycleHook(PositionId, {
5656
onAdded: (entityId, componentType, component) => {
5757
console.log(
5858
`组件添加钩子触发: 实体 ${entityId} 添加了 ${componentType} 组件,值为 (${component.x}, ${component.y})`,
5959
);
6060
},
6161
});
6262

63-
world.registerComponentLifecycleHook(VelocityId, {
63+
world.registerLifecycleHook(VelocityId, {
6464
onRemoved: (entityId, componentType) => {
6565
console.log(`组件移除钩子触发: 实体 ${entityId} 移除了 ${componentType} 组件`);
6666
},

src/world.test.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ describe("World", () => {
431431
let hookComponentType: EntityId<Position> | undefined;
432432
let hookComponent: Position | undefined;
433433

434-
world.registerComponentLifecycleHook(positionComponent, {
434+
world.registerLifecycleHook(positionComponent, {
435435
onAdded: (entityId, componentType, component) => {
436436
hookCalled = true;
437437
hookEntityId = entityId;
@@ -461,7 +461,7 @@ describe("World", () => {
461461
let hookEntityId: EntityId | undefined;
462462
let hookComponentType: EntityId<Position> | undefined;
463463

464-
world.registerComponentLifecycleHook(positionComponent, {
464+
world.registerLifecycleHook(positionComponent, {
465465
onRemoved: (entityId, componentType) => {
466466
hookCalled = true;
467467
hookEntityId = entityId;
@@ -485,13 +485,13 @@ describe("World", () => {
485485
let hook1Called = false;
486486
let hook2Called = false;
487487

488-
world.registerComponentLifecycleHook(positionComponent, {
488+
world.registerLifecycleHook(positionComponent, {
489489
onAdded: () => {
490490
hook1Called = true;
491491
},
492492
});
493493

494-
world.registerComponentLifecycleHook(positionComponent, {
494+
world.registerLifecycleHook(positionComponent, {
495495
onAdded: () => {
496496
hook2Called = true;
497497
},
@@ -512,7 +512,7 @@ describe("World", () => {
512512
let addedCalled = false;
513513
let removedCalled = false;
514514

515-
world.registerComponentLifecycleHook(positionComponent, {
515+
world.registerLifecycleHook(positionComponent, {
516516
onAdded: () => {
517517
addedCalled = true;
518518
},
@@ -540,7 +540,7 @@ describe("World", () => {
540540

541541
let addedCalled = false;
542542

543-
world.registerComponentLifecycleHook(positionComponent, {
543+
world.registerLifecycleHook(positionComponent, {
544544
onAdded: () => {
545545
addedCalled = true;
546546
},
@@ -562,7 +562,7 @@ describe("World", () => {
562562

563563
let removedCalled = false;
564564

565-
world.registerComponentLifecycleHook(positionComponent, {
565+
world.registerLifecycleHook(positionComponent, {
566566
onRemoved: () => {
567567
removedCalled = true;
568568
},
@@ -585,13 +585,16 @@ describe("World", () => {
585585
// Create a relation component (positionComponent -> entity2)
586586
const relationId = createRelationId(positionComponent, entity2);
587587

588+
// Create a wildcard relation ID for positionComponent
589+
const wildcardRelationId = createRelationId(positionComponent, "*");
590+
588591
let addedCalled = false;
589592
let removedCalled = false;
590593
let addedComponentType: EntityId<{ x: number; y: number }> | undefined;
591594
let removedComponentType: EntityId<{ x: number; y: number }> | undefined;
592595

593596
// Register a wildcard relation hook for positionComponent
594-
world.registerWildcardRelationLifecycleHook(positionComponent, {
597+
world.registerLifecycleHook(wildcardRelationId, {
595598
onAdded: (entityId, componentType, component) => {
596599
addedCalled = true;
597600
addedComponentType = componentType;
@@ -624,10 +627,13 @@ describe("World", () => {
624627
const entity1 = world.createEntity();
625628
const entity2 = world.createEntity();
626629

630+
// Create a wildcard relation ID for positionComponent
631+
const wildcardRelationId = createRelationId(positionComponent, "*");
632+
627633
let hookCalled = false;
628634

629635
// Register a wildcard relation hook for positionComponent
630-
world.registerWildcardRelationLifecycleHook(positionComponent, {
636+
world.registerLifecycleHook(wildcardRelationId, {
631637
onAdded: () => {
632638
hookCalled = true;
633639
},

src/world.ts

Lines changed: 24 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Archetype } from "./archetype";
22
import { CommandBuffer, type Command } from "./command-buffer";
33
import type { EntityId, WildcardRelationId } from "./entity";
4-
import { EntityIdManager, getDetailedIdType, getIdType } from "./entity";
4+
import { EntityIdManager, createRelationId, getDetailedIdType, getIdType, isWildcardRelationId } from "./entity";
55
import { Query } from "./query";
66
import type { QueryFilter } from "./query-filter";
77
import type { System } from "./system";
@@ -23,15 +23,9 @@ export class World<ExtraParams extends any[] = [deltaTime: number]> {
2323
private componentToArchetypes = new Map<EntityId<any>, Archetype[]>();
2424

2525
/**
26-
* Hook storage for component lifecycle events
26+
* Hook storage for component and wildcard relation lifecycle events
2727
*/
28-
private componentLifecycleHooks = new Map<EntityId<any>, Set<LifecycleHook<any>>>();
29-
30-
/**
31-
* Hook storage for wildcard relation lifecycle events
32-
* Maps base component type to set of wildcard relation hooks
33-
*/
34-
private wildcardRelationLifecycleHooks = new Map<EntityId<any>, Set<LifecycleHook>>();
28+
private lifecycleHooks = new Map<EntityId<any>, Set<LifecycleHook<any>>>();
3529

3630
/**
3731
* Reverse index tracking which entities use each entity as a component type
@@ -221,48 +215,24 @@ export class World<ExtraParams extends any[] = [deltaTime: number]> {
221215
}
222216

223217
/**
224-
* Register a lifecycle hook for component events
218+
* Register a lifecycle hook for component or wildcard relation events
225219
*/
226-
registerComponentLifecycleHook<T>(componentType: EntityId<T>, hook: LifecycleHook<T>): void {
227-
if (!this.componentLifecycleHooks.has(componentType)) {
228-
this.componentLifecycleHooks.set(componentType, new Set());
220+
registerLifecycleHook<T>(componentType: EntityId<T>, hook: LifecycleHook<T>): void {
221+
if (!this.lifecycleHooks.has(componentType)) {
222+
this.lifecycleHooks.set(componentType, new Set());
229223
}
230-
this.componentLifecycleHooks.get(componentType)!.add(hook);
224+
this.lifecycleHooks.get(componentType)!.add(hook);
231225
}
232226

233227
/**
234-
* Unregister a lifecycle hook for component events
228+
* Unregister a lifecycle hook for component or wildcard relation events
235229
*/
236-
unregisterComponentLifecycleHook<T>(componentType: EntityId<T>, hook: LifecycleHook<T>): void {
237-
const hooks = this.componentLifecycleHooks.get(componentType);
230+
unregisterLifecycleHook<T>(componentType: EntityId<T>, hook: LifecycleHook<T>): void {
231+
const hooks = this.lifecycleHooks.get(componentType);
238232
if (hooks) {
239233
hooks.delete(hook);
240234
if (hooks.size === 0) {
241-
this.componentLifecycleHooks.delete(componentType);
242-
}
243-
}
244-
}
245-
246-
/**
247-
* Register a lifecycle hook for wildcard relation events
248-
* The hook will be triggered for any component that matches the wildcard relation pattern
249-
*/
250-
registerWildcardRelationLifecycleHook<T>(baseComponentType: EntityId<T>, hook: LifecycleHook<T>): void {
251-
if (!this.wildcardRelationLifecycleHooks.has(baseComponentType)) {
252-
this.wildcardRelationLifecycleHooks.set(baseComponentType, new Set());
253-
}
254-
this.wildcardRelationLifecycleHooks.get(baseComponentType)!.add(hook as LifecycleHook<any>);
255-
}
256-
257-
/**
258-
* Unregister a lifecycle hook for wildcard relation events
259-
*/
260-
unregisterWildcardRelationLifecycleHook<T>(baseComponentType: EntityId<T>, hook: LifecycleHook<T>): void {
261-
const hooks = this.wildcardRelationLifecycleHooks.get(baseComponentType);
262-
if (hooks) {
263-
hooks.delete(hook as LifecycleHook<any>);
264-
if (hooks.size === 0) {
265-
this.wildcardRelationLifecycleHooks.delete(baseComponentType);
235+
this.lifecycleHooks.delete(componentType);
266236
}
267237
}
268238
}
@@ -674,9 +644,10 @@ export class World<ExtraParams extends any[] = [deltaTime: number]> {
674644
): void {
675645
// Trigger component added hooks
676646
for (const [componentType, component] of addedComponents) {
677-
const hooks = this.componentLifecycleHooks.get(componentType);
678-
if (hooks) {
679-
for (const hook of hooks) {
647+
// Trigger direct component hooks
648+
const directHooks = this.lifecycleHooks.get(componentType);
649+
if (directHooks) {
650+
for (const hook of directHooks) {
680651
if (hook.onAdded) {
681652
hook.onAdded(entityId, componentType, component);
682653
}
@@ -690,7 +661,8 @@ export class World<ExtraParams extends any[] = [deltaTime: number]> {
690661
detailedType.type === "component-relation" ||
691662
detailedType.type === "wildcard-relation"
692663
) {
693-
const wildcardHooks = this.wildcardRelationLifecycleHooks.get(detailedType.componentId!);
664+
const wildcardRelationId = createRelationId(detailedType.componentId!, "*");
665+
const wildcardHooks = this.lifecycleHooks.get(wildcardRelationId);
694666
if (wildcardHooks) {
695667
for (const hook of wildcardHooks) {
696668
if (hook.onAdded) {
@@ -703,9 +675,10 @@ export class World<ExtraParams extends any[] = [deltaTime: number]> {
703675

704676
// Trigger component removed hooks
705677
for (const componentType of removedComponents) {
706-
const hooks = this.componentLifecycleHooks.get(componentType);
707-
if (hooks) {
708-
for (const hook of hooks) {
678+
// Trigger direct component hooks
679+
const directHooks = this.lifecycleHooks.get(componentType);
680+
if (directHooks) {
681+
for (const hook of directHooks) {
709682
if (hook.onRemoved) {
710683
hook.onRemoved(entityId, componentType);
711684
}
@@ -719,7 +692,8 @@ export class World<ExtraParams extends any[] = [deltaTime: number]> {
719692
detailedType.type === "component-relation" ||
720693
detailedType.type === "wildcard-relation"
721694
) {
722-
const wildcardHooks = this.wildcardRelationLifecycleHooks.get(detailedType.componentId!);
695+
const wildcardRelationId = createRelationId(detailedType.componentId!, "*");
696+
const wildcardHooks = this.lifecycleHooks.get(wildcardRelationId);
723697
if (wildcardHooks) {
724698
for (const hook of wildcardHooks) {
725699
if (hook.onRemoved) {

0 commit comments

Comments
 (0)