11import { Archetype } from "./archetype" ;
22import { CommandBuffer , type Command } from "./command-buffer" ;
33import type { EntityId , WildcardRelationId } from "./entity" ;
4- import { EntityIdManager , getDetailedIdType , getIdType } from "./entity" ;
4+ import { EntityIdManager , createRelationId , getDetailedIdType , getIdType , isWildcardRelationId } from "./entity" ;
55import { Query } from "./query" ;
66import type { QueryFilter } from "./query-filter" ;
77import 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