import * as Toolkit from 'chipmunk.client.toolkit' ; import ServiceElectronIpc from '../service.electron.ipc' ; import OutputRedirectionsService from './service.output.redirections' ; import { IPC } from '../service.electron.ipc' ; import { IRange } from './service.output.redirections' ; export interface IExportAction { id : string ; caller : ( ) => void ; caption : string ; disabled : boolean ; } export class OutputExportsService { private _logger : Toolkit . Logger = new Toolkit . Logger ( 'OutputExportsService' ) ; private _subscriptions : { [ key ] : Toolkit . Subscription ; } = { } ; constructor ( ) { this . _subscriptions . OutputExportFeatureSelectionRequest = ServiceElectronIpc . subscribe ( IPC . OutputExportFeatureSelectionRequest , this . _ipc_OutputExportFeatureSelectionRequest . bind ( this ) ) ; } public getActions ( session , source ) { return new Promise < IExportAction [ ] > ( ( resolve , reject ) => { ServiceElectronIpc . request < IPC . OutputExportFeaturesResponse > ( new IPC . OutputExportFeaturesRequest ( { session : session , source : source , } ) , IPC . OutputExportFeaturesResponse ) . then ( ( response ) => { resolve ( response . actions . map ( ( action ) => { return { id : action . id , caption : action . caption , disabled : ! action . enabled , caller : this . _caller . bind ( this , session , action . id ) , } ; } ) ) ; } ) . catch ( ( err ) => { this . _logger . warn ( `Fail request export actions due error: ${ err . message }` ) ; reject ( err ) ; } ) ; } ) ; } private _caller ( session , actionId ) { OutputRedirectionsService . getOutputSelectionRanges ( session ) . then ( ( selection ) => { if ( selection === undefined ) { selection = [ ] ; } const converted = selection . map ( ( range ) => { return { from : range . start . output , to : range . end . output } ; } ) ; ServiceElectronIpc . request < IPC . OutputExportFeatureCallResponse > ( new IPC . OutputExportFeatureCallRequest ( { actionId : actionId , session : session , selection : converted , } ) , IPC . OutputExportFeatureCallResponse ) . then ( ( response ) => { if ( response . error ) { this . _logger . warn ( `Fail to call action "${ actionId }" due error: ${ response . error }` ) ; } else { this . _logger . debug ( `Action "${ actionId }" done.` ) ; } } ) . catch ( ( err ) => { this . _logger . warn ( `Fail to call action "${ actionId }" due error: ${ err . message }` ) ; } ) ; } ) . catch ( ( err ) => { this . _logger . warn ( `Fail request selection due error: ${ err . message }` ) ; } ) ; } private _ipc_OutputExportFeatureSelectionRequest ( request , response ) { OutputRedirectionsService . getOutputSelectionRanges ( request . session ) . then ( ( selection ) => { if ( selection === undefined ) { selection = [ ] ; } const converted = selection . map ( ( range ) => { return { from : range . start . output , to : range . end . output } ; } ) ; response ( new IPC . OutputExportFeatureSelectionResponse ( { session : request . session , selection : converted , } ) ) . catch ( ( err ) => { this . _logger . warn ( `Fail to send selection for action "${ request . actionId }" due error: ${ err . message }` ) ; } ) ; } ) . catch ( ( err ) => { this . _logger . warn ( `Fail request selection due error: ${ err . message }` ) ; response ( new IPC . OutputExportFeatureSelectionResponse ( { session : request . session , selection : [ ] , error : err . message , } ) ) . catch ( ( e ) => { this . _logger . warn ( `Fail to send selection for action "${ request . actionId }" due error: ${ e . message }` ) ; } ) ; } ) ; } } export default new OutputExportsService ( ) ;