import { EventEmitter , Injectable } from '@angular/core' ; import { CommunicatorEntity } from './communicator.entity' ; export interface ICommunicators { [ id ] : CommunicatorEntity ; } @ Injectable ( ) export class ImageryCommunicatorService { public communicators : ICommunicators = { } ; public instanceCreated = new EventEmitter < { id : string ; } > ( ) ; public instanceRemoved = new EventEmitter < { id : string ; } > ( ) ; public has ( id ) { return Boolean ( this . communicators [ id ] ) ; } public provide ( id ) { if ( ! this . communicators [ id ] ) { return null ; } return this . communicators [ id ] ; } communicatorsAsArray ( ) { return Object . values ( this . communicators ) as CommunicatorEntity [ ] ; } public createCommunicator ( communicatorEntity ) { if ( this . communicators [ communicatorEntity . id ] ) { throw new Error ( `'Can't create communicator ${ communicatorEntity . id }, already exists!'` ) ; } this . communicators [ communicatorEntity . id ] = communicatorEntity ; this . instanceCreated . emit ( { id : communicatorEntity . id } ) ; } public remove ( id ) { if ( ! this . communicators [ id ] ) { return ; } this . communicators [ id ] = null ; delete ( this . communicators [ id ] ) ; this . instanceRemoved . emit ( { id } ) ; } }