Skip to content

Commit 105b6e2

Browse files
committed
refactor(world): unify lifecycle hook interfaces
1 parent 0723ade commit 105b6e2

1 file changed

Lines changed: 9 additions & 24 deletions

File tree

src/world.ts

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { getOrCreateWithSideEffect } from "./utils";
1111
/**
1212
* Hook types for component lifecycle events
1313
*/
14-
export interface ComponentLifecycleHook<T> {
14+
export interface LifecycleHook<T = unknown> {
1515
/**
1616
* Called when a component is added to an entity
1717
*/
@@ -22,21 +22,6 @@ export interface ComponentLifecycleHook<T> {
2222
onRemoved?: (entityId: EntityId, componentType: EntityId<T>) => void;
2323
}
2424

25-
/**
26-
* Hook types for wildcard relation lifecycle events
27-
* These hooks are triggered for any component that matches a wildcard relation pattern
28-
*/
29-
export interface WildcardRelationLifecycleHook<T = unknown> {
30-
/**
31-
* Called when any component matching the wildcard relation pattern is added to an entity
32-
*/
33-
onAdded?: (entityId: EntityId, componentType: EntityId<T>, component: T) => void;
34-
/**
35-
* Called when any component matching the wildcard relation pattern is removed from an entity
36-
*/
37-
onRemoved?: (entityId: EntityId, componentType: EntityId<T>) => void;
38-
}
39-
4025
/**
4126
* World class for ECS architecture
4227
* Manages entities, components, and systems
@@ -54,13 +39,13 @@ export class World<ExtraParams extends any[] = [deltaTime: number]> {
5439
/**
5540
* Hook storage for component lifecycle events
5641
*/
57-
private componentLifecycleHooks = new Map<EntityId<any>, Set<ComponentLifecycleHook<any>>>();
42+
private componentLifecycleHooks = new Map<EntityId<any>, Set<LifecycleHook<any>>>();
5843

5944
/**
6045
* Hook storage for wildcard relation lifecycle events
6146
* Maps base component type to set of wildcard relation hooks
6247
*/
63-
private wildcardRelationLifecycleHooks = new Map<EntityId<any>, Set<WildcardRelationLifecycleHook>>();
48+
private wildcardRelationLifecycleHooks = new Map<EntityId<any>, Set<LifecycleHook>>();
6449

6550
/**
6651
* Reverse index tracking which entities use each entity as a component type
@@ -237,7 +222,7 @@ export class World<ExtraParams extends any[] = [deltaTime: number]> {
237222
/**
238223
* Register a lifecycle hook for component events
239224
*/
240-
registerComponentLifecycleHook<T>(componentType: EntityId<T>, hook: ComponentLifecycleHook<T>): void {
225+
registerComponentLifecycleHook<T>(componentType: EntityId<T>, hook: LifecycleHook<T>): void {
241226
if (!this.componentLifecycleHooks.has(componentType)) {
242227
this.componentLifecycleHooks.set(componentType, new Set());
243228
}
@@ -247,7 +232,7 @@ export class World<ExtraParams extends any[] = [deltaTime: number]> {
247232
/**
248233
* Unregister a lifecycle hook for component events
249234
*/
250-
unregisterComponentLifecycleHook<T>(componentType: EntityId<T>, hook: ComponentLifecycleHook<T>): void {
235+
unregisterComponentLifecycleHook<T>(componentType: EntityId<T>, hook: LifecycleHook<T>): void {
251236
const hooks = this.componentLifecycleHooks.get(componentType);
252237
if (hooks) {
253238
hooks.delete(hook);
@@ -261,20 +246,20 @@ export class World<ExtraParams extends any[] = [deltaTime: number]> {
261246
* Register a lifecycle hook for wildcard relation events
262247
* The hook will be triggered for any component that matches the wildcard relation pattern
263248
*/
264-
registerWildcardRelationLifecycleHook<T>(baseComponentType: EntityId<T>, hook: WildcardRelationLifecycleHook<T>): void {
249+
registerWildcardRelationLifecycleHook<T>(baseComponentType: EntityId<T>, hook: LifecycleHook<T>): void {
265250
if (!this.wildcardRelationLifecycleHooks.has(baseComponentType)) {
266251
this.wildcardRelationLifecycleHooks.set(baseComponentType, new Set());
267252
}
268-
this.wildcardRelationLifecycleHooks.get(baseComponentType)!.add(hook as WildcardRelationLifecycleHook<any>);
253+
this.wildcardRelationLifecycleHooks.get(baseComponentType)!.add(hook as LifecycleHook<any>);
269254
}
270255

271256
/**
272257
* Unregister a lifecycle hook for wildcard relation events
273258
*/
274-
unregisterWildcardRelationLifecycleHook<T>(baseComponentType: EntityId<T>, hook: WildcardRelationLifecycleHook<T>): void {
259+
unregisterWildcardRelationLifecycleHook<T>(baseComponentType: EntityId<T>, hook: LifecycleHook<T>): void {
275260
const hooks = this.wildcardRelationLifecycleHooks.get(baseComponentType);
276261
if (hooks) {
277-
hooks.delete(hook as WildcardRelationLifecycleHook<any>);
262+
hooks.delete(hook as LifecycleHook<any>);
278263
if (hooks.size === 0) {
279264
this.wildcardRelationLifecycleHooks.delete(baseComponentType);
280265
}

0 commit comments

Comments
 (0)