import ServiceElectron , { IPCMessages } from "../service.electron" ; import ServiceStreams , { IStreamInfo } from "../service.streams" ; import Logger from "../../tools/env.logger" ; import { Subscription } from "../../tools/index" ; import { IService } from "../../interfaces/interface.service" ; import { IFile } from "../../../../common/ipc/electron.ipc.messages/concat.files.request" ; import ConcatFiles from "../../controllers/features/concat/concat.files" ; import { Progress } from "indexer-neon" ; import * as Tools from "../../tools/index" ; import { IMapItem } from "../../controllers/files.parsers/interface" ; class description class ServiceConcatFiles implements IService { private _logger : Logger = new Logger ( "ServiceConcatFiles" ) ; private _subscriptions : { [ key ] : Subscription ; } = { } ; private _tasks : Map < string , ConcatFiles > = new Map < string , ConcatFiles > ( ) ; returns public init ( ) { return new Promise ( ( resolve , reject ) => { ServiceElectron . IPC . subscribe ( IPCMessages . ConcatFilesRequest , this . _onConcatFilesRequest . bind ( this ) ) . then ( ( subscription ) => { this . _subscriptions . ConcatFilesRequest = subscription ; this . _subscriptions . 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 . _subscriptions ) . forEach ( ( key ) => { this . _subscriptions [ key ] . destroy ( ) ; } ) ; resolve ( ) ; } ) ; } public getName ( ) { return "ServiceConcatFiles" ; } private _onConcatFilesRequest ( request , response ) { const req = request as IPCMessages . ConcatFilesRequest ; const dest = ServiceStreams . getStreamFile ( req . session ) ; if ( dest instanceof Error ) { return response ( new IPCMessages . ConcatFilesResponse ( { written : 0 , id : req . id , error : `Fail concat file due error: ${ dest . message }` , } ) ) ; } const controller = new ConcatFiles ( req . session , req . files . map ( ( file ) => { return { file : file . file , parser : file . parser , } ; } ) , ( ticks ) => { ServiceStreams . updateProgressSession ( req . id , ticks . ellapsed / ticks . total , req . session ) ; } ) ; controller . write ( ( map ) => { ServiceStreams . pushToStreamFileMap ( dest . streamId , map ) ; } ) . then ( ( res ) => { response ( new IPCMessages . ConcatFilesResponse ( { written : res , id : req . id , } ) ) ; } ) . catch ( ( mergeError ) => { response ( new IPCMessages . ConcatFilesResponse ( { written : 0 , id : req . id , error : mergeError . message , } ) ) ; } ) . cancel ( ( ) => { response ( new IPCMessages . ConcatFilesResponse ( { written : 0 , id : req . id , error : `Operation is canceled` , } ) ) ; } ) . finally ( ( ) => { ServiceStreams . removeProgressSession ( req . id , req . session ) ; this . _tasks . delete ( req . session ) ; } ) ; this . _tasks . set ( req . session , controller ) ; } private _onSessionClosed ( guid ) { const controller = this . _tasks . get ( guid ) ; if ( controller === undefined ) { return ; } controller . abort ( ) . then ( ( ) => { this . _logger . debug ( `Task is aborted` ) ; } ) ; } } export default new ServiceConcatFiles ( ) ;