import ServiceElectron , { IPCMessages as IPCElectronMessages , Subscription } from '../services/service.electron' ; import Logger from '../tools/env.logger' ; import { IService } from '../interfaces/interface.service' ; export type TSourceId = number ; export interface ISource { name : string ; session : string ; meta ? : string ; } export class ServiceStreamSources implements IService { private _logger : Logger = new Logger ( 'ServiceStreamSources' ) ; private _seq : number = 1000 ; private _sources : Map < TSourceId , ISource > = new Map ( ) ; public init ( ) { return new Promise ( ( resolve , reject ) => { resolve ( ) ; } ) ; } public destroy ( ) { return new Promise ( ( resolve ) => { resolve ( ) ; } ) ; } public getName ( ) { return 'ServiceStreamSources' ; } public add ( source ) { const id = this . _seq ++ ; this . _sources . set ( id , source ) ; this . _notify ( id , source ) ; return id ; } public get ( id ) { return this . _sources . get ( id ) ; } public set ( id , desc ) { if ( this . _sources . has ( id ) ) { return new Error ( this . _logger . warn ( `Source id "${ id }" is already registred.` ) ) ; } this . _sources . set ( id , desc ) ; this . _notify ( id , desc ) ; } public getIdByName ( session , name ) { let id ; this . _sources . forEach ( ( source , key ) => { if ( id !== undefined ) { return ; } if ( source . session !== session ) { return ; } if ( source . name !== name ) { return ; } id = key ; } ) ; return id ; } private _notify ( id , source ) { ServiceElectron . IPC . send ( new IPCElectronMessages . StreamSourceNew ( { id : id , name : source . name , session : source . session , meta : source . meta , } ) ) . catch ( ( error ) => { this . _logger . warn ( `Fail to notify render about new source ("${ name }") due error: ${ error . message }` ) ; } ) ; } } export default ( new ServiceStreamSources ( ) ) ;