import { Behavior , IBehaviorAware } from '@babylonjs/core/Behaviors/behavior.js' ; import { Nullable } from '@babylonjs/core/types.js' ; import { CreatedInstance } from '../CreatedInstance' ; import BaseLifecycleListener from './BaseLifecycleListener' ; export default class BehaviorLifecycleListener extends BaseLifecycleListener < Behavior < any > , any > { private behaviorAware : Nullable < IBehaviorAware < any > > = null ; private behavior : Nullable < Behavior < any > > = null ; onParented ( parent , child ) { super . onParented ( parent , child ) ; if ( parent . metadata . isNode && parent . hostInstance ?. addBehavior && child . metadata . isBehavior ) { parent . hostInstance . addBehavior ( child . hostInstance ) ; this . behaviorAware = parent . hostInstance ; this . behavior = child . hostInstance ; } else { console . warn ( 'Could not locate IBehaviorAware on Behavior parent.' ) ; } } onUnmount ( ) { if ( this . behaviorAware ) { this . behaviorAware . removeBehavior ( this . behavior ! ) ; } } }