import { BehaviorSubject , Observable } from "rxjs" ; import { Controller , IoActor , IoStateEvent , Uuid } from ".." ; type IoActorItems = [ IoActor , BehaviorSubject < boolean > , BehaviorSubject < any > ] ; /**
 * Provides convenience methods for observing IO values and for
 * monitoring changes in the association state of specific IO actors.
 */ export class IoActorController extends Controller { private _actorItems : Map < Uuid , IoActorItems > ; onInit ( ) { super . onInit ( ) ; this . _actorItems = new Map < Uuid , IoActorItems > ( ) ; } onCommunicationManagerStarting ( ) { super . onCommunicationManagerStarting ( ) ; this . _reregisterAll ( ) ; } onCommunicationManagerStopping ( ) { super . onCommunicationManagerStopping ( ) ; } remarks param actor returns observeIoValue < T > ( actor ) { const [ , , valueSubject ] = this . _ensureRegistered < T > ( actor ) ; return valueSubject . asObservable ( ) ; } param actor returns getIoValue < T > ( actor ) { const [ , , valueSubject ] = this . _ensureRegistered < T > ( actor ) ; return valueSubject . value ; } remarks param actor observeAssociation < T > ( actor ) { const [ , association ] = this . _ensureRegistered < T > ( actor ) ; return association . asObservable ( ) ; } /**
     * Determines whether the given IO actor is currently associated.
     */ isAssociated < T > ( actor ) { const [ , association ] = this . _ensureRegistered < T > ( actor ) ; return association . value ; } private _reregisterAll ( ) { this . _actorItems . forEach ( item => { const actor = item [ 0 ] ; const ioState = this . communicationManager . observeIoState ( actor ) ; const ioValue = this . communicationManager . observeIoValue ( actor ) ; ioState . subscribe ( event => this . _onIoStateChanged ( actor . objectId , event ) ) ; ioValue . subscribe ( value => this . _onIoValueChanged ( actor . objectId , value ) ) ; item [ 1 ] . next ( ioState . value . data . hasAssociations ) ; } ) ; } private _ensureRegistered < T > ( actor ) { const actorId = actor . objectId ; let item = this . _actorItems . get ( actorId ) ; if ( ! item ) { const ioState = this . communicationManager . observeIoState ( actor ) ; const ioValue = this . communicationManager . observeIoValue ( actor ) ; ioState . subscribe ( event => this . _onIoStateChanged ( actorId , event ) ) ; ioValue . subscribe ( value => this . _onIoValueChanged ( actorId , value ) ) ; item = [ actor , new BehaviorSubject < boolean > ( ioState . value . data . hasAssociations ) , new BehaviorSubject < T > ( undefined ) , ] ; this . _actorItems . set ( actorId , item ) ; } return item ; } private _onIoStateChanged ( actorId , event ) { const item = this . _actorItems . get ( actorId ) ; if ( ! item ) { return ; } if ( item [ 1 ] . value !== event . data . hasAssociations ) { item [ 1 ] . next ( event . data . hasAssociations ) ; } } private _onIoValueChanged ( actorId , value ) { const item = this . _actorItems . get ( actorId ) ; if ( ! item ) { return ; } item [ 2 ] . next ( value ) ; } }