import { IMinemeldConfigService , IMinemeldConfig , IMinemeldConfigNode } from './config' ; import { IMineMeldAPIService } from './minemeldapi' ; import { IMinemeldPrototypeService , IMinemeldPrototype } from './prototype' ; import { IMineMeldEngineStatusService , IMineMeldEngineStatus } from './enginestatus' ; export interface IMinemeldResolvedConfigNode { name : string ; node : IMinemeldConfigNode ; resolvedPrototype ? : IMinemeldPrototype ; } export interface IMineMeldRunningConfigStatus { nodes : { [ id ] : IMinemeldResolvedConfigNode ; } ; } export interface IMineMeldRunningConfigStatusService { getStatus ( ) : ng . IPromise < IMineMeldRunningConfigStatus > ; } export class MineMeldRunningConfigStatusService implements IMineMeldRunningConfigStatusService { MinemeldConfigService : IMinemeldConfigService ; MineMeldAPIService : IMineMeldAPIService ; MinemeldPrototypeService : IMinemeldPrototypeService ; MineMeldEngineStatusService : IMineMeldEngineStatusService ; toastr : any ; $interval : angular . IIntervalService ; $rootScope : angular . IRootScopeService ; $q : angular . IQService ; currentStatusDeferred : ng . IDeferred < IMineMeldRunningConfigStatus > ; statusSubscription : ( ) => void ; currentStatus : IMineMeldRunningConfigStatus ; ngInject constructor ( MinemeldConfigService , MineMeldAPIService , MinemeldPrototypeService , MineMeldEngineStatusService , toastr , $interval , $rootScope , $q ) { this . MinemeldConfigService = MinemeldConfigService ; this . MineMeldAPIService = MineMeldAPIService ; this . MinemeldPrototypeService = MinemeldPrototypeService ; this . MineMeldEngineStatusService = MineMeldEngineStatusService ; this . toastr = toastr ; this . $interval = $interval ; this . $rootScope = $rootScope ; this . $q = $q ; this . currentStatusDeferred = this . $q . defer < IMineMeldRunningConfigStatus > ( ) ; this . MineMeldAPIService . onLogin ( this . initStatusMonitor . bind ( this ) ) ; this . MineMeldAPIService . onLogout ( this . destroyStatusMonitor . bind ( this ) ) ; if ( this . MineMeldAPIService . isLoggedIn ( ) ) { this . initStatusMonitor ( ) ; } } public getStatus ( ) { if ( ! this . currentStatus ) { return this . currentStatusDeferred . promise ; } return this . $q . when ( this . currentStatus ) ; } private initStatusMonitor ( ) { if ( this . statusSubscription ) { return ; } this . updateRunningConfigStatus ( ) ; } private destroyStatusMonitor ( ) { if ( this . statusSubscription ) { this . statusSubscription ( ) ; this . statusSubscription = undefined ; } this . currentStatus = undefined ; } private changeListener ( event , status ) { this . MineMeldEngineStatusService . getStatus ( ) . then ( ( status ) => { if ( status . statename === 'RUNNING' || status . statename === 'STARTING' ) { this . updateRunningConfigStatus ( ) ; } } ) ; } private updateRunningConfigStatus ( ) { this . MinemeldConfigService . runningConfig ( false ) . then ( ( rconfig ) => { return this . MinemeldPrototypeService . getPrototypeLibraries ( false ) . then ( ( result ) => { var resolvedNodes = [ ] ; angular . forEach ( rconfig . nodes , ( node , nodename ) => { if ( typeof node . prototype === 'undefined' ) { resolvedNodes . push ( this . $q . when ( { name : nodename , node : node } ) ) ; return ; } resolvedNodes . push ( this . MinemeldPrototypeService . getPrototype ( node . prototype ) . then ( ( proto ) => { return { name : nodename , node : node , resolvedPrototype : proto } ; } ) ) ; } ) ; this . $q . all ( resolvedNodes ) . then ( ( rconfignodes ) => { this . currentStatus = { nodes : { } } ; angular . forEach ( rconfignodes , ( node ) => { this . currentStatus . nodes [ node . name ] = node ; } ) ; if ( ! this . statusSubscription ) { this . statusSubscription = this . $rootScope . $on ( 'mm-engine-status-changed' , this . changeListener . bind ( this ) ) ; this . currentStatusDeferred . resolve ( this . currentStatus ) ; } this . $rootScope . $broadcast ( 'mm-running-config-changed' ) ; } , ( error ) => { if ( ! error . cancelled ) { this . toastr . error ( 'ERROR RESOLVING CONFIG PROTOTYPES: ' + error . statusText ) ; } throw error ; } ) ; } , ( error ) => { if ( ! error . cancelled ) { this . toastr . error ( 'ERROR RETRIEVING PROTOTYPES LIBRARIES: ' + error . statusText ) ; } throw error ; } ) ; } , ( error ) => { if ( ! error . cancelled ) { this . toastr . error ( 'ERROR RETRIEVING MINEMELD RUNNING CONFIG: ' + error . statusText ) ; } throw error ; } ) ; } }