import ServiceElectron , { IPCMessages } from '../service.electron' ; import ServiceStreams , { INewSessionEvent } from '../service.streams' ; import Logger from '../../tools/env.logger' ; import { Subscription } from '../../tools/index' ; import { IService } from '../../interfaces/interface.service' ; export interface IExportAction { id : string ; caption : string ; source : IPCMessages . EOutputExportFeaturesSource ; handler : ( request : IPCMessages . OutputExportFeatureCallRequest ) => Promise < void > ; isEnabled : ( request : IPCMessages . OutputExportFeaturesRequest ) => boolean ; } class description class ServiceOutputExport implements IService { private _logger : Logger = new Logger ( 'ServiceOutputExport' ) ; private _subscription : { [ key ] : Subscription ; } = { } ; private _store : Map < string , Map < string , IExportAction > > = new Map ( ) ; returns public init ( ) { return new Promise ( ( resolve , reject ) => { Promise . all ( [ ServiceElectron . IPC . subscribe ( IPCMessages . OutputExportFeaturesRequest , this . _onOutputExportFeaturesRequest . bind ( this ) ) . then ( ( subscription ) => { this . _subscription . OutputExportFeaturesRequest = subscription ; } ) , ServiceElectron . IPC . subscribe ( IPCMessages . OutputExportFeatureCallRequest , this . _onOutputExportFeatureCallRequest . bind ( this ) as any ) . then ( ( subscription ) => { this . _subscription . OutputExportFeatureCallRequest = subscription ; } ) , ] ) . then ( ( ) => { this . _subscription . onSessionCreated = ServiceStreams . getSubjects ( ) . onSessionCreated . subscribe ( this . _onSessionCreated . bind ( this ) ) ; this . _subscription . onSessionClosed = ServiceStreams . getSubjects ( ) . onSessionClosed . subscribe ( this . _onSessionClosed . bind ( this ) ) ; resolve ( ) ; } ) . catch ( ( error ) => { this . _logger . error ( `Fail to init module due error: ${ error . message }` ) ; reject ( error ) ; } ) ; } ) ; } public destroy ( ) { return new Promise ( ( resolve ) => { Object . keys ( this . _subscription ) . forEach ( ( key ) => { this . _subscription [ key ] . destroy ( ) ; } ) ; resolve ( ) ; } ) ; } public getName ( ) { return 'ServiceOutputExport' ; } public setAction ( session , action , overwrite = true ) { let store = this . _store . get ( session ) ; if ( store === undefined ) { store = new Map ( ) ; } if ( store . has ( action . id ) ) { if ( overwrite ) { store . delete ( action . id ) ; } else { return new Error ( this . _logger . debug ( `Action with id "${ action . id }" is already registred for session "${ session }"` ) ) ; } } store . set ( action . id , action ) ; this . _store . set ( session , store ) ; ServiceElectron . updateMenu ( ) ; return undefined ; } public getActions ( session ) { const actions = this . _store . get ( session ) ; if ( actions === undefined ) { return [ ] ; } return Array . from ( actions . values ( ) ) ; } public callAction ( request ) { const store = this . _store . get ( request . session ) ; if ( store === undefined ) { return Promise . reject ( new Error ( `No any export actions for stream "${ request . session }" has been registred.` ) ) ; } const action = Array . from ( store . values ( ) ) . find ( a => a . id === request . actionId ) ; if ( action === undefined ) { return Promise . reject ( new Error ( `Fail to find action "${ request . actionId }" for stream "${ request . session }".` ) ) ; } const measure = this . _logger . measure ( `Running export action "${ request . actionId }" for stream "${ request . session }"` ) ; return action . handler ( request ) . then ( ( ) => { measure ( ) ; } ) ; } private _onSessionCreated ( event ) { if ( this . _store . has ( event . stream . guid ) ) { return ; } this . _store . set ( event . stream . guid , new Map ( ) ) ; } private _onSessionClosed ( session ) { this . _store . delete ( session ) ; } private _onOutputExportFeaturesRequest ( request , response ) { const req = request as IPCMessages . OutputExportFeaturesRequest ; const store = this . _store . get ( req . session ) ; const actions = [ ] ; if ( store !== undefined ) { store . forEach ( ( action , actionId ) => { if ( action . source !== IPCMessages . EOutputExportFeaturesSource . all && action . source !== req . source ) { return ; } actions . push ( { id : actionId , caption : action . caption , enabled : action . isEnabled ( req ) , } ) ; } ) ; } return response ( new IPCMessages . OutputExportFeaturesResponse ( { session : req . session , actions : actions , } ) ) ; } private _onOutputExportFeatureCallRequest ( request , response ) { this . callAction ( request ) . then ( ( ) => { return response ( new IPCMessages . OutputExportFeatureCallResponse ( { session : request . session , actionId : request . actionId , } ) ) ; } ) . catch ( ( err ) => { response ( new IPCMessages . OutputExportFeatureCallResponse ( { session : request . session , actionId : request . actionId , error : err . message , } ) ) ; } ) ; } } export default ( new ServiceOutputExport ( ) ) ;