@@ -112,6 +112,9 @@ export class World<ExtraParams extends any[] = [deltaTime: number]> {
112112
113113 // Remove from component reverse index
114114 this . removeComponentReference ( sourceEntityId , componentType , entityId ) ;
115+
116+ // Trigger component removed hooks
117+ this . executeComponentLifecycleHooks ( sourceEntityId , new Map ( ) , new Set ( [ componentType ] ) ) ;
115118 }
116119 }
117120
@@ -484,29 +487,8 @@ export class World<ExtraParams extends any[] = [deltaTime: number]> {
484487 }
485488 }
486489
487- // Trigger component added hooks
488- for ( const [ componentType , component ] of adds ) {
489- const hooks = this . componentLifecycleHooks . get ( componentType ) ;
490- if ( hooks ) {
491- for ( const hook of hooks ) {
492- if ( hook . onAdded ) {
493- hook . onAdded ( entityId , componentType , component ) ;
494- }
495- }
496- }
497- }
498-
499- // Trigger component removed hooks
500- for ( const componentType of removes ) {
501- const hooks = this . componentLifecycleHooks . get ( componentType ) ;
502- if ( hooks ) {
503- for ( const hook of hooks ) {
504- if ( hook . onRemoved ) {
505- hook . onRemoved ( entityId , componentType ) ;
506- }
507- }
508- }
509- }
490+ // Trigger component lifecycle hooks
491+ this . executeComponentLifecycleHooks ( entityId , adds , removes ) ;
510492 }
511493
512494 /**
@@ -615,4 +597,37 @@ export class World<ExtraParams extends any[] = [deltaTime: number]> {
615597 }
616598 }
617599 }
600+
601+ /**
602+ * Execute component lifecycle hooks for added and removed components
603+ */
604+ private executeComponentLifecycleHooks (
605+ entityId : EntityId ,
606+ addedComponents : Map < EntityId < any > , any > ,
607+ removedComponents : Set < EntityId < any > >
608+ ) : void {
609+ // Trigger component added hooks
610+ for ( const [ componentType , component ] of addedComponents ) {
611+ const hooks = this . componentLifecycleHooks . get ( componentType ) ;
612+ if ( hooks ) {
613+ for ( const hook of hooks ) {
614+ if ( hook . onAdded ) {
615+ hook . onAdded ( entityId , componentType , component ) ;
616+ }
617+ }
618+ }
619+ }
620+
621+ // Trigger component removed hooks
622+ for ( const componentType of removedComponents ) {
623+ const hooks = this . componentLifecycleHooks . get ( componentType ) ;
624+ if ( hooks ) {
625+ for ( const hook of hooks ) {
626+ if ( hook . onRemoved ) {
627+ hook . onRemoved ( entityId , componentType ) ;
628+ }
629+ }
630+ }
631+ }
632+ }
618633}
0 commit comments